LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-11-2011, 06:55 AM   #1
dmathwizard
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Rep: Reputation: Disabled
Else if Statement


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.
 
Old 06-11-2011, 07:07 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
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
 
1 members found this post helpful.
Old 06-11-2011, 07:07 AM   #3
dmathwizard
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-11-2011, 07:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I think I must be missing something here? There are no else if (or elif) statements in your example.
Quote:
I am coming from windows so I am learning this stuff and some things that are easy in windows just doesn't transfer.
Maybe if you illustrate how you would 'easily' do this in Windows we can give you the equivalent?
 
Old 06-11-2011, 07:13 AM   #5
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
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.
 
Old 06-11-2011, 07:16 AM   #6
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Quote:
Originally Posted by grail View Post
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.
 
Old 06-11-2011, 07:22 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
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).
 
1 members found this post helpful.
Old 06-11-2011, 07:26 AM   #8
dmathwizard
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-11-2011, 07:30 AM   #9
dmathwizard
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
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.
 
Old 06-11-2011, 07:37 AM   #10
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Quote:
Originally Posted by dmathwizard View Post
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!
 
1 members found this post helpful.
Old 06-12-2011, 03:37 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
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
 
Old 06-12-2011, 04:44 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

http://tldp.org/LDP/Bash-Beginners-G...l/chap_11.html
 
Old 06-12-2011, 09:09 AM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Just remember that if statements work like any other command. This, for example is valid:

Code:
if if false; then true; else false; fi
then
    if true
    then
        echo "true"
    fi
else
    echo "false"
fi
It prints "false".

Last edited by MTK358; 06-12-2011 at 09:10 AM.
 
Old 06-12-2011, 09:15 AM   #14
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
  


Reply



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
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
for statement WT* malikah Programming 7 07-11-2008 02:15 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 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
Open Source Consulting | Domain Registration