2013-10-09 198 views
0

我是UNIX的新手......我正在嘗試編寫一個bash腳本,該腳本需要用戶輸入兩個整數,並使用if條件輸出這兩個數字之間的偶數。如果出現「其他附近出現意外的令牌」錯誤消息,則我卡在嵌套中。我不知道錯誤是什麼。任何幫助?如何解決嵌套,如果問題

這是我迄今所做的:

echo plz enter first number 
read n1 
echo plz enter second number 
read n2 
start=$n1 
end=$n2 
if [ start < end ] then 
    for (c=start;c<=end;c++) 
    do 

     if [ $((c % 2)) -eq 0 ]; then 
      echo $c 
     fi 
    done 
else 
    echo "not bigger" 
fi 

回答

1

我想我會推薦不同的方法:

((start % 2)) && ((start = 1 + start)) 
while ((start < end)) 
do 
    echo ${start} 
    ((start += 2)) 
done 
0

你需要插入或者分號或之前,第一個「新行then「:

if [ start < end ] ; then 
       ^
1

我已經試過這樣: -

echo "Enter first number" 
read first 
echo "Enter second number" 
read second 
start=$first 

endLine=$second 
while [ $start -le $endLine ] 
do 
if [ $((start % 2)) -eq 0 ] 
then 
echo $start "is an even number" 
#else 
# echo $start "is an odd number" 
fi 
start=`expr $start + 1` 
done