shell中if用法.docx_第1页
shell中if用法.docx_第2页
shell中if用法.docx_第3页
shell中if用法.docx_第4页
shell中if用法.docx_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论