LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell-Script check if $xx is of type integer (https://www.linuxquestions.org/questions/programming-9/shell-script-check-if-%24xx-is-of-type-integer-71479/)

d-fens 07-10-2003 01:49 AM

Shell-Script check if $xx is of type integer
 
Hi all,

does anyone know how to check if a variable is integer in Linux Shell-Script?

-------------
#!/bin/sh
declare -i size
declare -i result
#extract size from actual line of log - $i comes from a loop
size_temp=$(head --lines=$i /home/logs/size | tail --lines=1)
#convert - to 0
if [ "$size_temp" = '-' ]
then
size=0
else
size=$size_temp
fi
result=`expr $result + $size`
--------------

The Problem is that the log-files comtain errors so it happens from day to day that two lines of the log are written into one line and $size for that line gets something like "12545HTTP" and my script stops.

Now if i could check if $size contains a integer value or not i could skip the damaged lines..

Thanks alot,
d-fens

moses 07-10-2003 09:14 AM

I think you can use the declare statement in the assignment statement, and check for an exit status of 0. . . I haven't figured out the syntax or if it's possible. . .

TheLinuxDuck 07-10-2003 11:26 AM

d-fens:

One method you can use is by using pattern matching.
1. Variable is defined that may or may not contain a number and/or characters
2. Scan variable, removing anything that is not a number
3. Compare old and new variables.
3a. If they match, the original is a number.
3b. If they do not match, the original is not a number.

Here is some test code that should help you out.
Code:

#!/bin/bash

for num in 192391 gmkrelgg3 2t4 32 gfsgfsd t43g4 4224 gr3gr3 42
do
  num2=${num//[^0-9]/}

  echo -n "num,num2: $num,$num2: "

  if [ "$num" == "$num2" ]; then
    echo "is a number"
  else
    echo "is not a number"
  fi
done

and the results:
Code:

~/shell> ./t.sh
num,num2: 192391,192391: is a number
num,num2: gmkrelgg3,3: is not a number
num,num2: 2t4,24: is not a number
num,num2: 32,32: is a number
num,num2: gfsgfsd,: is not a number
num,num2: t43g4,434: is not a number
num,num2: 4224,4224: is a number
num,num2: gr3gr3,33: is not a number
num,num2: 42,42: is a number

Of course, this won't work with all numbers (for example, numbers that contain + or - and .'s) but it's prolly good enough for what you need it for. (=

Hko 07-10-2003 12:06 PM

Re: Shell-Script check if $xx is of type integer
 
Quote:

Originally posted by d-fens
#extract size from actual line of log - $i comes from a loop
size_temp=$(head --lines=$i /home/logs/size | tail --lines=1)

Do you have this inside a loop to have size_temp contain one line?
There is a nicer way to do this:
Code:

cat /home/logs/size | while read size_temp
do
# size_temp contains one line each time, until the whole file is processed
done

Quote:

The Problem is that the log-files comtain errors so it happens from day to day that two lines of the log are written into one line and $size for that line gets something like "12545HTTP" and my script stops.

If you want to get rid of text following the numbers, you can do this (assuming there's allways a number at each start of a line):
Code:

cat /home/logs/size | while read size_temp
do
    size_temp=${size_temp%%[A-Za-z]*}
    # size_temp now contains only the numbers at the start of each line
done

or, more profound, use 'sed':
Code:

#!/bin/sh

sed '/^ *[0-9]*/ s/^ *\([0-9]*\).*/\1/' /home/logs/size |
while read size_temp
do
        echo $size_temp    # process the current line, here just echo...
done


inkedmn 07-10-2003 02:59 PM

or you could use a python one-liner :)
Code:

import sys
if type(sys.argv[1]) == type(1): sys.stdout.write("integer")

ok, so two lines counting the import statement :)

TheLinuxDuck 07-11-2003 08:38 AM

Quote:

Originally posted by inkedmn
or you could use a python one-liner :)
Code:

import sys
if type(sys.argv[1]) == type(1): sys.stdout.write("integer")

ok, so two lines counting the import statement :)

Fine..fine.. since you set the bar...
Code:

num=854f353; if [ "${num//[^0-9]/}" == "$num" ]; then echo "Is a number"; fi
;PppPpPp


All times are GMT -5. The time now is 03:39 AM.