I need to check the number of input parameters in the K shell script.
Also, I want to exit if the no of parameters is not met.
How can I do that ?
Thanks
LS1429
I need to check the number of input parameters in the K shell script.
Also, I want to exit if the no of parameters is not met.
How can I do that ?
Thanks
LS1429
I think I see what you mean... For example, the command:
myscript.ksh -a 1 -q
has 3 input parameters... Am I correct? If so, you can use $# to count them. Here is an example:
#!/usr/bin/ksh
if [ "$#" = "0" ]; then
echo "You don't have any arguments! "; exit
fi
echo $#
To try this script, try this:
chmod +x scriptname
./scriptname
./scriptname 1 b 3 d
See what happens.
If you have any more questions, please feel free to post back. I hope I got what you were asking.
Yeah thatz it.
One more thing. But I am not clear why the ; after condition is really needed or not
if [ $# -ne 1 ]
then
echo "Provide one Parameter "
exit 1
fi
Thanks
LS1429
Take an easier example, the date and who commands. If we want to run both commands, we might use two lines:
date
who
or we can enter both commands on a single line if we use the semicolon like this:
date ; who
But we gotta have something separating the two commands. Usually we have the newline character as the delimiter between commands, but the semicolon also works.
So it's a matter of style whether you use
if [ ... ] ; then
or
if [ ... ]
then
I like the first style because it lets more of the script be visible on a page. But I wouldn't call the second style "wrong".