LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-21-2013, 02:04 PM   #1
paziulek
Member
 
Registered: Mar 2009
Location: N.C.
Distribution: "gentoo|debian|arch"
Posts: 77

Rep: Reputation: 11
if...then...else...fi


Hello,

What is the difference or advantage ( if any ) of checking conditions this way:

Code:
ls &>/dev/null && { echo "error $?"; } || { echo "Error $?"; }
compared to:

Code:
if ls
then
   echo "error $?"
else
   echo "Error $?"
fi
or is it only about what the "keyboard operator" prefers?

tx
 
Old 02-21-2013, 02:16 PM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,156
Blog Entries: 4

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
#2 is easier to read and hence easier to debug should there be one?
 
Old 02-21-2013, 02:37 PM   #3
paziulek
Member
 
Registered: Mar 2009
Location: N.C.
Distribution: "gentoo|debian|arch"
Posts: 77

Original Poster
Rep: Reputation: 11
Hi,

I noticed, that many of the system scripts, Makefiles, init.d scripts using the #1 form,
it seems also that many experienced users prefer #1.

#2 is easier to read, #1 quicker to read ( for some ).

bash is great!


Quote:
Originally Posted by Habitual View Post
#2 is easier to read and hence easier to debug should there be one?
 
Old 02-21-2013, 02:56 PM   #4
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,156
Blog Entries: 4

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
as long as it works, hey?

yes, c-li rocks!
 
Old 02-21-2013, 03:48 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
The main difference between both two forms is the use of different comaparision models i.e. conditional operators and if-else-fi.

While using conditional operators, one or more conditions can be true and execute (specially in kind of example you showed), whereas in case of if-else-fi, only one statement or condition will be executed that will be true.

You can once check this.

Last edited by shivaa; 02-21-2013 at 03:51 PM.
 
1 members found this post helpful.
Old 02-24-2013, 04:39 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
...&&...||... is a common shortcut syntax; quick, easy, and perfectly fine for short, simple succeed-or-fail situations, such as issuing error messages on failed commands. It can also sometimes be good for readability, since you can reduce redundant clutter in your code.

Be aware though that there is a common pitfall involved with this pattern.


For longer, more complex conditionals, or if what's happening can be in any way hard to understand at a glance, it's generally better to stick with the traditional if statement.
 
1 members found this post helpful.
Old 02-25-2013, 11:49 AM   #7
paziulek
Member
 
Registered: Mar 2009
Location: N.C.
Distribution: "gentoo|debian|arch"
Posts: 77

Original Poster
Rep: Reputation: 11
Quote:
Originally Posted by David the H. View Post

Be aware though that there is a common pitfall involved with this pattern.
Thank you - the 'pitfall' example is great - did not realize it till I saw it.

another words:

Code:
i=0
((i++)); echo $?

i=1
((i++)); echo $?
this can hurt....

Every day something new! Thanks again!

Last edited by paziulek; 02-25-2013 at 11:57 AM.
 
  


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



All times are GMT -5. The time now is 03:32 PM.

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