分页: 1 / 1

这个if语句怎么无法比较数字大小?

发表于 : 2017-07-17 21:02
yubinalice
如题,

一个很简单的if判断语句,

但是就是不成。

不知道哪里出问题了。

语句如下:

for ((i=1;i<=10;i++))
do
if $i=1
echo $i time
then
echo $i times
fi
done

返回如下,真稀奇:

bash: 1=1: 未找到命令
1 time
1 times
bash: 2=1: 未找到命令
2 time
2 times
bash: 3=1: 未找到命令
3 time
3 times
bash: 4=1: 未找到命令
4 time
4 times
bash: 5=1: 未找到命令
5 time
5 times
bash: 6=1: 未找到命令
6 time
6 times
bash: 7=1: 未找到命令
7 time
7 times
bash: 8=1: 未找到命令
8 time
8 times
bash: 9=1: 未找到命令
9 time
9 times
bash: 10=1: 未找到命令
10 time
10 times

谁知道这个应该如何写?

后来我又换成了

if $i eq 1

但是还是不成,

不知道哪里出问题了。

希望高手可以看看。

谢谢!!

Re: 这个if语句怎么无法比较数字大小?

发表于 : 2017-07-17 22:01
astolia
问题就在你根本不会shell的基本语法就想当然瞎写

代码: 全选

       if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The if list is executed.  If its exit status is zero, the then list is executed.  Otherwise, each elif list  is  executed
              in  turn,  and if its exit status is zero, the corresponding then list is executed and the command completes.  Otherwise,
              the else list is executed, if present.  The exit status is the exit status of the last command executed, or  zero  if  no
              condition tested true.

代码: 全选

       [[ expression ]]
              Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.  Expressions are composed
              of the primaries described below under CONDITIONAL EXPRESSIONS.  Word splitting and pathname expansion are not  performed
              on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command sub‐
              stitution, process substitution, and quote removal are performed.  Conditional operators such as -f must be  unquoted  to
              be recognized as primaries.

代码: 全选

       test expr
       [ expr ]
              Return a status of 0 (true) or 1 (false) depending on the evaluation of the conditional expression expr.   Each  operator
              and  operand  must  be  a separate argument.  Expressions are composed of the primaries described above under CONDITIONAL
              EXPRESSIONS.  test does not accept any options, nor does it accept and ignore an argument of -- as signifying the end  of
              options.

Re: 这个if语句怎么无法比较数字大小?

发表于 : 2017-07-24 17:11
yubinalice
astolia 写了:问题就在你根本不会shell的基本语法就想当然瞎写

代码: 全选

       if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The if list is executed.  If its exit status is zero, the then list is executed.  Otherwise, each elif list  is  executed
              in  turn,  and if its exit status is zero, the corresponding then list is executed and the command completes.  Otherwise,
              the else list is executed, if present.  The exit status is the exit status of the last command executed, or  zero  if  no
              condition tested true.

代码: 全选

       [[ expression ]]
              Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.  Expressions are composed
              of the primaries described below under CONDITIONAL EXPRESSIONS.  Word splitting and pathname expansion are not  performed
              on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command sub‐
              stitution, process substitution, and quote removal are performed.  Conditional operators such as -f must be  unquoted  to
              be recognized as primaries.

代码: 全选

       test expr
       [ expr ]
              Return a status of 0 (true) or 1 (false) depending on the evaluation of the conditional expression expr.   Each  operator
              and  operand  must  be  a separate argument.  Expressions are composed of the primaries described above under CONDITIONAL
              EXPRESSIONS.  test does not accept any options, nor does it accept and ignore an argument of -- as signifying the end  of
              options.

这个非常感谢,

目前写出来了

应该是
if [$i == 1}
then
echo $i time
else
echo $i times
fi