Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test If Command Line Parameter Not Passed 2

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In a UNIX Bourne shell scipt how can I test if a command line parameter is not passed?

Example (with param): ./myscript "Albatross"
Example (without param): ./myscript

Thanks,

Michael42
 
Check the number of aguments:

Code:
#!/bin/sh

if [ $# -eq 0 ]
then
   echo "argument not passed"
fi
 
If you want to provide a default value:
myArg=${1-Albatross}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Or do you mean a specific parameter was not passed ? Then you need to check the arguments one by one. ( Or someone will tell you a more simple solution using [tt]getopts[/tt]. )

Feherke.
 
Guys,

This is very useful: if [ $# -eq 0 ]

If I could take this a little deeper. How can i test for the 2nd and 3rd command line parameters?

Example: ./myscript one two three


Thanks again,

Michael42
 
Oop that was a bit incomplete - sorry.

More clearly, what is the best way to test if $2 and $3 have no value.

Thanks,

Michael



Thanks,

Michael42
 
[ -z "$2" ] && echo "No 2nd arg !"

Anyway:
man test
man sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Guys,

Thanks again for your advice. Thanks to your help I have been able to cut the number of coding lines in half. :)

Regards,

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top