我是linux控制台中的乞丐;我想用整数变量创建if语句
if[$x= [$#-2]]但是控制台收到如果can't find this statment if[1 = [5-2]]请帮助我和纠正我的陈述。
发布于 2015-11-28 13:10:24
您需要算术扩展:$((expression))
if [ $x = $(($# - 2)) ]; then
# ^ ^ ^ ^ ^ spaces are mandatory发布于 2015-11-28 14:19:35
启动$#是传递给bash脚本的参数数
./bash_script 1 2 3
$#汽车-神奇地填充到3。我希望你已经知道这一点。
#!/bin/bash
x=1
#If you are trying to compare `$x` with the value of the expression `$# - 2` below is how you do it :
if (( $x == $# - 2 ))
then
echo "Some Message"
fi
#If you are trying to check the assignment to `$x was successful below is how you do it :
if (( x = $# - 2 ))
then
echo "Some Message"
fi第二个条件几乎总是正确的,但第一个条件可能是假的。以下是我的测试结果:
#Here the both ifs returned true
sjsam@WorkBox ~/test
$ ./testmath1 1 2 3
Some Message
Some Message
#Here the first if returned false because we passed 4 parameters
sjsam@WorkBox ~/test
$ ./testmath1 1 2 3 4
Some Messagehttps://stackoverflow.com/questions/33971598
复制相似问题