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 02-09-2011, 02:26 PM   #1
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
one-liner with && and ||


I recently noticed some of my one-liners with && and || didn't work as I expected so I googled for "bash && || pitfalls" and found this page http://mywiki.wooledge.org/BashPitfalls (#22)

It's probably my fault, but I still don't understand why it wouldn't work as I want it.....

I don't even have the code anymore that wasn't working properly because I rewrote it using if ; then ; command ; fi

Could someone explain me a bit better what I probably did wrong....

I know my question is a bit vague and if I need to rephrase it, no problem.
 
Old 02-09-2011, 03:31 PM   #2
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
Hi,

if expr then...
considers the expression "tested", when
test expr && cmd || cmd
considers only the last command's success or failure.
 
Old 02-09-2011, 05:52 PM   #3
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
I don't think it's the answer to my question....
Let me ask it in a different way....

If I want to do something like this:
Code:
echo 'yy' >/tmp/file1
if grep -q 'xx' /tmp/file1 ; then
  if ! grep -q 'yy' /tmp/file1 ; then
    rm /tmp/file1
  fi
fi
ls -l /tmp/file1
I prefer to write it as a one-liner and I would expect this line to do the same
Code:
echo 'yy' >/tmp/file1
grep -q 'xx' /tmp/file1 && grep -q 'yy' /tmp/file1 || rm /tmp/file1
ls -l /tmp/file1
Somehow this isn't the case and I don't get why not....
Even if there is no 'xx' in the file, the file could still get deleted.

Isn't there a better explanation than the one on that site and yours (it may be my fault that I don't get it)


This one does work the same
Code:
echo 'yy' >/tmp/file1
if grep -q 'xx' /tmp/file1 ; then
  grep -q 'yy' /tmp/file1 || rm /tmp/file1
fi
ls -l /tmp/file1

Last edited by frater; 02-09-2011 at 05:57 PM.
 
Old 02-09-2011, 06:11 PM   #4
sys64738
Member
 
Registered: May 2008
Location: NRW/Germany
Posts: 105

Rep: Reputation: 30
Hi
I'm not sure if I can help you.
What should be the condition when the file is to be deleted?
No "yy" and no "xx" in the file?
Or something else?
 
Old 02-09-2011, 06:23 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If you read the link you posted the information is all there (I know as have recently used it several times myself ), but I will try to explain further.

When you are using the 'if' construct it is very specific in the fact that the 'then' clause or the 'else' clause ONLY run when the expression is true or false in the 'if'.

However, when using the single line entry (&& and ||), you face the issue that ANY false expression will jump to || clause and ANY true expression will jump to &&.
I find the final example to be helpful:
Code:
$ true && { echo true; false; } || echo false
true
false
$ false && { echo true; false; } || echo false
false
$ false || { echo false; true; } && echo true
false
true
$ true || { echo false; true; } && echo true
true
Hope this helps
 
  


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
zsnes && logitech dual action gamepad && dpad woes John5788 Linux - Games 5 10-29-2008 09:56 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
(FreeBSD && Fedora Core 4 && Slackware 10.0) Filesystem Support taylor_venable *BSD 1 07-14-2005 02:24 PM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM

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

All times are GMT -5. The time now is 03:14 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