Operating System - HP-UX
1827741 Members
3002 Online
109969 Solutions
New Discussion

input has 2 .'s or none, but is an int in either case!

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

input has 2 .'s or none, but is an int in either case!


Relating to this question,
http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0x58fec6af36b7d5118ff10090279cd0f9,00.html

I'm attempting to put the two possible functions into one script and based on input run the appropriate command.

ie

./script 1.1.10
(script recognises this has 2 periods in it and sets VALUE=1

./script 65892
(script recognises this has not got . in it and sets value=2

./script abcd
(script recognised this is not numerical and sets value=3

I've seen posts like this (IP address scripts) and some on linux, but can't find them for the moment.

Best ways to do this?
Later,
Bill
It works for me (tm)
7 REPLIES 7
Santosh Nair_1
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Again, needs to be cleaned up but here's one solution:

-------------cut here --------------
#!/bin/sh

read input
OLDIFS=$IFS
IFS=.
set -- $input
echo $1 $2 $3

if [[ ! -n $2 ]]
then
case $1 in
+([0-9]))
echo "One integer value $1"
break
;;
*)
echo "One non-integer value $1"
break
;;
esac
fi
-------------cut here --------------

-Santosh
Life is what's happening while you're busy making other plans
harry d brown jr
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Would you be willing to write this in perl?
Live Free or Die
Bill McNAMARA_1
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Can't guarentee that perl will be on all systems it'll be run on.
Thanks anyway.

Bill
It works for me (tm)
Robin Wakefield
Honored Contributor
Solution

Re: input has 2 .'s or none, but is an int in either case!

Hi Bill,

This should do it:

#!/bin/sh

VALUE=3
echo $1 | grep -E -q "^[0-9]+\.[0-9]+\.[0-9]+$" && VALUE=1
echo $1 | grep -E -q "^[0-9]+$" && VALUE=2
echo $VALUE

Rgds, Robin.
Santosh Nair_1
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Slight modifications to my original script:

----cut here----
#!/bin/sh

input=$1
OLDIFS=$IFS
IFS=.
set -- $input
let VALUE=1
if [[ ! -n $2 ]]
then
case $1 in
+([0-9]))
VALUE=2
break
;;
*)
VALUE=3
break
;;
esac
fi
echo $VALUE
----cut here----
Life is what's happening while you're busy making other plans
Carlos Fernandez Riera
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Best source scripts for IP manage i have seen never is on SCO systems, now Netware.

All that validations are done by shell scripting.

And for all that like learn shell script programming, try to read installation scripts of drivers on SCO.

Good Luck.
unsupported
Wodisch
Honored Contributor

Re: input has 2 .'s or none, but is an int in either case!

Hello Bill,

even if I am the only one using "case" it still
seems easiest to me that way:

case "$1" in
*.*.*) exit 1;;
*[^0-9]*) exit 3;;
*) exit 2;;
esac


but I am not logged into a UN*X system,
so it might not work...

Regards,
Wodisch