2013-10-09 68 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