_TOP_MENU

Oct 14, 2014

Shell Script Tutorial



1Q. What is Shell Script ?

Shells are interactive, it means it accepts commands from you via keyboard and execute them.
But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is known as shell script.

2Q. What is the use of shell scripts ?
Scripting basically to write to save lot of users time to edit files/run program manually. it's like automation where you just need to fire a command and with in that command, hundred of operations being performed by scripts. 

Few basic steps to get familiar with shell scripts - 

create a file , called test.sh 

echo "$HOME"
echo "$USERNAME"
echo "$PWD" 


3. Shell Arithmetic 

Syntax:
expr <op1>  <math-operator>  <op2>

Examples: 
$ expr 2 + 5
$ expr 3 - 2
$ expr 20 / 5
$ expr 20 % 7
$ expr 50 \* 3
$ echo `expr 10 + 4`

Note:expr 20 %5 - Remainder read as 20 mod 5 and remainder is 0.
expr 50 \* 3 - Multiplication use \* and not * since its wild card.
For the last statement not the following points

For echo, if used double quotes then whatever in there will be printed , but if used single quotes then value will be calculated and printed.

$ echo "expr 2 + 3" # It will print expr 2 + 3
$ echo 'expr 1 + 3' # It will print expr 1 + 3


4.  Type exit if you want to exit from the script (may be after matching some conditions)

5.  Type  'date' , if you want to print current date on terminal, you can use this in random variable generation. 

6. If you want to pass multiple argument from command line, you can use $1, $2, $3, ..  
For Example  -

myrun.sh 
In the script - 

vlog $1.v $2.v $3.v   
echo " file name $1 , $2 , $3 " 


when I run this scripts, I will give - 
./myrun.sh  cnt1 cnt2 cnt3 


7. Shell script support all unix command, you can move files, copy files fron one location to other location , browse directories  , create dir , etc 

8.  Below is table giving information on running processes -  


For this purposeUse this CommandExamples*
To see currently running process ps$ ps
To stop any process by PID i.e. to kill processkill    {PID}$ kill  1012
To stop processes by name i.e. to kill processkillall   {Process-name}$ killall httpd
To get information about all running processps -ag$ ps -ag
To stop all process except your shellkill 0$ kill 0
For background processing (With &, use to put particular command and program in background)linux-command  &$ ls / -R | wc -l &
To display the owner of the processes along with the processes  ps aux$ ps aux
To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command ps ax | grep  process-U-want-to see
For e.g. you want to see whether Apache web server process is running or not then give command$ ps ax | grep httpd
To see currently running processes and other information like memory and CPU usage with real time updates.top
See the output of top command.

$ top


Note 
that to exit from top command press q.
To display a tree of processespstree$ pstree


9.  If .... Else   
Below is the format to use if..else conditional statement in script - 

if  [ "$1" == "$var" ];  then 
  echo "condition is matching" 
else 
  echo "condition not matching" 
fi 


Note semicolon after "]" , and then , If you do not write correct syntax , shell will give error. 

for else if , use elif 

if [  ] ; then 
  echo " ... " 
elif [ ] ; then 
 echo " " 
else 
 echo " .. " 
fi 

check below example - 
#!/bin/sh
# Script to test if..elif...else
#
if [ $1 -gt 0 ]; then
  echo "$1 is positive"
elif [ $1 -lt 0 ]
then
  echo "$1 is negative"
elif [ $1 -eq 0 ]
then
  echo "$1 is zero"
else
  echo "Opps! $1 is not number, give number"
fi

10. For ... Loop 

for i in 1 2 3 4 5
do
echo "Welcome $i times"
done


#!/bin/sh
#
#Script to test for loop
#
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i = `expr $i \* $n`"
done

Other way of writing for loop - 

for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

11. While ... Loop 

          while [ condition ]
           do
                 command1
                 command2
                 command3
                 ..
                 ....
            done

All above shell scripting was basic level of shell scripting , there is advanced level of shell scripting which includes below topics - 

  • Functions
  • User interface
  • Conditional execution
  • File Descriptors
  • traps
  • Multiple command line args handling etc
There will be separate blog on shell advanced topics. 

Thanks for reading my blog , any question/comments are most welcome.


Title of the document List of Semiconductor Companies

No comments:

Post a Comment