1 / 20

Shell Scripting

Shell Scripting. Jan Stelovsky. Shell Scripts. structured Unix commands in a file scripts executed using command-lines used to simplify often occuring tasks used to simplify repetitive tasks typical use: manipulating files. Basic Unix Commands. cat

navid
Télécharger la présentation

Shell Scripting

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 Scripting Jan Stelovsky

  2. Shell Scripts • structured Unix commands in a file • scripts executed using command-lines • used to simplify often occuring tasks • used to simplify repetitive tasks • typical use: manipulating files

  3. Basic Unix Commands • cat • concatenate files (often used to display the file) • cp • copy file • mv • move or rename a file • rm -f • remove files – silently! • ls –alg • list files in current directory • cd • change directory

  4. Basic Unix Commands • man • help pages • grep • look for a string • chmod • change permissions (chmod 755 for web pages) • head • show first lines of a file • tail • show last lines of a file

  5. Unix Paths • relative my_file.html • absolute /home/janst/public_html/215/index.html • ~/ • user’s home • wild chard characters * matches any substring (sequence of characters) ? matches any single character

  6. Redirection • redirect std-out to a file > • append std-out to a file >> • reads input (std-in) from a file <

  7. Pipelines • redirect std-out from one command to std-in of another command • Example ls-alg/ | tra-z A-Z • ls –alg / outputs a long-format directory listing of the root (/) directory • pipe feeds the output into second command • tr a-z A-Z takes the listing and converts all lowercase characters to uppercase

  8. Shell Scripts • when executed • can read input (from std-in) • process unix commands) • write output (to std-out) • write errors (to std-err)

  9. Abundance of Shells • Bourne Shell • bin/sh • bin/bash • C shell • bin/csh • Korn shell • bin/ksh • Turbo C shell • bin/tcsh

  10. Writing Scripts • use any text editor • PC: Notepad • Mac: TextMate, TextWrangler • Unix: pico, vi • type shell program text in a file using an editor: • #! /bin/sh# this is a comment# body of programto continue a line append \#this is the continued lineexit 0

  11. Executing Scripts chmod+x scriptfile • makes the script executable • not needed in cygwin scriptfile • executes the script sh -v scriptfile • prints input lines as read sh -x scriptfile • prints commands as executed

  12. Hello World #!/bin/sh # hi# Writes a sample string to std-out echo "Aloha, world from ICS 215!" exit 0 • unix[1]hiAloha, world from ICS 215!unix[2]

  13. Simple Scripts #!/bin/sh # hi_john # Greets John name=John echo "Aloha, $name!" exit 0 • unix[1]hi_johnAloha, John!unix[2]

  14. Simple Scripts cont. #!/bin/sh # remove # Trashes old.temp rmold.temp echo "rmreturned code $?" exit 0 • unix[1]touch old.tempunix[2] removermreturned code 0unix[3] removerm: old.temp: No such file or directoryrm returned code2

  15. How to Make Script Quiet #!/bin/sh # quiet # Trashes old.temp rmold.temp > /dev/null echo "rmreturned code $?" exit 0 • unix[1]touch old.tempunix[2] removermreturned code 0unix[3] removerm returned code2

  16. Passing Parameters #!/bin/sh # parameters echo "there are $# parameters" echo "the parameters are \"$@\"" echo "script name is $0" echo "1st parameter is $1" echo "3rd parameter is $3" exit 0 • unix[1] parameters scripting is coolthere are 3 parametersthe parameters are "scripting is cool"script name is parameters1st parameter is scripting 3rd parameter is cool • unix[2]

  17. Branching, grep #!/bin/sh # find_in_text grep $1 $2 > /dev/null if [ $? -eq0 ] then echo "found" else echo "not found" fi exit 0 • unix[1]find_in_textbook.txt scriptingfoundunix[2] find_in_textbook.txtmahalonot foundunix[3]

  18. True or False; Other Oddities • Note: • ulike in C, true is represented by 0 • false is represented by 1 • also in returned values • Equality -eg • numeric values = • strings • no blanks around = in an assignment • always blanks around = in a comparison • always blanks [ and ] in if condition

  19. Looping #!/bin/sh # add sum=0 for x in $@ do sum='expr$sum + $x done echo "sum is $sum" exit 0 • unix[1] add 2 1 5sum is 8

  20. cygwin • emulates Unix on Windows PC • download from www.cygwin.com • start → All Programs → Cygwin → XTerm • opens unixwindow • command prompt: $ • cygwinexecutables: a.exe

More Related