




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一部分 If的用法第一种 iflistthen dosomethinghere fi 当list表述返回值为True(0)时,将会执行dosomethinghere。#!/bin/sh myPath=/var/log/httpd/ myFile=/var /log/httpd/access.log 1. 是否存在并且是否具有可执行权限 #这里的-x 参数判断$myPathif ! -x $myPath; then mkdir $myPath fi 2. 目录是否存在 #这里的-d 参数判断$myPath是否存在 if ! -d $myPath; then mkdir $myPath fi 3. 文件是否存在 #这里的-f参数判断$myFile是否存在 if ! -f $myFile ; then touch $myFile fi 4. 变量是否有值#其他参数还有-n,-n是判断一个变量是否是否有值 if ! -n $myVar ; then echo $myVar is empty exit 0 fi 5. 两个变量判断是否相等 if $var1 = $var2 ; then echo $var1 eq $var2 else echo $var1 not eq $var2 fi6. 比较数字的大小if $df -gt 50 then $df diskalertelseexitfi7. 字符串和数字比较大小if (d = 0) if $D -eq 0 ; then if (d != 0) if $D -ne 0 ; then if (d 0) if $D -gt 0 ; then if (d 0) if $D -lt 0 ; then if (d = 0) if $D -ge 0 ; then 字符串比较 if (strcmp(str,”abc”)=0) if $STR != abc ; then8. If其他用法shell脚本中判断环境变量是否存在2007-09-12 20:25if ! -d $JAVA_HOME then echo JAVA_HOME not set!else echo JAVA_HOME: $JAVA_HOMEfi更紧凑的写法 -n $JAVA_HOME &echo exist|echo No exist注意-d和-n是不同的,前者判断目录是否存在,后者判断String是否不为空-a file True if file exists. -b file True if file exists and is a block special file. -c file True if file exists and is a character special file. -d file True if file exists and is a directory. -e file True if file exists. -f file True if file exists and is a regular file. -g file True if file exists and is set-group-id. -h file True if file exists and is a symbolic link. -k file True if file exists and its sticky bit is set. -p file True if file exists and is a named pipe (FIFO). -r file True if file exists and is readable. -s file True if file exists and has a size greater than zero. -t fd True if file descriptor fd is open and refers to a terminal. -u file True if file exists and its set-user-id bit is set. -w file True if file exists and is writable. -x file True if file exists and is executable. -O file True if file exists and is owned by the effective user id. -G file True if file exists and is owned by the effective group id. -L file True if file exists and is a symbolic link. -S file True if file exists and is a socket. -N file True if file exists and has been modified since it was last read. file1 -nt file2 True if file1 is newer (according to modification date) than file2. file1 -ot file2 True if file1 is older than file2. file1 -ef file2 True if file1 and file2 have the same device and inode numbers. -o optname True if shell option optname is enabled. See the list of options under the description of the -o option to the set builtin below. -z string True if the length of string is zero. -n string string True if the length of string is non-zero. string1 = string2 True if the strings are equal. = may be used in place of =. string1 != string2 True if the strings are not equal. string1 string2 True if string1 sorts after string2 lexicographi? cally in the current locale. arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.第二部分 shell脚本1. 删除相同内容的文件for file in * #遍历文件do if ! -f $file #如果文件存在的话,因为有的文件可能被剔除了. then echo $file not exist else for i in * do if $file != $i #如果文件名不同 then if cmp -s $file $i then mv $i ./same/ fi fi done mv $file ./unique/ fidone我一共处理4000多个文件,运行的挺慢的,用了1天零2夜才处理完,我汗,竟剔出了近2000,我再汗.为了提高效率,后来考虑只对文件大小相同的文件才进行比较.脚本如下:for file in * #遍历文件do if ! -f $file #如果文件存在的话,因为有的文件可能被剔除了. then echo $file not exist else leno=ls -l $file | awk print #使用ls和awk提出文件大小 for i in * do leni=ls -l $i | awk print if $file != $i -a $leno = $leni #如果文件名不同,并且文件大小相同 then if cmp -s $file $i then mv $i ./same/ fi fi do
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖南湘西古丈县教育类事业单位公开引进高层次急需紧缺人才6人模拟试卷及答案详解(有一套)
- 2025广东揭阳市惠来县校园现场招聘教师70人模拟试卷及答案详解参考
- 高薪招聘公司用人承诺书(5篇)
- 2025广西城轨工程建设有限公司招聘20人考前自测高频考点模拟试题及答案详解(考点梳理)
- 2025福建省康辉国际旅行社股份有限公司招聘5人考前自测高频考点模拟试题含答案详解
- 2025内蒙古镶黄旗蒙金矿业开发有限公司招聘25人模拟试卷及参考答案详解1套
- 债务偿还及信誉保持保证承诺书(4篇)
- 我的朋友写人篇章5篇范文
- 2025贵州省妇幼保健院第十三届贵州人才博览会引才1人模拟试卷附答案详解
- 山东省九五高中协作体2024-2025学年高三下学期5月质量检测联考地理试题(解析版)
- 制作扒鸡的流程
- 居间房屋租赁合同模板
- 2025年度典型火灾案例及消防安全知识专题培训
- 《智慧化工园区系统运维管理要求》
- 外研版九年级英语上册期中综合测试卷含答案
- 肝癌中医治疗新进展
- 药品类体外诊断试剂专项培训课件
- 高中数学新教材选择性必修第二册《4.2等差数列》课件
- 建筑识图与构造 课件 项目8 识读建筑详图
- 《湖南省职工基本医疗保险门诊慢特病基础用药指南(第一批)》
- 四年级上册道德与法治学科质量分析报告
评论
0/150
提交评论