html5-img
1 / 23

UNIX Shell Script (1)

UNIX Shell Script (1). Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn. What is a shell script?. A series of OS commands for execution Stored in a text file. #!/bin/sh rm -f /tmp/listing.tmp > /dev/null 2>&1

fergus
Download Presentation

UNIX Shell Script (1)

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. UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

  2. What is a shell script? • 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. How to be helped • Man pages hoai@moon:~> man bash ... hoai@moon:~> man sh ...

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

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

  8. Example (argument) (2) hoai@moon:~> my_script.sh hoai 1 university Script name is [my_script.sh] First argument is [hoai] Second argument is [1] This process ID is [5401] This argument count is [3] All arguments [hoai 1 university]

  9. Filename metacharacters

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

  11. Example (metacharacters) 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

  12. Quoting

  13. 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/hoai $ echo "The value of \$x is $x" The value of $x is 100

  14. Variables (1)

  15. Variables (2)

  16. Command forms

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

  18. Simple commands

  19. Example (script) (1) #!/bin/bash alphabet="a b c d e" # Initialise a string count=0 # Initialise 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

  20. Example (script) (2) alphabet="a b c d e" # Initialise a string count=0 # Initialise 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

  21. Homeworks (1) • Write a script for C compiler • Objective: use gcc by default, if gcc is not availablle, find another compiler (ending with cc) instead. • Write a script to convert filenames to lowercase letters • Input: a directory path string • Output: all filenames in the directory in lowercase

  22. Homeworks (2) • Write a script to warn users using too much space • Objective: find all users of the system (/etc/passwd, cut), check if someone uses > 1GB, mail him a warning message

  23. Conditional structure • Loop structure • Function • File input/output • Array are NEXT

More Related