David A. Harding on 19 Aug 2008 04:48:37 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] if command then foo else bar


On Mon, Aug 18, 2008 at 11:35:06PM -0400, JP Vossen wrote:
> At the PLUG West meeting, Toby asked about using a bash 'if' without a 
> 'test', but testing the command instead.  Like:
>
> 	if command; then
> 		foo
> 	elif
> 		bar
> 	fi
> 
> This one bites me every once in a while too, since that's how it 
> "logically" should work.  

That is how it works.  For example:

	$ if false ; then echo "Hello, World" ; else echo "Goodbye, World" ; fi
	Goodbye, World

Or in a script:

	if false
	then
		echo "Hello, World"
	else
		echo "Goodbye, World"
	fi

(Note: false is /bin/false, a GNU Coreutils command.)

Also, set -e can be placed on a bash script's command line. For example:

        #!/bin/bash -e

I also like -u.  You can read an old blog I wrote about both of them at
the following URL:

        http://gnuisance.net/blog/t/2007/bash-eu.html

-Dave
-- 
David A. Harding            Website:  http://dtrt.org/
1 (609) 997-0765              Email:  dave@dtrt.org
                        Jabber/XMPP:  dharding@jabber.org
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug