× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hans Boldt wrote:
Alternatively, you could just code:

if [ "$1" ] ; then
echo 'No!'
else
echo 'Yes!'
fi


Hans, that was Scott's original objection, except it was like this:

if [ "$1" ] ; then
echo 'Yes!' # $1 has a value
else
echo 'No!' # $1 Does not have a value
fi

I did forget the quotes, and that would matter if there is whitespace in
the value of $1, since it would then be treated as an array.

Scott's objection was that if $1 happens to contain an expression which
evaluates to false, it would behave incorrectly. for instance, if $1
happens to contain "1 = 3", the result would be 'No!'. Without the
quotes, a value with embedded whitespace like '1 2 3', which contains an
array, not an expression will produce an error. With the quotes, it
depends on the shell. Recent bash shells will still produce an error
unless you specify the value as "${1}", in order to cause the potential
array to be evaluated as a single entity.

So I think Scott's suggestion is valid. You're right though that the use
of getopts and shift is much more robust. It unfortunately doesn't
handle the posix (is that right?) version of arguments like --option-name.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.