LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 11-02-2009, 09:54 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
Shell Script (Yes it is a homework question)


Code:
#!/bin/bash
echo What is your name?
read name

case $name in
 [Ss]haun  ) echo That is my name too!;;


esac
echo $name "is a nice name";
The preceding code is supposed to ask what the user name is, if it the same as mine, it receives the correlating statement. If not it is supposed to print out "'name' is a nice name".

The problem is that when I enter my name it prints out "that is my name too!" and "is a nice name". It should only perform the first statement; "that is my name too!".

I realize I am as green as you get not to get the syntax on this, but any help would be great. I did all of the work, I am just missing how to break the statements.

Blessings,
Shaun
 
Old 11-02-2009, 10:03 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Modify your case ..

case $name in
matchthis )
do this
;;
* )
do this if nothing matches
;;
esac
 
Old 11-02-2009, 10:04 PM   #3
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
Your problem is that the second echo statement is completely unconditional, it exists outside of the case statement completely.

Bah beaten to it, it seems and with a specific example of how to do it.

Last edited by r3sistance; 11-02-2009 at 10:05 PM. Reason: bah beaten to it
 
Old 11-02-2009, 10:05 PM   #4
scottro11
Member
 
Registered: Jun 2009
Location: NYC
Posts: 262

Rep: Reputation: 59
Ok, read name and case name in, you get that part right? If not, post again.

Now, you've done esac. (case spelled backwards, get it?) So, what does that mean?

I think that you're confusing esac with exit.

This stuff is very confusing at first, but will soon be clear to you. This one is fairly simple, which is why I make that statement.

First, case stuff. End the case stuff. The script is still going. So it's just going to enter $name is a nice name, regardless of what happens, because it's not part of the case--it happens after you've ended the case portion.


Have they covered *) as part of a case statement? They probably did. What do you think that would mean?

Sorry to not give the complete answer, but as you say it's homework, I'm trying to do it more as a lesson. I *think* I've given you enough clues.
 
Old 11-02-2009, 10:09 PM   #5
scottro11
Member
 
Registered: Jun 2009
Location: NYC
Posts: 262

Rep: Reputation: 59
Heh, yeah, I knew I was taking too long to type. Ok, so much for the lesson, dxqcanada has explained the *)---if nothing matches.

@dxqcanada, that's not a complaint, it's simply saying that you've given a good explanation.

The important thing is that you understand why dxqcanada's example works.

As I said, it all seems confusing at first, so feel free to post again if the examples and explanations aren't clear. Being able to do this one thing isn't the important thing, it's understanding why it works---then you can use it to write far more complex examples.
 
Old 11-02-2009, 10:11 PM   #6
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
I was trying not to give away the exact answer ... but it was difficult not to give away an example.
 
Old 11-02-2009, 10:14 PM   #7
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
As Scottro11 stated ...

your code ran through the case section ... then completed that section (esac) ... then the last command is run.
 
Old 11-02-2009, 10:17 PM   #8
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by r3sistance View Post
Your problem is that the second echo statement is completely unconditional, it exists outside of the case statement completely.

Bah beaten to it, it seems and with a specific example of how to do it.
No, I really appreciated your way. I had to actually figure it out, but you pointed me in the right direction. You get the thanks!

No offense to the other girl/guy, I dont want you to give me the fish. I want you to teach me to fish!

I corrected it and placed the code back in the case statement.

#!/bin/bash
echo What is your name?
read name

case $name in
[Ss]haun ) echo That is my name too!;;
$name "is a nice name";;

esac
 
Old 11-02-2009, 10:20 PM   #9
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Quote:
Originally Posted by smturner1 View Post
No, I really appreciated your way. I had to actually figure it out, but you pointed me in the right direction. You get the thanks!
I concur.
 
Old 11-02-2009, 10:25 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,989

Rep: Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217
I'd suggest you get the Advanced Bash scripting guide from here.
Great learning tool, and can certainly be used by beginners - the "advanced" is misleading.
 
Old 11-02-2009, 10:55 PM   #11
scottro11
Member
 
Registered: Jun 2009
Location: NYC
Posts: 262

Rep: Reputation: 59
There's a lot of good beginner shell scripting books and guides out there. One that I always liked http://www.freeos.com/guides/lsst/

I actually prefer it, for beginners, to the one that syg00 recommends. It gives more "why".
 
Old 11-02-2009, 11:11 PM   #12
niraj_india
LQ Newbie
 
Registered: Oct 2005
Location: delhi
Posts: 5

Rep: Reputation: 0
Thumbs down solution

you should modify your script using this statement


case $name in
matchthis )
do this
;;
* )
do this if nothing matches
;;
esac






NIRAJ KUMAR SINHA
RHCE(Redhat Certified Engineer)
LINUX ADMINISTRATOR
 
  


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
Shell Script Question ytnexus Linux - Newbie 3 06-28-2009 10:22 PM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 01:51 PM
Shell Script Question abdul_zu Programming 7 10-06-2005 04:33 AM
Shell script question. pete1234 Programming 10 09-06-2005 03:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:52 PM.

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