Good News Everybody!
The new search engine is LIVE!
Please report any problems to david (at) midrange.com.
|
Pete,
That code doesn't do what you think it does!!
#!/bin/bash
if [ $1 ] ; then
echo 'Yes!'
else
echo 'No!'
fi
What this is doing is trying to execute $1 as an expression for the
"test" utility to evaluate. If test evaluates to true, it'll print yes,
if not, it'll print no. So this isn't really testing to see if a
parameter is passed! It's evaluating the parameter as an expression!
For example, try this:
$ ./myscript '1 -eq 0'
No!
See what I mean? I *did* pass it a parameter, but it says no. Why?
Because it's running my parameter as an expression. Since 1 is not
equal to 0, it says "No!"
If you just want to test if a parameter is empty, do this instead:
if [ -z "$1" ] ; then
echo 'No!'
else
echo 'Yes!'
fi
I would've given that answer at first if I had known that was what John
was asking. But there are so many things he could've meant...
This mailing list archive is Copyright 1997-2026 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.