Shell Scripting OS LAB
Shell Scripting OS LAB
Shell Scripting OS LAB
◦ vi myscript.sh
Make sure you use .bash or .sh file extension for each script. This
ensures easy identification of shell script.
Setup executable permission
• Once script is created, you need to setup executable permission on a
script. Why?
• Without executable permission, running a script is almost impossible.
• Besides executable permission, script must have a read permission.
• Syntax to 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 script by running it.
◦ bash your-script-name
◦ sh your-script-name
◦ ./your-script-name
Examples
◦ $ bash bar
◦ $ sh bar
◦ $ ./bar
Using Exit code($?)
Every Linux or Unix command executed by the shell script or
user has an exit status.
Exit status is an integer number. 0 exit status means the
command was successful without any errors.
A non-zero (1-255 values) exit status means command was a
failure.
Example
◦ $ echo $vech
◦ $ echo $n
Don’t try
◦ $ echo vech
◦ it will print vech instead its value 'Bus‘.
◦ $ echo n
◦ it will print n instead its value '10‘.
“You must use $ followed by variable name.”
Class work
1. Define variable x with value 10 and print it on
screen.
else
fi
if condition
Syntax:
if condition
then
command1 if condition is true or if
exit status
of condition is 0 (zero)
fi
Example
$ vim myscript.sh
read choice
if [ $choice -gt 0 ]; then
echo "$choice number is positive"
else
echo "$ choice number is negative"
fi
Nested if-else-fi
$ vi nestedif.sh