conversion to interger


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conversion to interger
# 1  
Old 03-20-2005
conversion to interger

Hello,

I want to convert a string to interger.
Quote:
for i in $1 $2 $3 $4
do
var=int ($i)
if test $var -gt 255
then
echo INVALID
exit 0
fi
but its not working..
can anyone help me

thanks in advance

esham
# 2  
Old 03-20-2005
There is no "int" function in bash/ksh, and even if there was, that syntax is incorrect in the way you're invoking the "function" and in the way you're trying to assign to the variable. The correct method would be, assuming an "int" function existed, to do something like

foo=`int $i`

Anyway, back to the matter at hand, something like this should get you started in ensuring that the input is an integer

Code:
#!/bin/ksh

for i in "$@"
do
  echo "$i" | grep '^[0-9]*$' >/dev/null 2>&1
  if [ "$?" -ne "0" ]; then
     echo "$i Not an integer"
  elif [ "$i" -gt "255" ]; then
     echo "$i Invalid"
  else
     echo "$i okay...."
  fi
done
exit 0

Modify that to suit your needs.
A sample run....
Code:
$ ./testint.ksh 1 2 3 10.1 abcd
1 okay....
2 okay....
3 okay....
10.1 Not an integer
abcd Not an integer

One more thing, if you're going to add exit codes, it doesn't make sense to return an exit code of zero after some invalid action is performed. Always return a non-zero exit code if your script exits as the result of some error condition.

Cheers
ZB
# 3  
Old 03-20-2005
You can type your variable as an integer using the command typeset -i. Example: typeset -i FOO. Now the shell will expect FOO to be set with an integer. UNIX will throw an error if you attempt to set FOO to any non-integer. Capture $? and evaluate it for non-zero status. Also, be sure to discard any output error messages to /dev/null during the assignment. If $? is non-zero, FOO was not set with an integer.

typeset -i FOO

read FOO 2>&1 /dev/null
ERR=$?

if [ $ERR -ne 0 ]
then
echo "Integer input expected"
else
echo "Thanks for the integer"
fi
# 4  
Old 03-21-2005
awk '{for(i=1;i<=NF;i++) print int($i)}'

then strings will be convert into zero.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern to match decimal number and interger

Hi, i've a code if (($A && ((!($A =~ /^\d+$/))))) { -- -- not a number } else { --- its number. } which matches for integer value, i need to modify that pattern where it matches decimal number with 2 decimal points and also integer value. for eg: values 10, 10.00. 0.1, 1 , 3 must... (2 Replies)
Discussion started by: asak
2 Replies

2. UNIX for Advanced & Expert Users

.so to .sl conversion ?

Hi all, I have one libxxx.so file ( which I got from a third party ). We use shared library libxxx.sl . Is there any way to convert the .so file to .sl file ? Thanks in advance - M (3 Replies)
Discussion started by: kanu_kanu
3 Replies

3. Shell Programming and Scripting

json_decode conversion

Hi, I have a variable which contains json string ex : temp=`curl -X GET http://localhost:5984/example/$id` now temp contains =>... (3 Replies)
Discussion started by: shams11
3 Replies

4. Shell Programming and Scripting

Help in conversion ......

Hi, I have a requirement to capture file time stamp and compare with current system time. I am using HP-AUX K-shell. Below is what i have done Getting current date into myfile2 --------------------------------- date +%Y%m%d%H%M%S > myfile2 20091110132800 Getting the file date into... (5 Replies)
Discussion started by: chinniforu2003
5 Replies

5. Shell Programming and Scripting

Conversion

How to convert Nov 10 14:20 to YYYYMMDDHHMMSS in unix I am using K-shell HP-AUX (1 Reply)
Discussion started by: chinniforu2003
1 Replies

6. Shell Programming and Scripting

Date conversion

Hi I want to convert MAY 05 2005 01:15:00PM date format to 2005/05/05 01:15:00PM . CAn somebody suggest me a code ,I am new to unix shell programming. Thanks Arif (21 Replies)
Discussion started by: mab_arif16
21 Replies

7. Shell Programming and Scripting

conversion

hi all i have a file like 151125 25252 2452567 253464576 255 i want this file to be like '151125','25252','2452567','253464576','255' please help thanks (3 Replies)
Discussion started by: infyanurag
3 Replies

8. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

9. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies

10. UNIX for Advanced & Expert Users

conversion

Dear friends, i am writing sh shell script I have a file containing binary data. like this. 010101010101010101101010101010100001010101010101001. i want to read some particular bits and convert it into decimal valuse. example. 1.first i want to read 5 bits and convert it into... (1 Reply)
Discussion started by: rajan_ka1
1 Replies
Login or Register to Ask a Question