210 likes | 470 Views
Shell Script Examples. A shell script is…. A series of OS commands for execution Stored in a text file. #!/bin/sh rm -f /tmp/listing.tmp > /dev/null 2>&1 touch /tmp/listing.tmp ls -l [a-z]*.doc | sort > /tmp/listing.tmp lpr -Ppostscript_1 /tmp/listing.tmp rm -f /tmp/listing.tmp.
E N D
A shell script is… • A series of OS commands for execution • Stored in a text file #!/bin/sh rm -f /tmp/listing.tmp > /dev/null 2>&1 touch /tmp/listing.tmp ls -l [a-z]*.doc | sort > /tmp/listing.tmp lpr -Ppostscript_1 /tmp/listing.tmp rm -f /tmp/listing.tmp
Shell in use (sh, bash, csh) Command Comment Components of a script #!/bin/sh rm -f /tmp/listing.tmp > /dev/null 2>&1 touch /tmp/listing.tmp # This is a comment ls -l [a-z]*.doc | sort > /tmp/listing.tmp lpr -Ppostscript_1 /tmp/listing.tmp rm -f /tmp/listing.tmp
How to invoke a script • Correct way $ /bin/bash my_script arg_1 arg_2 • Simple way $ my_script arg_1 arg_2 • The first line specifying the shell must be provided in simple way
Definitions • Blank = chunk of tab or space • Name = sequence of ASCII letters, digits, underscores, beginning with a letter or underscore • Argument = string supplied on command-line
Example (argument) (1) #!/bin/sh ############################## echo "Script name is [$0]" echo "First argument is [$1]" echo "Second argument is [$2]" echo "This process ID is [$$]" echo "This argument count is [$#]" echo "All arguments [$@]"
Example (argument) (2) :~> my_script.sh bhecker 1 university Script name is [my_script.sh] First argument is [bhecker] Second argument is [1] This process ID is [5401] This argument count is [3] All arguments [bhecker 1 university]
Simple regular expressions (Korn shell) • Pattern = sequence of patterns separated by “|”
Example (meta characters) List files having prefix new $ ls new* Cat files having prefix ch and one more letter $ cat ch? Vi files starting by letters from D to E $ vi [D-R]* Print files not *.o and core (Korn shell) $ pr !(*.o|core) | lp
Example (quoting) $ echo 'my class is "unix and tools"' My class is "unix and tools" $ echo "Well, isn't that \"good\" ?" Well, isn't that "good" ? $ echo "You have `ls | wc –l` files in `pwd`" You have 34 files in /home/bhecker $ echo "The value of \$x is $x" The value of $x is 100
Example (command forms) $ nroff file > file.txt & Format in the background $ cd; ls Execute sequentially $ (date; who; pwd) > logfile All output is redirected $ sort file | pr -3 | lp Sort file, page output, then print $ vi 'grep -l ifdef *.c' Edit files found by grep $ grep XX file && lp file Print file if it contains the pattern; $ grep XX file || echo "XX not found" otherwise, echo an error message
Example (script) (1) #!/bin/bash alphabet="a b c d e" # Initialize a string count=0 # Initialize a counter for letter in $alphabet # Set up a loop control do # Begin the loop count=‘expr $count + 1’ # Increment the counter # Display the result echo "Letter $count is [$letter]" done
Example (script) (2) alphabet="a b c d e" # Initialize a string count=0 # Initialize a counter while [ $count -lt 5 ] # Set up a loop control do # Begin the loop count=‘expr $count + 1’ # Increment the counter # Position of next letter position=‘bc $count + $count – 1’ letter=‘echo "$alphabet" | cut -c$position-$position’ # Get next letter # Display the result echo "Letter $count is [$letter]" done