Shell_Scripting
Shell_Scripting
IN THE NAME OF
ALLAH
WHO IS THE CREATOR OF OUR BELOVED
PROPHET(P.B.U.H)
HAZRAT MUHAMMAD
Shell Scripting
use OS
• How to know what shell you use r
echo $SHELL
use
r
Department of Criminology and Forensic Sciences
UNIX Shells
sh Bourne Shell (Original Shell) (Steven Bourne of AT&T)
bash Bourne Again Shell (GNU Improved Bourne Shell)
csh C-Shell (C-like Syntax)(Bill Joy of Univ. of California)
ksh Korn-Shell (Bourne+some C-shell)(David Korn of AT&T)
tcsh Turbo C-Shell (More User Friendly C-Shell).
To check shell:
$ echo $SHELL (shell is a pre-defined variable)
To switch shell:
$ exec shellname (e.g., $ exec bash or simply type $ bash)
You can switch from one shell to another by just typing the name of the
shell. exit return you back to previous shell.
done
/bin/rm ./junk ./head ./file_list
$ echo $SHELL
To see a list of your environment variables:
$ printenv
or:
$ printenv | more
• Example:
#!/bin/sh
a=(1 2 3)
echo ${a[*]}
echo ${a[0]}
Results: 1 2 3
1
Example:
Invoke : ./myscript one two buckle my shoe
During the execution of myscript variables $1 $2 $3 $4 and $5 will contain the
values one, two, buckle, my, shoe respectively.
Department of Criminology and Forensic Sciences
Variables
• vi myinputs.sh
#! /bin/sh
echo Total number of inputs: $#
echo First input: $1
echo Second input: $2
#!/bin/sh
count=5
count=`expr $count + 1 `
echo $count
** power of
!,~ logical/bitwise negation
&,| bitwise AND, OR
&& || logical AND, OR
# Note: = or == will both work in the test but == is better for readability.
Department of Criminology and Forensic Sciences
Tests
String and numeric comparisons used with test or [[ ]] which is an alias for test and
also [ ] which is another acceptable syntax
EXAMPLE:
while test "$i" -gt 0 # can also be while [ $i > 0 ]
do
i=`expr $i - 1`
done
Output:
Hello Bob
Hello Susan
Hello Joe
Hello Gerry
SYNTAX:
functionname()
{
block of commands
}
#!/bin/sh
sum() {
x=`expr $1 + $2`
echo $x
}
sum 5 3
echo "The sum of 4 and 7 is `sum 4 7`"
• Cons
• Performance slowdown
• Accurate scientific computing
input=./$each_file".com"
cat head > $input
cat junk >> $input
echo ' ' >> $input
done
/bin/rm ./junk ./head ./file_list
Department of Criminology and Forensic Sciences
LSF Job Submission
$ vi submission.sh
#!/bin/sh -f
#BSUB -q week
#BSUB -n 4
#BSUB -o output
#BSUB -J job_type
#BSUB -R “RH5 span[ptile=4]”
#BSUB -a mpichp4
mpirun.lsf ./executable.exe
exit
$chmod +x submission.sh
$bsub < submission.sh
http://ebooks.ebookmall.com/title/linux-shell-scripting-with-bash-burtch-ebooks.ht
m
• Shell Script in C Shell
http://www.grymoire.com/Unix/CshTop10.txt
• Linux Shell Scripting Tutorial
http://www.freeos.com/guides/lsst/
• Bash Shell Programming in Linux
http://www.arachnoid.com/linux/shell_programming.html
• Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/
• Unix Shell Programming
http://ebooks.ebookmall.com/title/unix-shell-programming-kochan-wood-ebooks.ht
m