Reputation: 35
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
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
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
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