I need to check the existence of an input argument. I have the following script:
if [ "$1" -gt "-1" ]
then echo hi
fi
I get
[: : integer expression expected
How do I check the input argument1 first to see if it exists?
|
It is:
EDIT Or you can check if an argument is an empty string or not like:
The |
|||||||||||||||||||||
|
It is better to demonstrate this way
You normally need to exit if you have too few arguments. |
|||||
|
Another way to detect if arguments were passed to the script:
Note that In order to exit in the absence of any arguments, one can say:
Another (analogous) way to say the above would be:
|
||||
|
If you're only interested in bailing if a particular argument is missing, Parameter Substitution is great:
In some cases you need to check whether the user passed an argument to the script and if not, fall back to a default value. Like in the script below:
Here if the user hasn't passed
|
||||
|
Try,
Thanks. |
|||||||||||||||||
|
as a small reminder the numeric test operators in bash only work on integers (-eq/-lt/-ge etc) I like to ensure my $vars are ints by
before I test them, just to defend against the "[: integer arg required" error |
|||
|