• shell中的if用法


    第一部分  If的用法

    第一种 
    if list then 
      do something here 
    fi 
    当list表述返回值为True(0)时,将会执行"do something here"。

    #!/bin/sh

    myPath="/var/log/httpd/"

    myFile="/var /log/httpd/access.log"

    1.       是否存在并且是否具有可执行权限

    #这里的-x 参数判断$myPath

    if [ ! -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'

    fi

    6.       比较数字的大小

    if [ $df -gt 50 ]
      then
      $df > diskalert
      else
      exit
    fi

    7.       字符串和数字比较大小

    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" -le "0" ] ; then

    if (d >= 0)

     if [ "$D" -ge "0" ] ; then

    字符串比较

     if (strcmp(str,”abc”)==0) {

    }

     if [ "$STR" != "abc" ]; then

    8.       If其他用法

    shell脚本中判断环境变量是否存在2007-09-12 20:25

    if [ ! -d "$JAVA_HOME" ]

    then echo "JAVA_HOME not set!"

    else echo "JAVA_HOME:" $JAVA_HOME

    fi

    更紧凑的写法

    [[ -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 before string2 lexicographi?

    cally in the current locale.

    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/
        fi
    done

    我一共处理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
                done
                mv $file ../unique/
        fi
    done

    2.       判断是不是字符文件

    #!/bin/bash

    #cp.sh

    echo –e “please input filename ”

    read FILENAME

    FILE=$FILENAME

    if [ -c $FILE ]

       then cp $FILENAME /dev

       else echo “ It’s not Character device file”

    fi

  • 相关阅读:
    prefixspan python
    python avro 数据格式使用demo
    UEBA 学术界研究现状——用户行为异常检测思路:序列挖掘prefixspan,HMM,LSTM/CNN,SVM异常检测,聚类CURE算法
    Dropout, DropConnect ——一个对输出,一个对输入
    成都Uber优步司机奖励政策(4月12日)
    北京Uber优步司机奖励政策(4月12日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(4月12日)
    MYSQL--慢查询,卡死等处理
    记boost在gcc的一个库链接问题generic_category()
    Linux生成core文件、core文件路径设置
  • 原文地址:https://www.cnblogs.com/hxu7373/p/3508164.html
Copyright © 2020-2023  润新知