Else in If Statements

Sorry to be a pain, but how does the else work in the if statements?

Ive been making scripts with if statements but i cant get the else statements working.

Can you help?

can you show your script?

well i haven't used the else in a script but for instance

if [ $choice = 1 ]

then

echo "cool its working"

exit 1

else

echo "not working try again"

sh andy1

fi

is this the correct way to use it

If you are comparing with numbers use -eq option

if [ $choice -eq 1 ]

Other than equal, rest is ok.

so the eq means if its not equal to 1 then its not correct? is that right?

-eq means equal
-ne means not equal

if [ $choice -eq 1 ]

if choice is equal to one then print "cool its working" and then exit the script with one.

Hi,

I tell you what i thinks we are getting confused, here is the full source code for the script im writing, run it and hit X to exit and i get an error. Type in a wrong number and it errors also

clear
echo
echo
echo
echo
echo "***********************************Welcome to INET Diagnostics Utility ***********************************************"
echo ""
echo
echo
echo
echo " 1) Ping Web Site"
echo " 2) Telnet"
echo " 3) Logit"
echo " 4) Pico"
echo " 5) Text Based Inetnet"
echo " 6) Files Counter"
echo " 7) File Lister"
echo " 8) Backup Directories"
echo " 9) Calculator"
echo " 10) Calender"
echo " 11) FTP Client"
echo " 12) DNS Client"
echo " X) Exit"
echo
echo
echo
echo
echo " Choose your choice"
echo "
"
read choice

if [ "$choice" -eq "1" ]

then

echo " To Escape A Ping Cycle Hold CTRL And Press C!"
echo
echo

sleep 4

echo "Which Site Do You Wish To Ping? (Include full extension without the www) "
read name
echo
echo
echo

ping -c 10 $name

echo "Hit Enter To Return To The Main Menu!"
echo "Paused............"
read
inet2

fi

if [ "$choice" -eq "2" ]

then

echo " To Quit Telnet, Type Quit And Hit Enter!"
echo
echo
sleep 4
echo " Which Server Do You Wish To Connect To?"
read server
echo
echo " Which Port Do You Wish To Connect To?"
read port
echo
echo
echo

telnet $server $port

inet2

fi

if [ "$choice" -eq "3" ]

then

echo " Now Transfering To Logit!"

sleep 4

logit

inet2

fi

if [ "$choice" -eq "4" ]

then

echo "Which File Do You Wish To Open? (If Not In Current Directory Use File Path Also!)"
echo
echo
read picofile

pico $picofile

inet2

fi

if [ "$choice" -eq "5" ]

then

echo " Connecting to Lynx Internet Browser"

sleep 4

echo " To Quit Lynx Press Q At Any Time"

sleep 3

lynx

inet2

fi

if [ "$choice" -eq "6" ]

then

echo " Enter Directory In Full"

read dname

echo
echo
echo
echo "Total Files : "

ls -l | grep -c ^-

sleep 6

inet2

fi

if [ "$choice" -eq "7" ]

then

echo "Loading Working Directory!"

wd= "pwd"

sleep 3

cd $wd

echo "Loading File List Please Wait!"

sleep 2

ls -R *

echo "Hit Enter At Any Time To Return!"

read

inet2

fi

if [ "$choice" -eq "8" ]

then

echo " Starting Backup Directories Now!"
echo
echo

sleep 3

backup

inet2

fi

then
echo "Type Quit And Hit Enter To Exit!"
echo
echo "Loading Calculator"
echo
echo
sleep 3

bc

inet2

fi

if [ "$choice" -eq "10" ]

then

echo " Loading Calender"
sleep 3
echo
cal
echo
echo
echo "Hit Enter To Return To Menu!"

read

inet2

fi

if [ "$choice" -eq "11" ]

then

echo " Loading FTP Client"
echo
echo " To Exit Type Quit And Hit Enter"
sleep 3
ftp

inet2

fi

if [ "$choice" -eq "12" ]

then

echo " Transfering To DNS"
echo " Type Exit And Hit Enter To Exit"
echo
sleep 3

nslookup

inet2

fi

if [ "$choice" -eq "X" ]

then

echo "Goodbye"

sleep 2

clear

exit 1

fi

Care to look at switch/case alternative ? See Perderabo's reply to get a feel.

fi


if [ "$choice" = "9" ]
then
echo "Type Quit And Hit Enter To Exit!"
echo
echo "Loading Calculator"

You missed above if

-eq is for numeric comparison.
When you enter option X

if [ "X" -eq "1" ]

This is error because it compares 1 with alphabet

To rectify this problem either convert all -eq to = or use numeric option to exit