Topics Discussed:
- Introduction
- Shebang
- Data type
- Variables
- Conditional statements
- looping
- Functions
- sed and awk
BASH : Bourne Again SHell scripting Common environment variables
PATH – Sets the search path for any executable command. Similar to the PATH variable in MSDOS.
HOME – Home directory of the user.
USER – User login name. Note: To see what are the values held by the above environment variables, just do an echo of the name of the variable preceded with a $. For example, if I do the following:
$ echo $USER
… I get the value stored in the environment variable USER.
Some bash shell scripting rules
● The first line in your script must be #!/bin/bash
… that is a # (Hash) followed by a ! (bang) followed by the path of the shell. This line lets the environment know the file is a shell script and the location of the shell.
● Before executing your script, you should make the script executable. You do it by using the following command:
$ chmod 755 your_shell_script.sh
● The name of your shell script must end with a .sh This lets the user know that the file is a shell script. This is not compulsory but is the norm.
Conditional statements
‘if’ Statement
● The ‘if’ statement evaluates a condition which accompanies its command line.
syntax:
if condition_is_true
then
execute commands
else
execute commands
fi
Multiway Conditionals
‘if’ condition also permits multiway branching. That is you can evaluate more conditions if the previous condition fails.
if condition_is_true
then
execute commands
elif another_condition_is_true
then
execute commands
else
execute commands
fi
if’s companion – test
● test is an internal feature of the shell.
● ‘test’ evaluates the condition placed on its right, and returns either a true or false exit status.
● For this purpose, ‘test’ uses certain operators to evaluate the condition. They are as follows:
Relational Operators
-eq – Equal to
-lt – Less than
-gt – Greater than
-ge – Greater than or Equal to
-le – Less than or Equal to
File related tests
-f file – True if the 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 has a size greater than zero.
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 strings are equal.
str – True if string str is assigned a value and is not null.
str1 != str2 – True if both strings are unequal.
-s file – True if file exists and has a size greater than zero.
And and Or
Test also permits the checking of more than one expression in the same line.
-a – Performs the AND function
-o – Performs the OR function
Things to remember while using test
● If you are using square brackets [] instead of test, then care should be taken to insert a space after the [ and before the ].
● test is confined to integer values only. Decimal values are simply truncated.
● Do not use wildcards for testing string equality – they are expanded by the shell to match the files in your directory rather than the string.
Case statement
Case statement is the second conditional statement offered by the shell.
SYNTAX :
case expression in
pattern1) execute commands ;;
pattern2) execute commands ;;
…
esac
The keywords here are in, case and esac. The ‘;;’ is used as option terminators. The construct also uses ‘)’ to delimit the pattern from the action.
Looping Statements
while loop
Syntax :
while condition_is_true
do
execute commands
done
until loop
Until complements while constructed in the sense that the loop body here is executed repeatedly as long as the condition remains false.
Syntax:
until false
do
execute commands
done
for loop
Syntax :
for variable in list
do
execute commands
done
Special symbols used in BASH scripting
$* – This denotes all the parameters passed to the script at the time of its execution. Which includes $1, $2 and so on.
$0 – Name of the shell script being executed.
$# – Number of arguments specified in the command line.
$? – Exit status of the last command.
The above symbols are known as positional parameters. .
The set and shift statements
set –
Lets you associate values with these positional parameters .
For example, try this:
$ set `date`
$ echo $1
$ echo $*
shift –
transfers the contents of a positional parameter to its immediate lower numbered one. This goes on as many times it is called.
Example :
$ set `date`
$ echo $1 $2 $3
$ shift
$ echo $1 $2 $3
Make your BASH shell script interactive
● read statement
Make your shell script interactive. read will let the user enter values while the script is being executed. When a program encounters the read statement, the program pauses at that point. Input entered through the keyboard id read into the variables following read, and the program execution continues.
Exit status of the last command
● Every command returns a value after execution. This value is called the exit status or return value of the command. A command is said to be true if it executes successfully, and false if it fails. This can be checked in the script using the $? positional parameter.