[mdlug] UNIX tips: Learn 10 good UNIX usage habits

Aaron Kulkis akulkis3 at hotpop.com
Sun Mar 9 18:06:46 EDT 2008


Raymond McLaughlin wrote:
> Aaron Kulkis wrote:
>>    Level: Intermediate
>>
>> Michael Stutz (stutz at dsl.org), Author, Consultant
>>   12 Dec 2006
>>
>> Adopt 10 good habits that improve your UNIX command line efficiency 
> <Snip>
> <http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html?ca=dgr-lnxw06tenunixtips&S_TACT=105AGX59&S_CMP=GR>
> 
> This looks pretty good, but isn't quite as clear as it could be. For
> instance, after introducing: the "&&" and "||"  control operators we are
> given this, without explanation
> 
>   Listing 7. A combined example of good habit #3: Combining commands
> with control operators
>   ~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && tar xvf -C tmp/a/b/c
> ~/archive.tar




> 
> So does execution stop if the cd command, or just skip the mkdir command
> and move on to the tar command? I would guess the later is what would be
> desired, but a sentence stating so would remove such guess work.
> 


It has to do with logical shortcuts

In a logical OR expression ( || ), if the first term is true
(successful execution with no errors), then expression MUST
be true, and the second term doesn't need to be evaluated
(executed)... so it isn't.

For two commands, cmd1 and cmd2

        cmd 2
   || | T | F |
c ---+---+---+
m  T | T | T | <= cmd1 true, so expression is true.
d ---+---+---+
1  F | T | F | <= 1st cmd false, expression is indeterminate.
   ---+---+---+
so

cmd1 || _cmd2_to_run_only_if_cmd1_fails_


For
command1 || command2
read as: Do command1 OR (if you couldn't) do command2


Similarly for logical AND ( && ), if the first term is false,
the whole expression is false, and the second term doesn't
need to be evaluated (executed).

        Cmd 2
   && | T | F |
C ---+---+---+
m  T | T | F | <= 1st cmd true, expression is indeterminate.
d ---+---+---+
1  F | F | F | <= 1st cmd false,  so expression is false
   ---+---+---+

cmd1 && _cmd2_to_run_only_if_cmd1_succeeds_

for
command1 && command2
read as: do command1 AND (if successful) continue with Command2



The "ultimate" example here

cmdA && _cmdB_to_run_only_if_cmdA_fails || 
cmdC_to_run_only_if_either_cmdA_or_cmd_B_succeeds


> Also, the next listing doesn't work as advertised, at least not in BASH
> on my system (OpenSuSE 10.3).
> 
>   Listing 8. Example of good habit #4: Quoting (and not quoting) a variable
> 
>   ~ $ ls tmp/
>   a b
>   ~ $ VAR="tmp/*"
>   ~ $ echo $VAR
>   tmp/a tmp/b
>   ~ $ echo "$VAR"
>   tmp/*
>   ~ $ echo $VARa
> 
>   ~ $ echo "$VARa"
> 
>   ~ $ echo "${VAR}a"
>   tmp/*a
>   ~ $ echo ${VAR}a
>   tmp/a
> 
> This all works the same for me, except that last one. On my system the
> last two do the same thing:
> 
>   pts/1 $ echo "{$VAR}a"
>   {tmp/*}a
>   pts/1 $ echo {$VAR}a
>   {tmp/*}a
>   pts/1 $
> 
> I don't know if the author used a different shell, or has different
> globbing options set, but I don't see just how that last one is even
> supposed to work.

probably the file globbing setting.

> 
> This article seems worth the read, but seems to need a little work.
> 

Yeah, it's not quite as clear as it should be....

And the author really should have specified that some of
these things are most beneficial in shell-scripting than
for interactive use on the command line.

There's really no need for using || or &&
when using the shell on a commmand line, because you
can immediately see if the first command fails or
succeeds.

On the other hand, when giving helpful commands on a
mailing list... the && and || tools can be useful,
rather than "and if the directory isn't present, then
run mkdir...   to create it"

> Regards
> Raymond McLaughlin




More information about the mdlug mailing list