1 / 19

Shell Script Examples

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.

gram
Download Presentation

Shell Script Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Script Examples

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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 [$@]"

  7. 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]

  8. Filename meta characters

  9. Simple regular expressions (Korn shell) • Pattern = sequence of patterns separated by “|”

  10. 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

  11. Quoting

  12. 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

  13. Variables (1)

  14. Variables (2)

  15. Command forms

  16. 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

  17. Simple commands

  18. 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

  19. 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

More Related