LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-25-2011, 04:33 AM   #1
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Rep: Reputation: 40
Post Scripting if statement question


So I am getting to know the basics of scripts, and I am just playing with it and having it feed me text (question and answers mostly) with echo and doing read name after and later using $name to bring that up. such as
Code:
echo "enter name"
read name
echo "Nice to meet you $name"
My question is:
What if want to have a question with multiple choice?

I have been trying to use if statements like so:
Code:
echo "Do you prefer wendys,mcdonalds,or real food"
read choice
if [ "$choice" = "wendys" ]
  echo "YAY square meat"
    if [ "$choice" ="mcdonalds" ]
      echo "insert mcdonalds joke here"
   fi
fi
But don't think it works without else echo (not sure though), or maybe I am doing something wrong because I keep getting
line 13: syntax error near unexpected token `fi'.

I have searched LQ and google, and added
#!/bin/bash
as my first line, and have tried it with -x argument to see it working, but it still halts at 'fi' and gives me the error.

Last edited by Hevithan; 06-25-2011 at 04:37 AM.
 
Old 06-25-2011, 05:11 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
In bash, the if construct is this way:

Code:
if [ ]; then
  whatever
fi
 
1 members found this post helpful.
Old 06-25-2011, 05:22 AM   #3
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Original Poster
Rep: Reputation: 40
adding ; then after the bracket got rid of the error, Thank you!
 
Old 06-25-2011, 05:25 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Also, may I suggest, as you are processing more than 2 options and neither appears reliant on the other that you investigate the 'case' statement instead of 'if'.
 
1 members found this post helpful.
Old 06-25-2011, 05:43 AM   #5
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by grail View Post
Also, may I suggest, as you are processing more than 2 options and neither appears reliant on the other that you investigate the 'case' statement instead of 'if'.
I'm afraid I don't follow you.

Yes I am processing more then 2 options (EX: Wendy,mcdonalds,other) ... I should state that I have changed it from:
Code:
echo "which do you prefer (choice)"
read choice
if [ 'choice' = 'wendy' ]; then
To a more workable (and usable)

Code:
echo "which do you prefer (choice)"
var=wendys
var=mcdonalds
var=other
if [ 'var=wendys' ]; then
Since it wasn't picking it up with if [ 'choice' ].
 
Old 06-25-2011, 06:04 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Illustration of the case statement:
Code:
echo "Do you prefer wendys,mcdonalds,or real food"
read choice
case $choice in
    wendys )
        <whatever>
        ;;
    mcdonalds )
        <whatever>
        ;;
    mcdonalds )
        <whatever>
        ;;
    'real food' )
        <whatever>
        ;;
    * )
        echo "Invalid option" >&2
esac
It is possible to use read's -p option to write "Do you prefer wendys,mcdonalds,or real food" instead of the echo.
 
1 members found this post helpful.
Old 06-25-2011, 06:08 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Hevithan View Post
Code:
echo "which do you prefer (choice)"
var=wendys
var=mcdonalds
var=other
if [ 'var=wendys' ]; then
The var=other line sets the var variable to other, rendering the previous two lines ineffective.

if [ 'var=wendys' ] tests the string "var=wendys" which will always be true (non-empty strings are true).
 
Old 06-25-2011, 06:09 AM   #8
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Original Poster
Rep: Reputation: 40
Thread marked as unsolved (Though I am still not getting any errors!) due to inability to actually distinguish between answer A and answers B and C.


So I ran it with var=a, var=b, and var=c ... all examples aside, What I am doing is trying to make a short 'game' I guess you would call it, just to get my feets wet. I am at a part where you wake up in a daze, a guy hands you a weapon and you must choose which is yours. either a,b,or c it works with var=a and only out puts A but if I choose C, it gives all three options as having been chosen. here is that part of my script:
Code:
echo "a man walks in holding your weapon a:"
echo "A)Sword B)Bow C)Fluffy Kitty -enter A,B,or C"
var=a
var=b
var=c
read $var
if [ "$var=a" ] ; then
	echo "You have chosen sword"
	if [ "$var=b" ] ; then
		echo "You have the bow"
		if [ "$var=c" ] ; then
  			echo "You have choosen Mr.Bigglesworth"
        	fi
        fi
fi
But like I said, When I enter c

it responds with


You have chosen sword
You have the bow
You have choosen Mr.Bigglesworth
_________________________________

Is something like this possible with script or am I just gonna be chasing my tail? ... I would think it to be possible, and I keep getting closer, But still no cigar.

Thanks for the help guys
 
Old 06-25-2011, 06:21 AM   #9
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
You have two problems in your statements. The first one is that you create a new expression "$var=a" in your first test, this is always true, the same for the other tests. it should be "$var"=a, not "$var=a".
The second problem is that you only test for condition b if a is true (which makes no sense), and you test for condition c only if a and b are true.
Try it this way:
Code:
echo "a man walks in holding your weapon a:"
echo "A)Sword B)Bow C)Fluffy Kitty -enter A,B,or C"
read $var
if [ "$var" = a ] ; then
	echo "You have chosen sword"
fi
if [ "$var" = b ] ; then
	echo "You have the bow"
fi
if [ "$var" = c ] ; then
	echo "You have choosen Mr.Bigglesworth"
fi
Or better with a case structure, as mentioned above.

Last edited by TobiSGD; 06-25-2011 at 07:09 AM. Reason: Fixed errors in script
 
1 members found this post helpful.
Old 06-25-2011, 06:31 AM   #10
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by catkin View Post
Illustration of the case statement:
Code:
echo "Do you prefer wendys,mcdonalds,or real food"
read choice
case $choice in
    wendys )
        <whatever>
        ;;
    mcdonalds )
        <whatever>
        ;;
    mcdonalds )
        <whatever>
        ;;
    'real food' )
        <whatever>
        ;;
    * )
        echo "Invalid option" >&2
esac
It is possible to use read's -p option to write "Do you prefer wendys,mcdonalds,or real food" instead of the echo.
Case statement worked great! set wendy changed whatever to "square meat", and when I chose it while running, LO AND BEHOLD it did what it was supposed to.


Thank you all for the help, It has definitely been fun, and hopefully this information sticks.
 
Old 06-25-2011, 06:31 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Those tests like [ "$var"=a ] need to be like [ "$var" = a ] if they are to work as intended.
 
2 members found this post helpful.
Old 06-25-2011, 07:09 AM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by catkin View Post
Those tests like [ "$var"=a ] need to be like [ "$var" = a ] if they are to work as intended.
Thanks for that hint, corrected.
 
  


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
[SOLVED] Scripting question brodieman01 Programming 8 06-06-2011 06:29 PM
scripting question Jurrian Linux - Newbie 5 10-21-2008 06:15 AM
Scripting question krazykevin Programming 9 03-19-2008 02:40 PM
scripting question. disruptive Programming 5 02-19-2008 02:12 PM
Scripting Question caps_phisto Linux - General 1 11-05-2004 03:43 PM


All times are GMT -5. The time now is 02:39 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