How do I use an If statement within a case in a Shell script??
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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)
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.
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.
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!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.