System Programming Session 6 Shell Scripting
Programming C Programming vs Shell Programming
C vs Shell Programming Compilation/Direct execution C Requires compilation while shell script can be directly run Programming constructs/facilities Common constructs like variables, if conditions, loops, arithmetic Program complexities Size of program, number of files, % code in total application code Usage Installations, security checks, complex algorithms
Different Shells Bourne Shell Original shell written by Steve Bourne for Unix /bin/sh Bash shell Bourne Again shell! Superset of Bourne GNU Shell Most commonly used on Linux /bin/bash Other shells - C Shell, K Shell, t cshell
First Shell Program! shell to use Comments #!/bin/bash # My first attempt to write shell program # It lists all files in current directory ls Code line Shell script execution starts from first line unlike C where it starts from main function
Quick look at real script on Linux #!/bin/bash # # httpd Startup script for the Apache HTTP Server # Source function library.. /etc/rc.d/init.d/functions # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/sbin/apachectl httpd=${httpd-/usr/sbin/httpd} prog=httpd pidfile=${pidfile-/var/run/httpd/httpd.pid} /etc/init.d/httpd # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; esac
Shell wildcards review All files * Example #ls code* for all files starting with code Specific character? Example #ls code?.c Any character from range Example #ls code[12].c No character from range [ijk] [!ijk] Example #ls code[!12].c Home directory name ~ Use backslash (\) to avoid replacement Example #ls code\?.c
Shell wildcards test! $ls *[i-n] $ls pr?.c $ls?i* $ls *
I/O Redirection Standard Input redirection < Standard output redirection > Standard output redirection upend >> Redirection in commands What will be outcome of this command? #cat > list.txt
Passing Arguments How do we enhance our first program to list files from any directory? Command line arguments can be passed to shell script Each argument can be accessed in script as $1,$2 $# provides number of arguments given while invoking script $* provides all arguments in one string
Second Shell Program Let us enhance our script to take directory name as argument for listing file Comments #!/bin/bash #Prints all files in specified directory Accessing argument Echo Listing of files ls $1/* exit(0) Exit statement
Variables You can define and use variables in shell program Paring of var-name>=<val> sets the variable Example OS=Linux, TODAY=26NOV Our real script example of /etc/init.d/httpd script had apachectl=/usr/sbin/apachectl You can derive values for variable using command output TODAY=`date` String quoted in tilda is interpreted as command You can set values from command line argument DIR1=$1, DIR2=$2 Variable name can t be number or start with number 10page=book
Shell Program with variables Setting value Accessing value #!/bin/bash #Prints all files in specified directories DIR1=$1 DIR2=$2 echo "Listing from $DIR1" ls $DIR1/* echo "Listing from $DIR2" ls $DIR2/*
System Variables What is differences between following methods of script execution? $sample1.sh $./sample1.sh
System Variables System defined variables which can be used directly on command line or in script Name PATH HOME LOGNAME Meaning Directories searched for executable /bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin: Home directory of user invoking script Login name of user invoking the script OSTYPE Type of Unix e.g. linux-gnu PWD Current director
System Variables $sample1.sh Searched from PATH variable value $./sample1.sh Searched in current working directory Which directory is searched Here? $../sample1.sh
Reading Arguments How do we enhance our script to prompt user to specify directory to list files? Use echo command to prompt message Use read statement to read input Store input in variables Access variables as discussed earlier
Interactive Shell Program read value use value #!/bin/bash #Prints all files in specified directories Echo Enter first directory name read DIR1 Echo Enter second directory name read DIR2 echo "Listing from $DIR1" ls $DIR1/* echo "Listing from $DIR2" ls $DIR2/*
Arithmetic Operations expr command is used to perform operation Example: expr 4 + 5 Spaces are important Supported Operations Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) expr 4.5 + 2 Only numerical numbers allowed
Shell Program with arithmetic #!/bin/bash Echo Enter first directory name read DIR1 Echo Enter second directory name read DIR2 count1=`ls $DIR1/* wc l` count2=`ls $DIR2/* wc l` total=`expr $count1 + $count2` echo total file count = $total