user1707517
user1707517

Reputation: 35

unexpected eof error in bash file

I'm creating a small program that does some simple math, but cannot run it. It just outputs "Unexpected end of file." I can't seem to find the error.. ?

#!/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

Upvotes: 0

Views: 555

Answers (3)

Jalal Hajigholamali
Jalal Hajigholamali

Reputation: 379

i executed under bash version 4 and no problem(no syntax error), but some semantics errors, variable second_num is undefined and && in if statement line 3: [: -ne: unary operator expected Enter number 1: 34 Enter number 2: 54 Cannot divide by 0. Start over. line 12: [: missing `]' You have exited the program.

send you script again

Upvotes: 0

RBH
RBH

Reputation: 592

I don't know what you are trying to do, but the following code should work fine without any error

 #!/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

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

At first glance I see 2 issues with your script:

  • until [ $second_num -ne 0 ]; do

    You don't initialize $second_num, so you're trying to compare 0 to nothing.

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

    && is a conditional execution operator. The AND operator for two expressions inside (single) square brackets is -a.

Upvotes: 2

Related Questions