bash文件中出现意外的eof错误

时间:2013-03-30 21:23:16

标签: linux bash scripting

我正在创建一个执行一些简单数学的小程序,但无法运行它。它只输出“意外的文件结束”。我似乎无法找到错误..?

#!/bin/bash

until [ $second_num -ne 0 ]; do
    echo "Enter number 1:"
    read first_num
    echo "Enter number 2:"
    read second_num
    echo "Cannot divide by 0. Start over."
done

result=0
if [ $first_num -ne 999 && $second_num -ne 999 ]; then
    let result=$((first_num/second_num))
    echo $result    
    echo "Program finished!"
else
    echo "You have exited the program."
    exit 0 
fi  

number=1
while [ $number -lt 100 ]; do
    echo $number
    let number=$(( $number % 5 ))
    echo $number >> sample.txt
done

3 个答案:

答案 0 :(得分:2)

乍一看,我看到你的剧本有两个问题:

  • until [ $second_num -ne 0 ]; do

    您没有初始化$second_num,因此您尝试将0与0进行比较。

  • if [ $first_num -ne 999 && $second_num -ne 999 ]; then

    &&conditional execution operator。 (单个)方括号内的两个表达式的AND运算符是-a

答案 1 :(得分:1)

我不知道你要做什么,但下面的代码应该可以正常工作而没有任何错误

 #!/bin/bash

until [ $second_num -ne 0 ]; do
    echo "Enter number 1:"
    read first_num
    echo "Enter number 2:"
    read second_num
    echo "Cannot divide by 0. Start over."
done

result=0
if [ "$first_num" -ne "999" -a "$second_num" -ne "999" ];

then
    let result=$(($first_num/$second_num))
    echo $result    
    echo "Program finished!"
else
    echo "You have exited the program."
    exit 0
fi

number=1
while [ $number -lt 100 ]; do
    echo $number
    let number=$(( $number % 5 ))
    echo $number >> sample.txt
done

答案 2 :(得分:0)

我在bash版本4下执行并没有问题(没有语法错误), 但是一些语义错误,变量second_num未定义和&&在if语句中 第3行:[:-ne:一元运算符预期 输入数字1: 34 输入数字2: 54 不能除以0.重新开始。  第12行:[:缺少`]' 你已退出该计划。

再次发送脚本