Shell脚本编程基础知识PPT文档格式.ppt
《Shell脚本编程基础知识PPT文档格式.ppt》由会员分享,可在线阅读,更多相关《Shell脚本编程基础知识PPT文档格式.ppt(74页珍藏版)》请在冰豆网上搜索。
/bin/bash#ThisisthefirstBashshellprogram#ScriptName:
greetings.shechoechoeHello$LOGNAME,cechoitsnicetalkingtoyou.echoYourpresentworkingdirectoryis:
pwd#ShowthenameofpresentdirectoryechoechoeThetimeisdate+%T!
.nByeecho,bashgreetings.sh,chmod+xgreetings.sh./greetings,Shell脚本举例,echo命令,功能说明:
显示文字。
语法:
echo-ne字符串或echo-help-version补充说明:
echo会将输入的字符串送往标准输出。
输出的字符串间以空白字符隔开,并在最后加上换行号。
-n不进行换行-e若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出n换行b空格.,参数:
-n不要在最后自动换行-e若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:
a发出警告声;
b删除前一个字符;
c最后不加上换行符号;
f换行但光标仍旧停留在原来的位置;
n换行且光标移至行首;
r光标移至行首,但不换行;
t插入tab;
v与f相同;
插入字符;
nnn插入nnn(八进制)所代表的ASCII字符;
-help显示帮助-version显示版本信息,#!
/bin/bash#Thisscriptistotesttheusageofread#Scriptname:
ex4read.shecho=examplesfortestingread=echo-eWhatisyourname?
creadnameechoHello$nameechoecho-nWheredoyouwork?
readechoIguess$REPLYkeepsyoubusy!
echoread-pEnteryourjobtitle:
#自动读给REPLYechoIthoughtyoumightbean$REPLY.echoecho=Endofthescript=,Shell脚本举例,read命令,readvariable#读取变量给variablereadxy#可同时读取多个变量read#自动读给REPLYreadp“Pleaseinput:
”#自动读给REPLY,状态变量$?
中保存命令退出状态的值,grep$USER/etc/passwdecho$?
grephello/etc/passwd;
echo$?
条件测试,条件测试可以根据某个特定条件是否满足,来选择执行相应的任务。
Bash中允许测试两种类型的条件:
命令成功或失败,表达式为真或假,任何一种测试中,都要有退出状态(返回值),退出状态为0表示命令成功或表达式为真,非0则表示命令失败或表达式为假。
内置测试命令test,通常用test命令来测试表达式的值,x=5;
y=10test$x-gt$yecho$?
test命令可以用方括号来代替,x=5;
y=10$x-gt$yecho$?
表达式测试包括字符串测试、整数测试和文件测试。
测试表达式的值,方括号前后要留空格!
name=Tom$name=Tt?
2.x版本以上的Bash中可以用双方括号来测试表达式的值,此时可以使用通配符进行模式匹配。
测试表达式的值,$name=Tt?
字符串测试,name=Tom;
-z$name;
操作符两边必须留空格!
字符串测试,name2=Andy;
$name=$name2;
整数测试,即比较大小,x=1;
$x-eq1;
x=a;
整数测试,操作符两边必须留空格!
X,整数测试也可以使用let命令或双圆括号,x=1;
let$x=1;
x=1;
($x+1=2);
只能用于整数测试!
整数测试,相应的操作符为:
=、!
=、=、=,例:
两种测试方法的区别,使用的操作符不同let和双圆括号中可以使用算术表达式,而中括号不能let和双圆括号中,操作符两边可以不留空格,逻辑测试,x=1;
name=Tom;
$x-eq1an$name;
逻辑测试,注:
不能随便添加括号,($x-eq1)a(n$name);
X,x=1;
$x-eq1echo$?
可以使用模式的逻辑测试,逻辑测试,文件测试:
文件是否存在,文件属性,访问权限等。
常见的文件测试操作符,更多文件测试符参见test的在线帮助,mantest,文件测试,检查空值,$name=,!
$name,X$name!
=X,检查空值,语法结构,ifexpr1#如果expr1为真(返回值为0)then#那么commands1#执行语句块commands1elifexpr2#若expr1不真,而expr2为真then#那么commands2#执行语句块commands2.#可以有多个elif语句else#else最多只能有一个commands4#执行语句块commands4fi#if语句必须以单词fi终止,if条件语句,commands为可执行语句块,如果为空,需使用shell提供的空命令“:
”,即冒号。
该命令不做任何事情,只返回一个退出状态0,if语句可以嵌套使用,ex4if.sh,chkperm.sh,chkperm2.sh,name_grep,tellme,tellme2,idcheck.sh,几点说明,elif可以有任意多个(0个或多个),else最多只能有一个(0个或1个),if语句必须以fi表示结束,expr通常为条件测试表达式;
也可以是多个命令,以最后一个命令的退出状态为条件值。
ex4if.sh,#!
/bin/bash#scriptname:
ex4if.sh#echo-nPleaseinputx,y:
readxyechox=$x,y=$yif(xy);
thenechoxislargerthanyelif(x=y);
thenechoxisequaltoyelseechoxislessthanyfi,chkperm.sh,#!
/bin/bash#Usingtheoldstyletestcommand:
#filename:
perm_check.sh#file=testingif-d$filethenecho$fileisadirectoryelif-f$filethenif-r$file-a-w$file-a-x$filethen#nestedifcommandechoYouhaveread,write,andexecutepermissionon$file.fielseecho$fileisneitherafilenoradirectory.fi,chkperm2.sh,#!
/bin/bash#Usingthenewstyletestcommand:
perm_check2.sh#file=./testingif-d$filethenecho$fileisadirectoryelif-f$filethenif-r$file&
-w$file&
-x$filethen#nestedifcommandechoYouhaveread,write,andexecutepermissionon$file.fielseecho$fileisneitherafilenoradirectory.fi,name_grep,#!
/bin/bash#filename:
name_grep#name=Tomifgrep$name/etc/passwd&
/dev/nullthen:
elseecho$namenotfoundin/etc/passwdexit2fi,tellme,#!
/bin/bashecho-nHowoldareyou?
readageif$age-lt0-o$age-gt120thenechoWelcometoourplanet!
exit1fiif$age-ge0-a$age-le12thenechoChildrenistheflowersofthecountryelif$age-gt12-a$age-le19thenechoRebelwithoutacauseelif$age-gt19-a$age-le29thenechoYougottheworldbythetail!
elif$age-ge30-a$age-le39thenechoThirtysomething.elseechoSorryIaskedfi,tellme2,#!
readageif(age120)thenechoWelcometoourplanet!
exit1fiif(age=0&
age=13&
age=19&
age=30&
age=39)thenechoThirtysomething.elseechoSorryIaskedfi,idcheck.sh,#!
/bin/bash#Scriptname:
idcheck.sh#purpose:
checkuseridtoseeifuserisroot.#Onlyroothasauidof0.#Formatforidoutput:
uid=501(tt)gid=501(tt)groups=501(tt)#rootsuid=0:
uid=0(root)gid=0(root)groups=0(root)#id=id|awk-F=(print$2#getuseridechoyouruseridis:
$idif(id=0)#$id-eq0thenechoyouaresuperuser.elseechoyouarenotsuperus