The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what these statements means namishtiwari UNIX for Dummies Questions & Answers 1 01-16-2008 11:20 PM
Automate SQL statements panchpan SUN Solaris 3 01-02-2008 11:32 PM
Please help on IF statements. filthymonk Shell Programming and Scripting 4 07-18-2007 02:59 AM
or statements? Blip Shell Programming and Scripting 1 01-19-2004 01:08 PM
if statements lilas UNIX for Dummies Questions & Answers 2 03-22-2001 08:49 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-30-2006
Registered User
 

Join Date: Nov 2006
Posts: 23
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?
Reply With Quote
Forum Sponsor
  #2  
Old 11-30-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
can you show your script?
Reply With Quote
  #3  
Old 11-30-2006
Registered User
 

Join Date: Nov 2006
Posts: 23
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
Reply With Quote
  #4  
Old 11-30-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by chapmana
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
Code:
if [ $choice -eq 1 ]
Other than equal, rest is ok.
Reply With Quote
  #5  
Old 11-30-2006
Registered User
 

Join Date: Nov 2006
Posts: 23
so the eq means if its not equal to 1 then its not correct? is that right?
Reply With Quote
  #6  
Old 11-30-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
-eq means equal
-ne means not equal

Code:
if [ $choice -eq 1 ]
if choice is equal to one then print "cool its working" and then exit the script with one.
Reply With Quote
  #7  
Old 11-30-2006
Registered User
 

Join Date: Nov 2006
Posts: 23
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0