LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2006, 07:36 AM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
BASH IF statement


Hi All

Need some help with this:

Quote:
clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

if ["$H1" == "1"];
then
`echo "You chose option 1.`;

elif ["$H1" == "2"];
then
`echo "You chose option 2.`;

elif ["$H1" == "3"];
then
`echo "You chose option 3.`;

elif ["$H1" == "4"];
then
`echo "You chose option 4.`;

elif [$"H1" == "5"];
then
`echo "You chose option 1 as well as"`
`echo "option 2"`
`echo "option 3"`
`echo "option 4"`;
fi
when I run the script, it simply says:

Quote:
./hold.sh: line 15: [5: command not found
./hold.sh: line 19: [5: command not found
./hold.sh: line 23: [5: command not found
./hold.sh: line 27: [5: command not found
./hold.sh: line 31: [H1: command not found

What am I doing wrong here? I'm running RedHat AS 3.
 
Old 05-04-2006, 07:58 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394
Hi,

There's more then one thing wrong with the above code, but quoting is the main culprit.
- echo is an internal bash command and you do not need the backticks around them,
- missing double end quotes in some of the echo lines,
- there need to be a space after the left square bracket and before the rigth square bracket.

Here's a working version:

Code:
#!/bin/bash
clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

if [ $H1 == "1" ]
then
  echo "You chose option 1."
elif [ $H1 == "2" ]
then
  echo "You chose option 2."
elif [ $H1 == "3" ]
then
  echo "You chose option 3."
elif [ $H1 == "4" ]
then
  echo "You chose option 4."
elif [ $H1 == "5" ]
then
  echo "You chose option 1 as well as"
  echo "option 2"
  echo "option 3"
  echo "option 4"
fi
Hope this clears things up a bit.
 
Old 05-04-2006, 08:31 AM   #3
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Druuna, thank you SO much!

Works great!
 
Old 05-04-2006, 09:15 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Void, Debian, Puppy, Raspbian
Posts: 3,483

Rep: Reputation: 223Reputation: 223Reputation: 223
why not use select
safer

Code:
#!/bin/bash
x='One Two Three All Quit'

clear
while :;do
echo "Which option do you want?"
echo
echo


select option in $x;do
case $option in
   Quit) break 3
       ;;
   *) echo you picked $option
        ;;
esac
break
done
done
 
Old 05-04-2006, 09:15 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Void, Debian, Puppy, Raspbian
Posts: 3,483

Rep: Reputation: 223Reputation: 223Reputation: 223
why not use select
safer

Code:
#!/bin/bash
x='One Two Three All Quit'

clear
while :;do
echo "Which option do you want?"
echo
echo


select option in $x;do
case $option in
   Quit) break 3
       ;;
   *) echo you picked $option
        ;;
esac
break
done
done
 
Old 05-05-2006, 02:31 AM   #6
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Seems a bit complicated for my blood, but I'll check it out definitely!

Thanks bikearsbilly.
 
Old 05-05-2006, 04:01 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Void, Debian, Puppy, Raspbian
Posts: 3,483

Rep: Reputation: 223Reputation: 223Reputation: 223
??????
hey! It's a lot less complex than
a load of ifs.


IMHO
it is definitely the way
 
Old 05-05-2006, 06:45 AM   #8
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Just a quick question tho:

Let's say you want to replace the "echo" bit with a command like say "sed something blabla", but in each case something different, how would you go about that using select?

Also, when using select, does the person only have to type say "3" or would the user have to type "3 [Enter]"?

Then the last question, in case the user wants to select a global option (like having option 1, 2, 3 and 4 run), how'd you do this using select?


Select seems powerful and if it could do all of the above, I'll definitely try it out until I get it right.


Thanks again bigearsbilly!
 
Old 05-06-2006, 08:42 AM   #9
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
As per bigearsbilly using a case statement is the way to go:
Code:
clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

select $H1 in
case
    1) echo "You chose option 1";;
    2) echo "You chose option 2";;
    3) echo "You chose option 3";;
    4) echo "You chose option 4";;
    5) echo "You chose option 1 as well as"
       echo "option 2"
       echo "option 3"
       echo "option 4";;
    *) echo "option not valid";;
esac
Hopefully the example above shows how the case statement works, although you can always read the man page
 
Old 05-07-2006, 02:25 AM   #10
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Mmmm, I will definitely look into this, thanks Disillusionist, bigearsbilly and druuna!
 
Old 05-07-2006, 03:48 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Sorry, there was a flaw with my case statement, it should have read:

Code:
read H1
clear

case $H1 in
    1) echo "You chose option 1";;
    2) echo "You chose option 2";;
    3) echo "You chose option 3";;
    4) echo "You chose option 4";;
    5) echo "You chose option 1 as well as"
       echo "option 2"
       echo "option 3"
       echo "option 4";;
    *) echo "option not valid";;
esac

I must remember to check syntax before posting
I must remember to check syntax before posting
I must remember to check syntax before posting
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with if statement in bash script crewblunts Programming 3 03-11-2006 01:38 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM
bash statement os2 Programming 2 03-20-2005 11:13 PM
bash if statement question xscousr Programming 3 09-02-2003 12:58 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration