We are providing Linux scripting services to our clients, through this article we are trying to provide some of the basics of Linux scripting.
What is a Shell ? :- Shell is a command interpreter – Expands and interprets the command entered before passing to kernel. With GUI, most of the tasks can be done, but not all.
/etc/profile – Ready by all shells and not shell specific
Prompts – A string of characters like $ or # that shell displays when it can accept your command.
Job control – Multitasking. Running many programs in bg and fg.
Wildcard Expansion – *, ?, [ ], !
ls * – all
ls ? – one character
ls [ab]* – starting with a or b
ls [!ab]* – Starting with any except a and b
ls [a-dx-z] – Starting with a, b, c, d, x, y, z
Piping and I/O Redirection – | , <, > , >> , 2>, 1>
Standard Input/Output/Error :-
File Descriptor STDIN – < or 0
File Descriptor STDOUT – > or 1 or 1>
File Descriptor STDERR – 2> or 2
#ls > output_file
#cat < input_file – > overwrites
#ls >> output_file – >> appends
Redirecting only the errors to a error_file :-
#ls non_existent_file 2> error_file – clears existing errors and add a fresh error entry
#ls non_existent_file 2>> error_file – appends the new error to existing entry
#ls existent_file non_existent_file > output_file 2>&1
displays the output and error of ls to output_file
#ls existent_file non_existent_file 2>&1 > output_file
displays error to console and output to output_file
By default, stderr is not passed on to pipes. i.e a command such as ls non_existent_file | grep error won’t show any output, however a small change to command like –> ls non_existent_file 2>&1 | grep -i error will show the error, if any.
Line continuation or Escape character – \
Variable expansion – $PWD, $VARIABLE1
Alias expansion – ll –> ls -l –color=tty
Command auto completion and substitution
# rm -f `ls /tmp` – will delete files inside /tmp, but not directories
# rm -f $(ls /tmp) – is same as above
# cat file ; date – Both commands date and cat file is executed even if there is an error with date
# cat file && date – date executes only if cat file is executed without any errors
# cat file || date – date executes only if cat file is executed with an error
Scripting :- A simple language with only a few reserved words like case, esac, if, fi, eif, done, for, do, function, select in, then, until, while etc.
Shebang line – For a perl script it is “#!/usr/bin/perl” – Location of perl (ofcouse without the double quotes), while in a bash script the file line is “#!/bin/bash”
#vi script.sh
# My first script
#!/bin/bash
echo “Hello. This is my first script”
echo “I am running this script from $(pwd) directory”
To run this script,
#chmod +x script.sh
# ./script.sh OR . script.sh OR source script.sh
$1, $2, $3 refer to arguments.
$@ is “$1”, “$2”, “$3” .. ie array of 3 arguments.
$* is “$1 $2 $3” .. ie considered as 1 argument seperated with space
$# is number of parameters
$? exit status
$$ represents the PID of the running shell
$! represents the PID value of the last child process
man test to see the expressions to test file or string status or values. Common options are
-f – file exists and is ordinary
-d – file exists and is directory
-r – file exists and is readable
-w – file exists and is writable
-x – file exists and is executable
-n – string is not empty
-z – string is empty
== – string comparison and proved equal
!= – string comparison, and not equal
Conditional Statements
if, elif, fi
#!/bin/bash
if [ -f /etc ]
then
echo “/etc is a directory”
else
echo “/etc is what?”
fi
case , esac
echo “Enter your option from 1-5 : ”
read i;
case $i in
1) ls -l ;;
2) ps -aux ;;
3) date ;;
4) who ;;
5) exit
esac
while do done
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo Count is $COUNTER
let COUNTER=COUNTER+1
done
for do done
Arithmetic operations :-
!/bin/bash
echo “Please input your first number”
read first
echo “Now input your second”
read second
echo “Adding first and second numbers gives me” $(($first+$second)) “as result.”
expr and let can be used to calculate in the bash shell also. let x=1+2; echo $x will show the result as 3
Debugging mode in bash :-
!#/bin/bash -x