Bash tip

James Knott james.knott-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Sat Mar 27 22:59:15 UTC 2004


I have discovered a simple method for conditionally running a command, 
depending on if a file exists.

When you want to run a command, when a file exists, the usual form has 
been something along the lines of:

if [ -x aaa ]; then
   bbb
fi

However, I've noticed that you can also use

ls aaa && bbb

The "&&" in the command says to run the second part of the command, only 
if the first part runs successfully.  You could also use "||", if you 
want to run the second part,  only if the first part fails.  Since ls 
uses std out, for success and std err for failure, you can also control 
the output to show one or the other.  For example, if you want to show 
the files that were found, you could use:

ls aaa 2>/dev/null && bbb

There are many variations on the above which may be useful.


jk
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list