您好,欢迎访问三七文档
BourneShellshellAltmayer.bbs@altmayer.dhs.orgjavaleeLINUXhbwork@dlut.edu.cn,April1999.URL::------------------------------------------------------------------------------BourneShellBourneShellshell.shell.shell(shell).shellscript.shell1.shellStephenBourneBellmanshUNIX(1)shell/etc/passwd$/etc/profile$HOME/.profile(2)shell-nshellscriptshellscript-x-aTagallvariablesforexport-cstringstrings-e-fshell1-hlocateandrememberfunctionsasdefind-i-k-r-s-t(shellexits)-u-vverbose,shell-e-i.(3)shell(RestirctedShell)sh-r/bin/rshcd,PATH,rshsh,rshvimoreshell,rsh(4)setshell$setshell-+UNIXa,e,f,h,k,n,u,vx/set-xv;set-tuecho$-shell(5).profilePATH=$PATH:/usr/loacl/bin;exportPATH(6)shellCDPATHcdHOME/etc/passwdIFSInternalFieldSeparator,tab2MAIL/var/mail/$USERNAMEmailPATHPS1PS2($)()TERMvt100,ansi,vt200,xterm$PS1=test:;exportPS1test:PS1=\$;exportPS1$echo$MAIL/var/mail/username(7)$shell$var|#&*0(DOS.)$-setshell$!$#shellscript$*shellscript$@$?$0shell$n(n:1-)$$(ProcessIdentifierNumber,PID)filefile`command`filename=`basename/usr/local/bin/tcsh`fiileappend$echo$HOME$PATH/home/hbwork/opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:$echo'$HOME$PATH'$HOME$PATH$echo\$HOME$PATH$HOME/opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbwork/bin$dir=ls3$$dir$aliasdirls$dirlsfilelistlsfilelistwc-lfilelistwc-lfilelistsleep5;echo5secondsreaches;ls-lpsax|egrepinetdfind/-namecore-execrm{}\;&filename=`date+%Y%m%d`.log2.shell$HOME,cd$HOME,BourneShell..shellscript..(shell)(1)$COUNT=1$NAME=HeBinwuUNIXshell$$echo$HOME/home/hbwork$WEEK=Satur$echoTodayis$WEEKdayTodayis$echoTodayis${WEEK}dayTodayisSaturdayShell(LinuxShell/bash!)$X=$YY=y$echo$X4y$Z=zY=$Z$echo$Y$unset$Z=hello$echo$Zhello$unsetZ$echo$Z$BourneShell.BourneShell${variable:-defaultvalue}$echoHello$UNAMEHello$echoHello${UNAME:-there}Hellothere$echo$UNAME#$UNAME=hbwork$echoHello${UNAME:-there}Hellohbwork$.${variable:=value}$echoHello$UNAMEHello$echoHello${UNAME:=there}Hellothere5$echo$UNAME#there$.$USERDIR=${$MYDIR:-`pwd`}.${variable:+value}.${variable:?value}$UNAME=$echo${UNAME:?UNAMEhasnotbeenset}UNAME:UNAMEhasnotbeenset$echo${UNAME:?}UNAME:parameternullornotset(2)(Shell)shellscript$1..$9$0.readonlyvariable.exportshellshellshellsubshell.BourneShellshellexportsubshellPS1exportshell$PS1=`hostname`$peony$sh$echo$PS1$-$exitpeony$exportPS1peony$shpeony$echo$PS1peony$-peony$3.ShellScriptUNIX6(1)Shell$ls-R/|grepmyname|more$cd/usr/yourname;ls*|cpio-o/dev/rmt/0h$catbackup.shcd/home/hbworkls*|cpio-o/dev/rmt/0h^D$shbackup.sh$chmod+xbackup.sh$./backup.shshellscript(2)shellshellshellscript.shellbackup.shcd$WORKDIRls*|cpio-o/dev/rmt/0h.Shell#.shellexprexprintegeroperatorintegeroperator+-*/%,*\,$expr4\*520$int=`expr5+7`$echo$int127(3)Shell,(read)restoreall.shbackup.sh$catrestoreall.shcd$WORKDIRcpio-i/dev/rmt/0h^Drestore1.sh:#restore1--programtorestoreasinglefilecd$WORKDIRcpio-i$i/dev/rmt/0h$restore1file1restoreany:#restoreany--programtorestoreasinglefilecd$WORKDIRcpio-i$*/dev/rmt/0h$restoreanyfile1file2file3(4).if-then,:ifcommand_1thencommand_2command_3ficommand_4if-then$?,command_1command_2command_3,command_4.unload:,cd$1#!ls-a|cpio-o/dev/rmt/0hrm-rf*:8#,ifls-a|cpio-o/dev/rmt/0hthenrm-rf*fi.if-then-elseifcommand_1thencommand_2elsecommand_3fi:shell,,..test:testconditionstest:a.b.c.,d.,and/or,a.:shellstr1=str2,str1!=str2-nstringstring()-zstringstringstringstring:$str1=abcd#$test$str1=abcd$echo$?0$str1=abcd$test$str1=abcd$echo$?19Note:test,,shell,shell.:$str1=$test$str1$echo$?1$test$str1$echo$?0$test-n$str1test:argumentexpected$test-n$str1$echo$?0$b.:testexpr,,expr,test.---------------------------------------int1-eqint2?int1-neint2?int1-gtint2int1int2?int1-geint2int1=int2?int1-ltint2int1int2?int1-leint2int1=int2?:$int1=1234$int2=01234$test$int1-eq$int2$echo$?0c.:-rfilenamefilename?10-wfilenamefilename?-xfilenamefilename?-ffilenamefilename?-dfilenamefilename?-cfilenamefilename?-bfilenamefilename?-sfilenamefilename?-tfnumbfnumb(1)?d.,!:$cat/dev/nullempty$test-rempty$echo$?0$test-sempty1$test!-sempty$echo$?0e.-aAnd-oOr:$test-rempty-a-sempty$echo$?1f.testtestshell,shell,BourneShelltest:test:$int1=4$[$int1-gt2]$echo$?0:unload,test#!/bin/sh11#unload-programtobackupandremovefiles#syntax:unloaddirectory#checkargumentsif[$#-ne1]thenechousage:$0directoryexit1fi#checkforvaliddirectorynameif[!-d$1]thenecho$1isnotadirectoryexit2ficd$1ls-a|cpio-o/dev/rmt/0hif[$?-eq0]thenrm-rf*elseechoAproblemhasoccuredincreatingbackupechoThedirectorywillnotbeereasedechoPleasecheckthebackupdeviceexit3fi#endofunloadexit,exit:,g.ifelififcommandthencommandelseifcommandthen12commandelseifcommandthencommandfififi:elififcommandthencommandelifcommandthencommandelifcommandthencommandfielifif,,./*****************/------------------------------------------------------------------------------:BourneShellShell(2)h.readreadvar1var2...varnread$readvar1var2var3Hellomyfriends13$echo$var1$var2$var3Hellomyfriends$echo$var1Hello$readvar1var2var3Hellomydearfriends$echo$var3dearfriends-
本文标题:shell编程实例
链接地址:https://www.777doc.com/doc-1538489 .html