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.
Ok I am working on a bash script using else if statements, problem is I want an else if and the else to execute another else if and want to know if that is proper or what? I am coming from windows so I am learning this stuff and some things that are easy in windows just doesn't transfer.
Code:
read -p "Do you want to backup programs?(Y/n)" bck
if [ $bck = n -o $bck = N ]; then
echo Programs will not be Saved
else
echo Backing up Programs
fi
if [ -d /sdcard/apk ]; then
echo
else
mkdir /sdcard/apk
fi
echo
cp /system/app/* /sdcard/apk
On the first else if statement if the user selects no then I want it bypass the second else if statement and continue with the script.
Yes, you can nest if statements. And I'm glad to see that you're
using indentation well. It becomes especially important once you
start nesting loops and if statements.
For example:
Code:
if [ sometest ]; then
if [ secondtest ]; then
echo "I passed both tests!"
else
echo "I passed the first test only"
fi
else
if [ `whoami` = root ]; then
echo "Too bad"
elif grep `whoami` /etc/group | grep ^sudo: > /dev/null; then
echo "Try using sudo"
else
echo "Too bad"
fi
fi
My second question is how far can I take these else if statements. And whether they are proper once I get a good script going or would it be better to make other scripts and "call" these scripts based on the users choices?
Last edited by dmathwizard; 06-11-2011 at 07:12 AM.
Reason: rephrasing
I would congratulate you if you manually type (without copying
and pasting) enough nested if statements that it would break
anything. I think for most computers you would run out of
system memory before you'd reach any limit in the shell.
This is considered the right way to handle the recurring else
if statements.
I think I must be missing something here? There are no else if (or elif) statements in your example.
Yes, the line you're missing is this:
elif grep `whoami` /etc/group | grep ^sudo: > /dev/null; then
Of course it is possible to add multiple elif statements in a row.
I just threw together a quick example to show nesting and one
instance of an elif statement.
if [ -d /sdcard/apk ]; then
echo
else
mkdir /sdcard/apk
fi
This if/then construct can be condensed in a single statement:
Code:
mkdir -p /sdcard/apk
The -p option is necessary if both the directories /sdcard and /sdcard/apk don't exist. Also it prevents any error message if the directory already exists (no need to check for its existence).
Awesome!! Thank you everyone. Everything works as planned and you guys are helpful.
@grail
Quote:
Maybe if you illustrate how you would 'easily' do this in Windows we can give you the equivalent?
In windows I can use GOTO LINE statements or for my later question I can make multiple ex1.cmd and ex2.cmd files. And when the user makes their choice ex.cmd will call ex1.cmd run its script and at the end ex1.cmd it will call ex.cmd which is the menu.
Btw I used cmd files instead of vbs because I never know what a user will be running and its just simpler.
if [ -d /sdcard/apk ]; then
echo
else
mkdir /sdcard/apk
fi
This if/then construct can be condensed in a single statement:
Code:
mkdir -p /sdcard/apk
The -p option is necessary if both the directories /sdcard and /sdcard/apk don't exist. Also it prevents any error message if the directory already exists (no need to check for its existence).
Thats awesome thank you. I still have a lot to learn. I know in Windows the less lines and if commands are abbreviated the script goes faster. Linux is already fast enough.
I know in Windows the less lines and if commands are abbreviated the script goes faster. Linux is already fast enough.
It is possible to optimize scripts for speed, but generally I don't
bother doing that. The main exception to this is if a script takes
a long time to run and/or if some part of the command is repeated
thousands of times.
Most of my scripts are written for convenience, so my primary
concern is that they are easy to understand and easy to update
6 months from now when I don't remember the details!
for my later question I can make multiple ex1.cmd and ex2.cmd files. And when the user makes their choice ex.cmd will call ex1.cmd run its script and at the end ex1.cmd it will call ex.cmd which is the menu.
This is the same in linux in that you can create multiple scripts and then call the appropriate one based on user input.
As for GOTO ... I seem to remember in some dark recess an instructor at Uni failing any assignment using this construct
Also consider using functions, particularly if you find yourself repeatedly duplicating the same commands. Of course, you'll still have to use if-else structures, but it will still help you keep your code clean and efficient.
If you end up making large trees of if else statements, maybe you're using the wrong structure or missing an easier way to do it. Remember there is also the case statement, which can help sometimes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.