1824945 Members
3795 Online
109678 Solutions
New Discussion юеВ

Re: typeset question?

 
lnl
Occasional Advisor

typeset question?

hello experts,

Can you advice why I can not echo the full value of $input?

typeset -i input

print "Enter numbers? \c"
read input > /dev/null 2>&1
echo $input (it returns 0 ??)

10 points for best solution!
15 REPLIES 15
RAC_1
Honored Contributor

Re: typeset question?

You will have tocombine the lengthof inputin setting typeset -i input.

man sh-posix
There is no substitute to HARDWORK
lnl
Occasional Advisor

Re: typeset question?

Any quick example please.
Thanks
RAC_1
Honored Contributor

Re: typeset question?

I do not have machinehandy at the moment.
But as i recall, there is option to set the lengthofa variable set with typeset.

Something like like typeset -i10 input.

This will acceptonly numbers less than or eqalto10 digits.
There is no substitute to HARDWORK
Vincent Fleming
Honored Contributor

Re: typeset question?

Hmmm...

I haven't an HP-UX box to try it on, but your script works fine on cygwin ksh and Linux bash.

By the way, the argument, -i, allows you to specify what BASE the number will be, not it's length. The default should be base10.

How big of a number are you trying? I suspect that it needs to be no bigger than 31 or 32-bit numbers, and probably is very platform dependent.
No matter where you go, there you are.
john korterman
Honored Contributor

Re: typeset question?

Hi,

try inserting the path to the shell interpreter, e.g.:

$ cat ./readme.sh
#!/usr/bin/sh
typeset -i input
print "Enter numbers? \c"
read input > /dev/null 2>&1
echo "$input"


If your script always returns 0, it sounds as if it does not accept any input at all. However, it works well over here, e.g.:

$ ./readme.sh
Enter numbers? 2147283648
2147283648

but if you try numbers bigger than the above, 2^32-1, the result of the operation is undefined.

regards,
John K.

it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: typeset question?

Hi:

Perhaps you want :

typeset -Z input

...instead of reading the input into a variable typeset with '-i'. The '-Z' says right-justify and fill with leading zeros.

Beware of doing arithmetic with leading zeros. if UNIX95 is set, for instance, a leading zero signals octal and a leading 0x signals hexadecimal base.

Regards!

...JRF...
lnl
Occasional Advisor

Re: typeset question?

Guys,

I think it's my fault that I didn't mention the number I'm trying to echo is in a form of octets. xx.xx.xx.xx? The $input variable always returns zero?
but if I enter numbers with no dots it echos correctly but that's not what I want anyway?

James,

"typeset -Z input" accept alphanumeric varialbls? I want it to accept numbers only with dots between them.


john korterman
Honored Contributor

Re: typeset question?

Hi,

you cannot do that by just defining af variable; you have to use other tools, e.g.:

#!/usr/bin/sh
typeset -Z input
print "Enter numbers? \c"
read input > /dev/null 2>&1
CHECK1=$(echo "$input" | tr -d \.)
CHECK2=$(echo "$CHECK1" | tr -d [:digit:])
if [ "$CHECK2" = "" ]
then
echo "$input"
else
echo illegal input
fi
~
regards,
John K.~
it would be nice if you always got a second chance
Patrick Wallek
Honored Contributor

Re: typeset question?

You can not use 'typeset -i' with the type of number you are entering. '-i' means integer and you are not entering an integer value.

You must rethink your use of typeset in this case. You may just want to omit typeset altogether.
RAC_1
Honored Contributor

Re: typeset question?

typeset -i is for intergersonly. You are entering the intergers. Just read something will do.
There is no substitute to HARDWORK
Bill Hassell
Honored Contributor

Re: typeset question?

As mentioned, typeset -i is for a single integer. When you entered the dots for an IP address, typeset failed because the dot is illegal for an integer. You need to read your input as a string, then parse the string into the 4 numbers. You could also do this with IFS but this is more complex. Do you want the result of the 4 numbers to be converted into an integer?


Bill Hassell, sysadmin
lnl
Occasional Advisor

Re: typeset question?

Bill,

I want it to read numbers only with dots and then echo the full input. That's all.

Any examples?
Bill Hassell
Honored Contributor

Re: typeset question?

You can do this:

print "Enter numbers? \c"
read
echo "$REPLY"

The read statement reads everything on the line and if no variable is specified, then everything is assigned to REPLY. You use a variable with mixed characters by surrounding it with double quotes.


Bill Hassell, sysadmin
Muthukumar_5
Honored Contributor

Re: typeset question?

Simply use without typeset -i input setting.

print "Enter numbers? \c"
read input > /dev/null 2>&1
echo $input

--
Muthu
Easy to suggest when don't know about the problem!
lnl
Occasional Advisor

Re: typeset question?

thanks