2015-11-28 36 views
0

我想创建if语句,但在这个声明中有一些错误如何比较变量在for循环bash的

for i in ${@:2} ; do 
if (($2 -eq $i)) 
     then 
     continue 
     fi 
    done 

如何解决我的if语句

+2

分享错误本身,你看过http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ – TommySM

回答

0

我建议

if [ "$2" = "$i" ] 
2

您的陈述仅适用于整数。

如果要比较他们作为字符串,您可以使用[[ "string1" = "string2" ]]

$ cat -v myscript 
#!/bin/bash 
for i in "${@:2}" ; do 
    if [[ "$2" = "$i" ]] 
    then 
    echo "$2 and $i are the same" 
    else 
    echo "$2 and $i are different" 
    fi 
done 

$ chmod +x myscript 
$ ./myscript dummy target foo bar target 
target and target are the same 
target and foo are different 
target and bar are different 
target and target are the same 

你可以从这个例子可运行看,它的工作原理。如果你发现它不在你的系统上,你应该提供一个完整的例子,就像上面的例子一样。