370 likes | 847 Views
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
E N D
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 • 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
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
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
Redirection • redirect std-out to a file > • append std-out to a file >> • reads input (std-in) from a file <
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
Shell Scripts • when executed • can read input (from std-in) • process unix commands) • write output (to std-out) • write errors (to std-err)
Abundance of Shells • Bourne Shell • bin/sh • bin/bash • C shell • bin/csh • Korn shell • bin/ksh • Turbo C shell • bin/tcsh
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
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
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]
Simple Scripts #!/bin/sh # hi_john # Greets John name=John echo "Aloha, $name!" exit 0 • unix[1]hi_johnAloha, John!unix[2]
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
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
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]
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]
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
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
cygwin • emulates Unix on Windows PC • download from www.cygwin.com • start → All Programs → Cygwin → XTerm • opens unixwindow • command prompt: $ • cygwinexecutables: a.exe