touch hello.sh
#! /bin/bash
#this is a comment
echo "hello world"
-------------------------------------------------------------------------------------------------------
print to screen : echo
echo 'hey'
echo hello
echo "hey "
variables are not parsed in single quoted echo
--------------------------------------------------------------------------------------------------
system variables : env, printenv , set command
echo $HOME
echo $BASH
echo $BASH_VERSION
echo $PWD
echo $PATH
-------------------------------------------------------------------------------------------------------
user defined variable : varname=valueofvar
name=myname
echo my name is $name
variable naming:not starting with number and no special characters except _
----------------------------------------------------------------------------------------------------------
reading user input: read
echo enter name
read name
echo hello $name
echo enter age and sex
read name1 name2 name3
echo $name1 $name2 $name3
read -p 'what is your user name: ' u_name #input with prompt
read -sp 'password: ' pass #silent/hidden input
reading array
read -a names
echo ${names[0]} ${names[1] }
default variable name : REPLY
read
echo $REPLY
----------------------------------------------------------------------------------------------------------------
passing arguments
# arguments are stored in $1 $2 ............
echo $1 $2 $3
$0 stores script name
hello.sh arg1 arg2 agr3 ... argn
$@ stores passed arguments as array
echo $@
echo #
#prints number of arguments passed
-----------------------------------------------------------------------------------------------------
IF- Else- ELseif
syntax
if [ condition ]
then
statement
fi
count=2
if [$count -lt 3 ]
then
echo its less than 3
fi
integer comparision
-eq =equal to
-ne =not equal to
-gt =greater than
-ge = greater tahn equal to
-lt = less than
-le = less than equal to
to use ==,!=,<,<=,>,>= we need double (()) parenthesis
example: if (($var<10 p="">
string comparision
= :equal to [ "$a" = "$b" ]
== :equal to [ "$a" == "$b" ]
!= :not equal [ "$a " != "$b" ]
<:less a="" alphabetical="" ascii="" b="" br="" in="" nbsp="" order="" than="">
>:greater than in ascii order [["$a" > "$b"]]
-z :is null string
# using angle brackets in string needs double [[]] parenthesis
else and elseif condition
if [ conditiion ]
then
statement
elif [ condition ]
then
statement
else
statement
fi
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
file test operator
<10 p="">
<10 p="">
<10 p="">
<10 p="">
echo -e "enter the name of file: \c"
<10 p="">read file_name
<10 p="">if [ -e $file_name ]
<10 p="">then
<10 p="">echo "file found"
<10 p="">else
<10 p="">echo "file not found"
<10 p="">fi
<10 p="">-e enables escape sequence interpretation
<10 p="">\c keep cursor in same line
#! /bin/bash
#this is a comment
echo "hello world"
-------------------------------------------------------------------------------------------------------
print to screen : echo
echo 'hey'
echo hello
echo "hey "
variables are not parsed in single quoted echo
--------------------------------------------------------------------------------------------------
system variables : env, printenv , set command
echo $HOME
echo $BASH
echo $BASH_VERSION
echo $PWD
echo $PATH
-------------------------------------------------------------------------------------------------------
user defined variable : varname=valueofvar
name=myname
echo my name is $name
variable naming:not starting with number and no special characters except _
----------------------------------------------------------------------------------------------------------
reading user input: read
echo enter name
read name
echo hello $name
echo enter age and sex
read name1 name2 name3
echo $name1 $name2 $name3
read -p 'what is your user name: ' u_name #input with prompt
read -sp 'password: ' pass #silent/hidden input
reading array
read -a names
echo ${names[0]} ${names[1] }
default variable name : REPLY
read
echo $REPLY
----------------------------------------------------------------------------------------------------------------
passing arguments
# arguments are stored in $1 $2 ............
echo $1 $2 $3
$0 stores script name
hello.sh arg1 arg2 agr3 ... argn
$@ stores passed arguments as array
echo $@
echo #
#prints number of arguments passed
-----------------------------------------------------------------------------------------------------
IF- Else- ELseif
syntax
if [ condition ]
then
statement
fi
count=2
if [$count -lt 3 ]
then
echo its less than 3
fi
integer comparision
-eq =equal to
-ne =not equal to
-gt =greater than
-ge = greater tahn equal to
-lt = less than
-le = less than equal to
to use ==,!=,<,<=,>,>= we need double (()) parenthesis
example: if (($var<10 p="">
string comparision
= :equal to [ "$a" = "$b" ]
== :equal to [ "$a" == "$b" ]
!= :not equal [ "$a " != "$b" ]
<:less a="" alphabetical="" ascii="" b="" br="" in="" nbsp="" order="" than="">
>:greater than in ascii order [["$a" > "$b"]]
-z :is null string
# using angle brackets in string needs double [[]] parenthesis
else and elseif condition
if [ conditiion ]
then
statement
elif [ condition ]
then
statement
else
statement
fi
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
<10 p="">
file test operator
<10 p="">
<10 p="">
<10 p="">
<10 p="">
echo -e "enter the name of file: \c"
<10 p="">read file_name
<10 p="">if [ -e $file_name ]
<10 p="">then
<10 p="">echo "file found"
<10 p="">else
<10 p="">echo "file not found"
<10 p="">fi
<10 p="">-e enables escape sequence interpretation
<10 p="">\c keep cursor in same line
Comments
Post a Comment