Shell Script
Shell Script
•Denoted as bash
•It is compatible to the Bourne shell. It includes features from Korn and Bourne
shell.
•Command full-path name is /bin/bash
Check Shell
$ cat /etc/shells
Changing Your Default Shell
Shell scripts store plain text file, generally one command per line.
◦ vi myscript.sh
Make sure you use .bash or .sh file extension for each script. This
ensures easy identification of the shell script.
Setup executable permission
◦ $ chmod +x <your-script-name>
◦ $ chmod 755 <your-script-name>
Run a script (execute a
script)
Now your script is ready with proper executable permission on it. Next,
test the script by running it.
◦ bash <your-script-name>
◦ sh <your-script-name>
◦ ./your-script-name
Examples
◦ $ bash file.sh
◦ $ sh file.sh
◦ $ ./bar file.sh
Variables in Shell
Defining Variables
variable_name=variable_value
Variables in Shell
Defining Variables
variable_name=variable_value
Example:
VAR1="LPU"
VAR2=100
Accessing Values
#!/bin/bash
NAME="LPU"
echo $NAME
Read-only Variables
#!/bin/sh
NAME="Lovely Professional"
readonly NAME
NAME="University"
Unsetting Variables
unset variable_name
#!/bin/bash
NAME="LPU"
unset NAME
echo $NAME
System Environmental Variables
Variable Description
env To list all system environment variables
USER The username
HOME Default path to the user's home directory
Different ways to process the arguments passed to a bash script inside the
script:
1. Positional Parameters
•Arguments passed to a script are processed in the same order in which they’re
sent. The indexing of the arguments starts at one, and the first argument can be
accessed inside the script using $1.
•Similarly, the second argument can be accessed using $2, and so on.
•The positional parameter refers to this representation of the arguments using their
position.
Use Command Line Arguments in a Bash Script
1.Positional Parameters
test [expression]
Example:
[ 5 –gt 6 ] ; echo $?
test command
[1 -eq 1 ] ; echo $?
0
[1 -ne 1 ] ; echo $?
1
[8 -gt 2 ] ; echo $?
0
[2 -ge 2 ] ; echo $?
0
[2 -lt 2 ] ; echo $?
1
[1 -lt 2 ] ; echo $?
0
test command
read choice
if [ $choice -gt 0 ]; then
echo "$choice number is
positive"
else
echo "$ choice number is
negative"
fi
File Test Operators
for i in *
do
statements
done
while loop
Syntax:
while [ condition ]
do
command1 command2
command3 .. ....
done
Example
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \*
$n`"
i=`expr $i + 1`
done
The case Statement
Syntax:
case $variable-name in
pattern1)
command…..;;
pattern2)
command…..;;
pattern N)
command….;;
.
*) command ;;
Example
read var
case $var in
1) echo “One”;;
2) echo “Two”;;
3) echo “Three”;;
4) echo “Four”;;
*) echo "Sorry, it is bigger
than Four";;
esac
break and continue
The break statement
• transfer control to the statement AFTER the done
statement
• terminate execution of the loop
47
The continue command
while [ condition ]
do
cmd-1 This iteration is
over; do the next
continue iteration
cmd-n
done
echo "done"
48
Example:
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index –le 3 ]; then
echo "continue"
continue
fi
echo $index
if [ $index –ge 8 ]; then
echo "break"
break
fi
done
49