UNIX Shell编程.ppt
2010 Hewlett-Packard Development Company,L.P.The information contained herein is subject to change without notice UNIX Shell-Scripting BasicsAgendalWhat is a shell?A shell script?lIntroduction to bashlRunning CommandslApplied Shell ProgrammingWhat is a shell?%What is a shell?/bin/bashWhat is a shell?#!/bin/bashWhat is a shell?INPUTshellOUTPUTERRORWhat is a shell?lAny ProgramlBut there are a few popular shellsBourne Shellsl/bin/shl/bin/bash“Bourne-Again Shell”Steve BourneOther Common ShellslC Shell(/bin/csh)lTurbo C Shell(/bin/tcsh)lKorn Shell(/bin/ksh)An aside:What do I mean by/bin?lC Shell(/bin/csh)lTurbo C Shell(/bin/tcsh)lKorn Shell(/bin/ksh)An aside:What do I mean by/bin?l/bin,/usr/bin,/usr/local/binl/sbin,/usr/sbin,/usr/local/sbinl/tmpl/devl/home/borwicjhWhat is a Shell Script?lA Text FilelWith InstructionslExecutableWhat is a Shell Script?%cat hello.sh%cat hello.shhello.sh MY_PROGRAM /tmp/myfilelcat /tmp/myfilelcat 2/tmp/myerrlcat /tmp/myinputlcat /tmp/x 2&1INPUTenvOUTPUTERROR012What is a Shell Script?How To Run%cat hello.sh hello.sh hello.sh hello.sh test.sh _TEST_exit 3_TEST_%chmod+x test.sh%./test.sh%echo$?3Logic:test%test 1-lt 10%echo$?0%test 1=10%echo$?1Logic:testltestl 1 lt 10 l “this string”=“this”l()(1 10)Logic:testl-f/etc/passwd l!f/etc/passwd l-f/etc/passwd a f/etc/shadow l-f/etc/passwd o f/etc/shadow An aside:$()for Math%echo$(1+2)3%echo$(2*3)6%echo$(1/3)0Logic:ifif somethingthen :#“elif”a contraction of“else if”:elif something-elsethen :elsethen :fiLogic:ifif$USER eq“borwicjh”then :#“elif”a contraction of“else if”:elif ls/etc/oratabthen :elsethen :fiLogic:if#see if a file existsif -e/etc/passwd then echo“/etc/passwd exists”else echo“/etc/passwd not found!”fiLogic:forfor i in 1 2 3do echo$idoneLogic:forfor i in/*do echo“Listing$i:”ls-l$i readdoneLogic:forfor i in/*do echo“Listing$i:”ls-l$i readdoneLogic:forfor i in/*do echo“Listing$i:”ls-l$i readdoneLogic:C-style forfor(expr1 ;expr2 ;expr3 )do listdoneLogic:C-style forLIMIT=10for(a=1 ;a/path/to/my/passwords _PW_FTP_USER=“sct”_PW_%echo$FTP_USER%./path/to/my/passwords%echo$FTP_USERsct%Variable Manipulation%FILEPATH=/path/to/my/output.lis%echo$FILEPATH/path/to/my/output.lis%echo$FILEPATH%.lis/path/to/my/output%echo$FILEPATH#*/path/to/my/output.lis%echo$FILEPATH#*/output.lisIt takes a long time to become a bash guru 2010 Hewlett-Packard Development Company,L.P.The information contained herein is subject to change without notice Running ProgramsReasons for Running ProgramslCheck Return Code$?lGet Job OutputOUTPUT=echo“Hello”OUTPUT=$(echo“Hello”)lSend Output SomewhereRedirection:PipesPipeslLots of Little Toolsecho“Hello”|wc-cINPUTechoOUTPUTERROR012INPUTwcOUTPUTERROR012A Pipe!Email Notification%echo“Message”|mail s“Heres your message”borwicjhwfu.eduDates%DATESTRING=date+%Y%m%d%echo$DATESTRING20060125%man dateFTP the Hard Wayftp n u server.wfu.edu outfilesqlplusJOB=“ZZZTEST”PARAMS=“ZZZTEST_PARAMS”PARAMS_USER=“BORWICJH”sqlplus$BANNER_USER/$BANNER_PW test.sh _TEST_echo“Your name is$1$2”_TEST_%chmod+x test.sh%./test.sh John Borwick ignore-thisYour name is John BorwickScheduling Jobs%crontab-l0 0*daily-midnight-job.sh0*hourly-job.sh*every-minute.sh0 1*0 1AM-on-sunday.sh%EDITOR=vi crontab e%man 5 crontabOther Questions?lShells and Shell ScriptslbashlRunning Commandslbash and Banner in Practice