Bash Shell编号比较

时间:2020-01-09 10:37:42  来源:igfitidea点击:

如何在bash shell中比较数字?
您需要使用test命令通过以下运算符执行各种数值比较:

  • INTEGER1 -eq INTEGER2INTEGER1等于INTEGER2
  • INTEGER1 -ge INTEGER2INTEGER1大于或等于INTEGER2
  • INTEGER1 -gt INTEGER2INTEGER1大于INTEGER2
  • INTEGER1 -le INTEGER2INTEGER1小于或等于INTEGER2
  • INTEGER1 -lt INTEGER2INTEGER1小于INTEGER2
  • INTEGER1 -ne INTEGER2INTEGER1不等于INTEGER2

test工具

测试命令计算表达式,如果计算结果为true,则返回零(true)退出状态。
否则返回1(假)。
语法为:

test condition  
test condition && do_something || do_nothing_due_to_false
[ condition ] && do_something || do_nothing_due_to_false

if命令的语法如下:

if [ condition ]; then
       do_run_this_due_to_true_condition
else
       do_run_this_due_to_false_condition
fi

示例:bash中比较数字

找出5是否大于10,输入(在终端键入命令):

x=5
y=10
[ $x -gt $y ] 
echo $?

输出示例:

1

在bash shell中,非零输出意味着错误的结果,即$x不大于$y。
请尝试以下示例(在终端上键入命令):

x=51
y=10
[ $x -gt $y ] 
echo $?
0

零值表示真实结果,即$x大于$y。
让我们使用以下语法使输出更具可读性:

[ condition ] &&  true-command || false-command

找出5是否大于10,输入(在终端键入命令):

x=5
y=10
[ $x -gt $y ]  && echo "Yes $x > $y " || echo "No $x is not > $y"
No $x is not > $y

您可以使用if语句,如下所示:

#!/bin/bash
read -p "Enter a number (must be greater than 20) : " n
if test $n -gt 20
then
    echo "$n is greater than 20."
else
    echo "You are not following my instructions."
fi

或者

#!/bin/bash
read -p "Enter a number (must be greater than 20) : " n
if [ $n -gt 20 ]; then
    echo "$n is greater than 20."
else
    echo "You are not following my instructions."
fi

如下运行:

./script.sh
Enter a number (must be greater than 20) : 22
22 is greater than 20.

./script.sh
Enter a number (must be greater than 20) : 8
You are not following my instructions.

算术测试选项

您可以通过键入以下命令来查看所有受支持选项的列表:

$ help test

输出示例:

File operators:
 
        -a FILE        True if file exists.
        -b FILE        True if file is block special.
        -c FILE        True if file is character special.
        -d FILE        True if file 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 is set-group-id.
        -h FILE        True if file is a symbolic link.
        -L FILE        True if file is a symbolic link.
        -k FILE        True if file has its `sticky' bit set.
        -p FILE        True if file is a named pipe.
        -r FILE        True if file is readable by you.
        -s FILE        True if file exists and is not empty.
        -S FILE        True if file is a socket.
        -t FD          True if FD is opened on a terminal.
        -u FILE        True if the file is set-user-id.
        -w FILE        True if the file is writable by you.
        -x FILE        True if the file is executable by you.
        -O FILE        True if the file is effectively owned by you.
        -G FILE        True if the file is effectively owned by your group.
        -N FILE        True if the file has been modified since it was last read.
 
      FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                       modification date).
 
      FILE1 -ot FILE2  True if file1 is older than file2.
 
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
 
    String operators:
 
        -z STRING      True if string is empty.
 
        -n STRING
        STRING         True if string is not empty.
 
        STRING1 = STRING2
                       True if the strings are equal.
        STRING1 != STRING2
                       True if the strings are not equal.
        STRING1 < STRING2
                       True if STRING1 sorts before STRING2 lexicographically.
        STRING1 > STRING2
                       True if STRING1 sorts after STRING2 lexicographically.
 
    Other operators:
 
        -o OPTION      True if the shell option OPTION is enabled.
        ! EXPR         True if expr is false.
        EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
        EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
 
        arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                       -lt, -le, -gt, or -ge.
 
    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.