The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what is wrong with this script? rs1969 UNIX for Dummies Questions & Answers 2 11-15-2007 07:16 AM
what is wrong with this script? hankooknara Shell Programming and Scripting 8 06-09-2007 01:33 PM
What is wrong with this script? heprox Shell Programming and Scripting 8 11-16-2006 05:43 AM
what is wrong with this script? circleW Shell Programming and Scripting 2 09-28-2004 09:27 PM
What is wrong with my script? Lem2003 UNIX for Dummies Questions & Answers 6 05-29-2003 02:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2007
amitg1980 amitg1980 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 2
What's wrong with this script

I am trying to create a script but it is giving me errors on Cygwin for the following script. Could someone tell me, what am I doing wrong?

choice=1000
echo "choice is $choice"
while [ $choice -ne 0 ]; do
echo "choice is $choice"
echo 'Please select your option:'
echo '1. Option 1'
echo '2. Option 2'
echo 'Enter the number:'
read choice
echo "choice is $choice"

if [ $choice -eq 1]; then
echo "You selected option 1"
choice=0
elif [$choice -eq 2]; then
echo "You selected option 2"
choice=0
else
echo "You have entered a wrong choice. Please choose from the options given"
choice=1000
fi
done
echo "Completed."


Response that I get is:
choice is 1000
choice is 1000
Please select your option:
1. Option 1
2. Option 2
Enter the number:
1
choice is 1
[: missing ]
[1: not found
You have entered a wrong choice. Please choose from the options given
choice is 1000
Please select your option:
1. Option 1
2. Option 2
Enter the number:

I think it is not taking $choice as integer in the condition box, but i am not sure.
  #2 (permalink)  
Old 11-12-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
U missed blanks:
Code:
[ $choice -eq 2]
should be:
Code:
[ $choice -eq 2 ]
Regards
  #3 (permalink)  
Old 11-12-2007
amitg1980 amitg1980 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 2
Hey thanks. That works. Can't believe the mistake, but that was an excellent pick on your side. Thanks once again.
  #4 (permalink)  
Old 11-12-2007
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
Suggestively, you may want to use a case statement in place of the if-elif.
This would allow for expansion if required later. And it's IMHO easier to read.

That is:
Code:
choice=1000
echo "choice is $choice"

while [ $choice -ne 0 ]; do

  echo "choice is $choice"
  echo 'Please select your option:'
  echo '1. Option 1'
  echo '2. Option 2'
  echo 'Enter the number:'
  read choice
  echo "choice is $choice"

  case $choice in
  1)  echo "You selected option 1"
      choice=0 ;;
  2)  echo "You selected option 2"
      choice=0 ;;
  *)  echo "You have entered a wrong choice. Please choose from the options given"
      choice=1000 ;;
  esac

done
echo "Completed."
Hope that helps some.

Cheers,
Cameron
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0