Operating System - HP-UX
1827294 Members
2184 Online
109717 Solutions
New Discussion

ksh script needing an interger cmd. line arg

 
SOLVED
Go to solution
Marc Ahrendt
Super Advisor

ksh script needing an interger cmd. line arg

what is the best way to test that the 3rd command line argument for my ksh script is a positive interger?

./my_script A B C
C needs to be >=0 and an integer




hola
12 REPLIES 12
Paula J Frazer-Campbell
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Hi

if (( $c >= 0 ))
then

fi

Paula
If you can spell SysAdmin then you is one - anon
linuxfan
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Hi Marc,


you could do something simple like

if [ $3 -gt 0 ]
then
echo "3rd argument greater then 0"
fi

-Ramesh
They think they know but don't. At least I know I don't know - Socrates
linuxfan
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Hi Marc,

for >=0 change it to

if [ $3 -ge 0 ]
then
echo "3rd argument greater then 0"
fi

-Ramesh
They think they know but don't. At least I know I don't know - Socrates
Sridhar Bhaskarla
Honored Contributor

Re: ksh script needing an interger cmd. line arg

First I will see if it is an integer or not

echo "$3 + 1 " |bc 2>&1 |grep syntax > /dev/null
if [ $? = 0 ]
then
echo "Your C is some character"
exit
fi

#Once the above is passed, it means it's an integer. Now I will check to see if it is > 0.

if [ $3 -gt 0 ]
then
echo "C should be greater than 0"
exit 1
fi

...code...

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: ksh script needing an interger cmd. line arg

ooooopsss.. Small correction.. Friday evening.

yaada..yadaa..yadaaa..
...
if [ $3 -le 0 ]
then
echo "Arg 3 should be greater than 0"
exit
fi

...code...

You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor
Solution

Re: ksh script needing an interger cmd. line arg

Hi Marc:

Here's one somewhat rigorous way:

#!/usr/bin/sh
#
typeset X=$3
#
[ -z "$X" ] && { echo "no argument provided";exit 2; }
#
[ `expr $X : '[0-9]*'` -ne `expr $X : '.*'` ] && { echo "not a positive integer"
;exit 1; }
#
[ "$X" -le 0 ] && { echo "zero not allowed";exit 1; }
#
echo "OK"
exit 0
#
#.end.

...JRF...
Paula J Frazer-Campbell
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Hi to all

As I am still learning scripting why use "gt" instead of >= ?

Paula
If you can spell SysAdmin then you is one - anon
Sridhar Bhaskarla
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Doesn't matter with POSIX. I personally commit syntax errors if I try the symbols like instead >= I type in => etc., it's more easy to remember gt, ge etc.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Marc Ahrendt
Super Advisor

Re: ksh script needing an interger cmd. line arg

thx for everyone's quick replies

sridhar, your solution does not work reliably for all possible input

james, yours works and is more compact than my crude attempt with grep and regular expressions, but i was hoping there was something built into the shell/test like "if (( -I $3 ))" where I would be that magical test on a number
hola
Sridhar Bhaskarla
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Marc,

Interesting. Did you have any instance where it couldn't get to work?. That will help me rectify my logic.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Marc Ahrendt
Super Advisor

Re: ksh script needing an interger cmd. line arg

sridhar, below succeeds and f is not an interger
echo "f + 5" | bc 2>&1
hola
Sridhar Bhaskarla
Honored Contributor

Re: ksh script needing an interger cmd. line arg

Didn't have a system infront of me now. But I know where it would be missing. Try this out.

....
echo "${3}.1 + 1 " |bc 2>&1 |grep syntax > /dev/null
............

if [ $? = 0 ]

....

Thanks for letting me know.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try