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:
The 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. |
|||||||||||||||||
|
If you're only interested in detecting 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
|
||||
|
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:
|
||||
|
Try:
|
|||||||||||||||||||||
|
As a small reminder, the numeric test operators in Bash only work on integers ( I like to ensure my $vars are ints by
before I test them, just to defend against the "[: integer arg required" error. |
||||
|