LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 12-27-2005, 09:34 AM   #1
crazygyhrous
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Rep: Reputation: 0
How do I use an If statement within a case in a Shell script??


Hello, I need to read a password and validate it. Then execute a code. Can someone explain me how to do this??
Please help. Thanks.

#!/bin/bash
tempfile=/tmp/name0
dialog --passwordbox "Pass:" 10 50 2>$tempfile
val=$?
case $val in
0) pw='cat $tempfile' (here i want to check if pass in file equals 'dummy'. If successful then open another dialog else exit)
1) exit
255) exit
esac

thanks!
 
Old 12-27-2005, 09:47 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,386

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
it's no different from any other case statement, case will execute everythign between the opening mathc and a double semi colon at the end:

case $1 in
1) echo hello;;
2) if [ "$2" = "up" ];
then echo upptiy up up;
else echo downity down down;
fi;;
3) echo goodbye;;
esac
 
Old 12-27-2005, 09:48 AM   #3
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Rep: Reputation: 15
maybe i'm stupid, but why not to do

#!/bin/bash
tempfile=/tmp/name0
dialog --passwordbox "Pass:" 10 50 2>$tempfile
val=$?
case $val in
1) exit
255) exit
esac
pw='cat $tempfile' (here you want to check if pass in file equals 'dummy'. If successful then open another dialog else exit)

you can also make 'exit $val'

Last edited by thelonius; 12-27-2005 at 09:49 AM.
 
Old 12-27-2005, 11:25 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/bin/bash

tempfile=/tmp/name0
dialog --passwordbox "Pass:" 10 50 2>$tempfile
val=$?
case $val in
	0)  pw=$(cat $tempfile)
		if [ "$pw" = "dummy" ] ; then
			pwok=yes
		fi
		;;
	1|255) exit
esac

if [ $pwok ] ; then
	echo "Password OK"
else
	echo "Wrong password"
fi
 
Old 12-28-2005, 10:08 AM   #5
crazygyhrous
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you guys (especially Hko)!! I had to add some modifications to it but it worked at the end.
Btw, can any of you tell me if it's possible to add routines on scripts??
i.e. case $val in
0) pw=$(cat $tempfile)
if [ "$pw" = "dummy" ] ; then
(invoke routine_A)
fi
(add an else here)
1|255) exit
esac

routine_A(){
...
}
exit

Can I have routines in here like that??
Sorry, I'm new to shell scripting.
 
Old 12-28-2005, 10:37 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,386

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
May i suggest you just look at the bash scripting howto at tldp.org? there's no benefit in us just telling you it all piece by piece... but yes, you can add functions all you want just do "functionname(parameters) { ... }" to define them, and call them like any other command from wherever you want.
 
Old 12-29-2005, 06:29 AM   #7
crazygyhrous
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
The reason why I'm asking is because I have several functions within my script but for some reason it executes them even if the conditions of the case statement aren't met. I don't know why
Do I have to put exit at the end of the script and then define the functions?
I will look at that website though. Thanks for the tip.
All your inputs are helping a lot!!
 
Old 01-03-2006, 06:41 AM   #8
crazygyhrous
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
I got the code to work! Thanks to all.
 
  


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
problematic case statement alaios Programming 4 08-24-2005 09:05 AM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 10:38 AM
bash'ed by case statement??? 3inone Programming 2 04-29-2004 04:52 PM


All times are GMT -5. The time now is 06:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration