1. Basicsa. The first line of the script must be
#!/bin/bash
b. Make the script exicutable
chmod +x test.sh
2. Conditional Statement2.1. if statementsyntax:if [ condition
]
thencommands
fi
2.2. if else statementsyntax:if [ condition
]thencommands
elsecommands
fi2.3. if elif statementsyntax:
if [ condition1
]thencommands
elif [ condition2
]
thencommands
elsecommands
fi3.1 Relational operators-eg Equal to
-lt Less than
-gt Greater than
-ge Greater than or equal to
-le Less than or equel to
3.2 File Related Tests-f file True if file exists and is a regular file
-r file True if file exists and is readable
-w file True if file exists and is writable
-x file True if file exists and is executable
-d file True if file exists and is a directory
-s file True if file exists and its size is greater than zero
3.3 String tests-n str True if string str is not a null string
-z str True if string str is a null string
str1 == str2 True if both the strings are equel
str1 != str2 True if both the strings are not equel
str True if str is assigned a value and is not null
3.4 Multiple conditions-a Performs the AND function
-o Performs the OR function
4. Case StatementSyntax: case expression in
pattert1) execute commands;;
pattert2) execute commands;;
pattert3) execute commands;;
....
esacExample:
#!/bin/bash
case `datacut -d" " -f1` in
Mon) commands;;
Tue) commands;;
Wen) commands;;
....
esac5. Looping Statements5.1 while loopsyntax:while [ condtion_is_true
]
doexecute commands
....
doneExample:while [ $NUM -gt 100 ]
do
sleep
done
5.2 until loopSyntax:until [ condition_is_false
]doexecute commands
doneExample:until [ -f $FILE ]
do
sleep 5
done
5.3 for loopSyntax:
for variable
in list
doexecute commands
doneExample:for I in 1 2 3 4 5
do
echo "The value of I is $I";
done
Example 2:#!/bin/bash
LIMIT=10
for ((a=1;a<=$LIMIT;a++)) do echo "$a" done
6. Special symbols$0 Name of the shell script being executed
$1 First parameter passed to the script
$* All the paramerts passes to the script
$# Number of parameters passed to the script
$? Exit status of the last command
7. Read statement#!/bin/bash
echo "Enter your name:"
read NAME
echo "Hello $NAME, Have a nice day."
8. FunctionsSyntax:Function_name ()
{
statements
}
Example:
#!/bin/bash
sumcalc ()
{
SUM= $[ $1 + $2 ]
echo "Result = $SUM"
}
echo -e "Enter the first number:\c"
read NUM1
echo -e "Enter the second number:\c"
read NUM2
read NUM2
# Call the function
sumcal $NUM1 $NUM2