shell中if用法

shell中if用法

ID:37712960

大小:54.64 KB

页数:6页

时间:2019-05-29

shell中if用法_第1页
shell中if用法_第2页
shell中if用法_第3页
shell中if用法_第4页
shell中if用法_第5页
资源描述:

《shell中if用法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、第一部分If的用法第一种 if list then   do something here fi 当list表述返回值为True(0)时,将会执行"do something here"。#!/bin/shmyPath="/var/log/httpd/"myFile="/var/log/httpd/access.log"1.是否存在并且是否具有可执行权限#这里的-x参数判断$myPathif[!-x"$myPath"];thenmkdir"$myPath"fi2.目录是否存在#这里的-d参数判断$myPath是否存在if[!-d"$myPath"];thenmkdir"$myPa

2、th"fi3.文件是否存在#这里的-f参数判断$myFile是否存在if[!-f"$myFile"];thentouch"$myFile"fi1.变量是否有值#其他参数还有-n,-n是判断一个变量是否是否有值if[!-n"$myVar"];thenecho"$myVarisempty"exit0fi2.两个变量判断是否相等if["$var1"="$var2"];thenecho'$var1eq$var2'elseecho'$var1noteq$var2'fi3.比较数字的大小if[$df-gt50]  then  $df>diskalert  else  exitfi4.字符串

3、和数字比较大小if(d==0)if["$D"-eq"0"];thenif(d!=0)if["$D"-ne"0"];thenif(d>0)if["$D"-gt"0"];thenif(d<0)if["$D"-lt"0"];thenif(d<=0)if["$D"-le"0"];thenif(d>=0)if["$D"-ge"0"];then字符串比较if(strcmp(str,”abc”)==0){}if["$STR"!="abc"];then1.If其他用法shell脚本中判断环境变量是否存在2007-09-1220:25if[!-d"$JAVA_HOME"]thenecho"JAV

4、A_HOMEnotset!"elseecho"JAVA_HOME:"$JAVA_HOMEfi更紧凑的写法[[-n$JAVA_HOME]]&&echoexist

5、

6、echoNoexist注意-d和-n是不同的,前者判断目录是否存在,后者判断String是否不为空-afileTrueiffileexists.-bfileTrueiffileexistsandisablockspecialfile.-cfileTrueiffileexistsandisacharacterspecialfile.-dfileTrueiffileexistsandisadirectory.-efileT

7、rueiffileexists.-ffileTrueiffileexistsandisaregularfile.-gfileTrueiffileexistsandisset-group-id.-hfileTrueiffileexistsandisasymboliclink.-kfileTrueiffileexistsandits``sticky''bitisset.-pfileTrueiffileexistsandisanamedpipe(FIFO).-rfileTrueiffileexistsandisreadable.-sfileTrueiffileexistsandhas

8、asizegreaterthanzero.-tfdTrueiffiledescriptorfdisopenandreferstoaterminal.-ufileTrueiffileexistsanditsset-user-idbitisset.-wfileTrueiffileexistsandiswritable.-xfileTrueiffileexistsandisexecutable.-OfileTrueiffileexistsandisownedbytheeffectiveuserid.-GfileTrueiffileexistsandisownedbytheeffect

9、ivegroupid.-LfileTrueiffileexistsandisasymboliclink.-SfileTrueiffileexistsandisasocket.-NfileTrueiffileexistsandhasbeenmodifiedsinceitwaslastread.file1-ntfile2Trueiffile1isnewer(accordingtomodificationdate)thanfile2.file1-otfile2Trueiffile1isoldert

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。