2015-11-28 38 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 

你可以從這個例子可運行看,它的工作原理。如果你發現它不在你的系統上,你應該提供一個完整的例子,就像上面的例子一樣。