Professional Documents
Culture Documents
Shell Script
Shell Script
www.histpk.org
A Division of Hidaya Trust, Pakistan
#!/bin/bash
• ./script-file
• sh script-file
• bash script-file
• /bin/sh script-file
• /bin/bash script-file
• ‘’ Single quotes
• Enclosed in single quotes remain unchanged.
• `` Back quotes
• Back quotes used to execute shell commands in double quotes.
• ; Semi colon
• Semi colon is used to command terminator.
• Equal ==
• Less than <
• Greater than >
• Not equal to !=
• -d #!/bin/bash
• If directory exists. echo “Enter file Name”
• -r read file
•
if [ -f $file ]; then
If file is read only.
echo "$fiel is exist..."
• -s else
• If non zero file exists. echo "$file does not exist..."
• -w fi
• If file is writeable.
• -x
• If file executable.
#!/bin/bash
clear
echo -n “Enter your name: ”
read name
echo “Your Name is $name”
if [ expression ]
then
Statement(s) to be executed if expression is
true
else
Statement(s) to be executed if expression is
not true
fi
#!/bin/bash
a=20
b=50
if [ $a = $b ]; then
echo “$a and $b are equal”
else
echo “$a and $b are not equal”
fi
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Nested If Conditions
#!#!/bin/bash #!/bin/bash
a=100 OR a=20
b=100 b=50
if [ $a -gt $b ]; then if [ $a -gt $b ]; then
echo "$a is greater than $b" echo “$a is greater than $b”
else elif [ $a -lt $b ]; then
if [ $a -lt $b ]; then echo “$a is less than $b”
echo "$a is less than $b" else
else echo “$a and $b are equal”
echo "$a and $b are equal" fi
fi
fi
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Logical Operators
• || OR operator #!/bin/bash
clear
num1=1000
num2=2000
num3=3000
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
• && AND operator echo "$num1 is Greater than $num2 and $num3"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
echo "$num2 is Greater than $num1 and $num3"
else
echo "$num3 is Greater than $num1 and $num2"
fi