Click to See Complete Forum and Search --> : if [i do something] then i get an error


chris65
07-11-2002, 03:40 AM
Im sure this is pretty simple to some of you ( and it WAS in visual basic) but bash does not want to take this and I am sure its just me but i have been to the help files over and over and just cant seem to see the answer.



echo "1. To do this press 1"
echo "2. To do that press 2"
echo "3. To exit press 3"
read#

if $read =1 then
(call some script.sh)
exit
fi

if $read =2 then
(call some script.sh)
exit
fi


I have 4 or 5 commands like this and the script will run either all the commands back to back, or give me an EFO error for the fi command. What am i missing??

edit
You know, I thought i was lost but now i am sure. i went back and
re-read the chapter on varuables and came to a conclusion: THIS IS CRAZY. i dont know if the "if " statement is right here or not. why cant i just use a good oL "dim foo as string" statement. Now i am pretty sure this wont work, but what is the closest thing in bash. ???

l01yuk
07-11-2002, 08:10 AM
Tests for numbers are not done with =.

man test to find out more.

you need to do

read A

if [ $A -eq 1 ]
then
[do this]
exit
fi

if [ $A -eq 2 ]
then
.
.
.

If you were testing for a string you would do

read A

if [ "$A" = "string" ]
then
.
.
.

The reason for the " around the $A is so that you don't get an error if $A contains nothing (user just hits [return]).

chris65
07-11-2002, 08:20 AM
THANK YOU, so what you are saying, I'm slow so be kind, is that the "a" is a null string? or that the user did not select a option number.
Now, will i need a "read" statement after each option for this to work .

echo "1.to do this press 1"
read
echo "2. to do that press 2"
read
...
or can i use the one "read" statement for the whole list?

l01yuk
07-11-2002, 09:16 AM
All variables are initialised to "" in bash. This means that if the first reference to the variable is for example

echo $A

then nothing will be printed.

For this reason you should put " around the string variable "$A" when testing the value. The $ is important because it shows that you want to reference the value held in the variable.


To put something in the variable you need to assign it a value. To do this you either:

A="value" #Notice, no $ and no [space] either side of the =.

The other way to assign a value to the variable is from they stdin (generally the keyboard). To do this you use the read command so you get:

read A.

You only need one read since you only want one input. Having many read statements will wait for many inputs from the user.

Hope this helps.

Stuka
07-11-2002, 04:48 PM
bash also allows you to declare variables as integers, with declare -i (I don't have my cheat sheet available, but that's close to the right syntax). Also, there is a case statement in bash, like so:

case $A in
1) #statements
break;;
2) #statements
break;;
*) #default
break;;
esac

Note: as above, the syntax is probably not perfect, but it's close.

chris65
07-11-2002, 09:22 PM
thanks, stuka, i checked the cheat sheet and see what you mean. the case statement looks good, ill try to make it work for the practice.
now, 101yuk, i did it like this,


echo "Whar do you want to do today?
echo "1.Burn a cd"
echo "2.Erease a CD"
echo "3.Use instant messenger"
echo "4.Exit"
read A

if [ $A -eq 1 ]
then
[ cdwrite.sh ]
exit
fi

if [ $A -eq 2 ]
then
[ cde.sh ]
exit
fi

if [ $A -eq 3 ]
then
[ amsn ]
exit
fi

if [ $A -eq 4 ]
then
exit
fi

but i get an EOF statement before the READ A statement starts. what did i do . knowing me, its what did i not do, but either way i get errors.
It says unexpected EFO while searching for matching ` " `. I feel like the if statement is right this time but could it be the EXIT command that is doing it?

X_console
07-11-2002, 11:45 PM
Look at your first line. You didn't terminate the string with "

chris65
07-12-2002, 12:59 AM
well, chalk that up to stupid mistakes. That fixed the EFO error. Dont know why i did not catch it, but thats life. now the script loads and displays the options. but no matter what option i select, the script will
EXIT. maybe the EXIT command being in all the statements is doing it. what if i do:


if [ $A -eq 1 ] #gets the value of A
then
do #would this work?
[ cdwrite.sh ]
done
fi
read A #instead of exit, would go back and look for new command


if [ $A -eq 2 ]
then
do
[ cde.sh ]
done
fi
read A
#could have just one exit command at the end
for option 4.


can i combind the "if" and "do" statement??
is this even possible.

l01yuk
07-12-2002, 02:36 AM
I would reccomend that you go to www.gnu.org and look at their bash manual.

You need to learn about flow control (for/while loops in particular).

X_console
07-12-2002, 03:27 PM
Try this


echo "What do you want to do today?
echo "1.Burn a cd"
echo "2.Erase a CD"
echo "3.Use instant messenger"
echo "4.Exit"
read A

[code]
if [ $A -eq 1 ] then
cdwrite.sh

elif [ $A -eq 2 ] then
cde.sh

elif [ $A -eq 3 ]then
amsn

else
exit 0
fi

exit 0