ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:19.44KB ,
资源ID:7953568      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7953568.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(shell编程if条件判断.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

shell编程if条件判断.docx

1、shell编程if条件判断shell编程if条件判断if 语句格式if条件thenCommandelseCommandfi别忘了这个结尾If语句忘了结尾fitest.sh: line 14: syntax error:unexpected end of fiif 的三种条件表达式ifcommandthenif函数then命令执行成功,等于返回0 (比如grep ,找到匹配)执行失败,返回非0 (grep,没找到匹配)if expression_r_r_rthen表达式结果为真,则返回0,if把0值引向thenif test expression_r_r_rthen表达式结果为假,则返回非0,i

2、f把非0值引向then &快捷if -f /etc/shadow & echo This computer uses shadow passwors&可以理解为then如果左边的表达式为真则执行右边的语句shell的if与c语言if的功能上的区别shell ifc语言if0为真,走then正好相反,非0走then不支持整数变量直接if必须:if i ne 0 但支持字符串变量直接ifif str 如果字符串非0支持变量直接ifif (i )=以command作为if 条件=以多条command或者函数作为if 条件echo n “input:”read userif多条指令,这些命令之间相当于

3、“and”(与)grep $user /etc/passwd /tmp/nullwho -u | grep $userthen上边的指令都执行成功,返回值$?为0,0为真,运行thenecho $user has loggedelse指令执行失败,$?为1,运行else echo $user has not loggedfi# sh test.shinput : macgmacgpts/0May 15 15:55.2075 (192.168.1.100)macg has logged# sh test.shinput : dddddd has not logged以函数作为if条件(函数就相当

4、于command,函数的优点是其return值可以自定义)if以函数作为if条件,getynthen函数reture值0为真,走thenecho your answer is yeselse函数return值非0为假,走elseecho your anser is nofiif command等价于 command+if $?$ vi testsh.sh#!/bin/shifcat 111-tmp.txt | grep ting1thenecho foundelseecho no foundfi$ vi testsh.sh#!/bin/shcat 111-tmp.txt | grep ting

5、1if $? -eq 0 thenecho $?echo foundelseecho $?echo no foundfi$ sh testsh.shno found$ sh testsh.sh1no found$ vi 111-tmp.txtthat is 222filethisting1 is 111file$ sh testsh.shthisting1 is 111filefound$ vi 111-tmp.txtthat is 222filethisting1 is 111file$ sh testsh.shthisting1 is 111file0found=以条件表达式作为 if条件

6、=传统if 从句子以条件表达式作为 if条件if 条件表达式 thencommandcommandcommandelsecommandcommandfi条件表达式 文件表达式if -ffile 如果文件存在if -d .如果目录存在if -s file如果文件存在且非空if -r file如果文件存在且可读if -w file 如果文件存在且可写if -x file 如果文件存在且可执行 整数变量表达式if int1 -eq int2 如果int1等于int2if int1 -ne int2 如果不等于 if int1 -ge int2 如果=if int1 -gt int2 如果if int

7、1 -le int2 如果=if int1 -lt int2 如果和 ,会被当作尖括号,只有-ge,-gt,-le,ltmacgmachome $ vi test.shecho input a:read aif $a -ge 100 ; thenecho 3bitelseecho 2bitfimacgmachome $ sh test.shinput a:1233bitmacgmachome $ sh test.shinput a:202bit整数操作符号-ge,-gt,-le,-lt, 别忘了加-iftest $age100 ; thenmacgmachome $ sh test.shtes

8、t.sh: line 4: test: ge: binary operator expectediftest $a -ge 100 ; thenmacgmachome $ sh test.shinput a:1233bit=逻辑表达式=逻辑非 !条件表达式的相反if ! 表达式 if ! -d $num 如果不存在目录$num逻辑与 a条件表达式的并列if 表达式1a表达式2 逻辑或 -o条件表达式的或if 表达式1o 表达式2 逻辑表达式 表达式与前面的=!= -d f x -ne -eq -lt等合用 逻辑符号就正常的接其他表达式,没有任何括号(),就是并列if -z $JHHOME -a

9、 -d $HOME/$num 注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了最常见的赋值形式,赋值前对=两边的变量都进行评测左边测变量是否为空,右边测目录(值)是否存在(值是否有效)macgmac-home $ vi test.sh:echo input the num:read numecho input is $numif -z $JHHOME -a -d $HOME/$num 如果变量$JHHOME为空,且$HOME/$num目录存在thenJHHOME=$HOME/$num则赋值fiecho JHHOME is $JHHOME-macgmac-home $ sh t

10、est.shinput the num:pppinput is pppJHHOME is目录-d $HOME/$num不存在,所以$JHHOME没被then赋值macgmac-home $ mkdir pppmacgmac-home $ sh test.shinput the num:pppinput is pppJHHOME is /home/macg/ppp一个-o的例子,其中却揭示了”=”必须两边留空格的问题echo input your choice:read ANSif $ANS=Yes-o $ANS=yes -o $ANS=y -o $ANS=Y thenANS=yelseANS=

11、nfiecho $ANSmacgmachome $ sh test.shinput your choice:nymacgmachome $ sh test.shinput your choice:noy为什么输入不是yes,结果仍是y(走then)因为=被连读了,成了变量$ANS=Yes,而变量又为空,所以走else了macgmachome $ vi test.shecho input your choice:read ANSecho input your choice:read ANSif $ANS = Yes-o $ANS = yes -o $ANS = y -o $ANS = Y the

12、nANS=yelseANS=nfiecho $ANSmacgmachome $ sh test.shinput your choice:nonmacgmachome $ sh test.shinput your choice:yesymacgmachome $ sh test.shinput your choice:yy=以test 条件表达式作为if条件=if test $num -eq 0等价于if $num eq 0 test表达式,没有 if test $num -eq 0thenecho try againelseecho goodfiman testmacgmachome $ ma

13、n test(1)User Commands(1)SYNOPSIStest EXPRESSION EXPRESSION -n STRINGthe length of STRING is nonzero-n和直接$str都是非0条件-z STRINGthe length of STRING is zeroSTRING1 = STRING2the strings are equalSTRING1 != STRING2the strings are not equalINTEGER1 -eq INTEGER2INTEGER1 is equal to INTEGER2INTEGER1 -ge INTE

14、GER2INTEGER1 is greater than or equal to INTEGER2INTEGER1 -gt INTEGER2INTEGER1 is greater than INTEGER2INTEGER1 -le INTEGER2INTEGER1 is less than or equal to INTEGER2INTEGER1 -lt INTEGER2INTEGER1 is less than INTEGER2INTEGER1 -ne INTEGER2INTEGER1 is not equal to INTEGER2FILE1 -nt FILE2FILE1 is newer

15、 (modification date) than FILE2FILE1 -ot FILE2FILE1 is older than FILE2-b FILEFILE exists and is block special-c FILEFILE exists and is character special-d FILEFILE exists and is a directory-e FILEFILE exists文件存在-f FILEFILE exists and is a regular file文件存在且是普通文件-h FILEFILE exists and is a symbolic l

16、ink (same as -L)-L FILEFILE exists and is a symbolic link (same as -h)-G FILEFILE exists and is owned by the effective group ID-O FILEFILE exists and is owned by the effective user ID-p FILEFILE exists and is a named pipe-s FILEFILE exists and has a size greater than zero-S FILEFILE exists and is a socket-w FILEFILE exists and is writable-x

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1