Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries



Links

Issociate
Impressum

#1: "or" in bash if statements

Posted on 2006-02-02 17:16:49 by didds

feel really stoooopid asking this but I've gogled etc to no avail...

how does one put an "or" in a bash shell if statement?

I am trying the following

if [ /etc/passwd -nt timestamp || /etc/shadow -nt timestamp ]; then
echo yes
fi


but receive the error

: [: missing `]'
: /etc/shadow: Permission denied

the if construct works as a single file check...

TIA
ian

Report this message

Mr Ad

Google

#2: Re: "or" in bash if statements

Posted on 2006-02-02 17:24:50 by Icarus Sparry

On Thu, 02 Feb 2006 08:16:49 -0800, didds wrote:

> feel really stoooopid asking this but I've gogled etc to no avail...
>
> how does one put an "or" in a bash shell if statement?
>
> I am trying the following
>
> if [ /etc/passwd -nt timestamp || /etc/shadow -nt timestamp ]; then
> echo yes
> fi
>
>
> but receive the error
>
> : [: missing `]'
> : /etc/shadow: Permission denied
>
> the if construct works as a single file check...

"if" takes a pipeline of commands, so you can do

if [ /etc/passwd -nt timestamp ] || [ /etc/shadow -nt timestamp ]; then
echo yes
fi

This is portable.

Or you can use the "[["/"]]" instead of "["/"]". This is less portable.

Report this message

#3: Re: "or" in bash if statements

Posted on 2006-02-02 17:33:08 by Stephane CHAZELAS

On Thu, 02 Feb 2006 08:24:50 -0800, Icarus Sparry wrote:
[...]
> "if" takes a pipeline of commands, so you can do
>
> if [ /etc/passwd -nt timestamp ] || [ /etc/shadow -nt timestamp ]; then
> echo yes
> fi

It takes a *list* of commands which might be pipelines.

cmd1 && cmd2 || cmd3

may be refered to as a OR-AND list.

cmd1 | cmd2 | cmd3

is a pipeline

>
> This is portable.

Except for "-nt" which is not POSIX nor Bourne.

> Or you can use the "[["/"]]" instead of "["/"]". This is less portable.

Probably as portable as "-nt": ksh/bash/zsh specific.

One can also use:

[ condition1 -o condition2 ]

(-o is then a "[" operator while "||" is a shell operator)
except that it is less reliable.

--
Stephane

Report this message

#4: Re: "or" in bash if statements

Posted on 2006-02-02 18:12:37 by merlyn

>>>>> "Stephane" == Stephane Chazelas <stephane_chazelas@yahoo.fr> writes:

Stephane> It takes a *list* of commands which might be pipelines.

Stephane> cmd1 && cmd2 || cmd3

Eeek. Not that crappy meme again. Please stop spreading that.
It gets used as *if* you're saying:

if cmd1; then cmd2; else cmd3; fi

But consider what happens when cmd2 fails after being selected. cmd3 runs!
Eeek! So that's really not a if/then/else... it's an
if-then-and-then-if-that-works-ok-do-nothing-else-the-other :)

If you want an if/then/else, you know where it is. Stop using the
abbreviated form that breaks when people least expect it.

Maybe I need a "Dangerous Use Of And-Or" award to go with my Useless Use of
Cat awards... :)

Just another Unix hacker (since 1977, yes *77*),

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Report this message

#5: Re: "or" in bash if statements

Posted on 2006-02-02 20:56:21 by Stephane CHAZELAS

2006-02-02, 09:12(-08), Randal L. Schwartz:
>>>>>> "Stephane" == Stephane Chazelas <stephane_chazelas@yahoo.fr> writes:
>
>Stephane> It takes a *list* of commands which might be pipelines.
>
>Stephane> cmd1 && cmd2 || cmd3
>
> Eeek. Not that crappy meme again. Please stop spreading that.
> It gets used as *if* you're saying:
>
> if cmd1; then cmd2; else cmd3; fi
>
> But consider what happens when cmd2 fails after being selected. cmd3 runs!
> Eeek! So that's really not a if/then/else... it's an
> if-then-and-then-if-that-works-ok-do-nothing-else-the-other :)
>
> If you want an if/then/else, you know where it is. Stop using the
> abbreviated form that breaks when people least expect it.
[...]

That's very true, but was not the point. The point was about
terminology.

cmd1 || cmd2

is not a pipeline, but an AND-OR list.

And we were discussing the

if cmd1 || cmd2 && cmd3; then

thing, i.e. the syntax of the if/then/else/fi statement, where
using an AND-OR list makes a lot of sense.

--
Stéphane

Report this message