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 |
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. . .
|
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 Code:
~/shell> ./t.sh |
Re: Shell-Script check if $xx is of type integer
Quote:
There is a nicer way to do this: Code:
cat /home/logs/size | while read size_temp Quote:
Code:
cat /home/logs/size | while read size_temp Code:
#!/bin/sh |
or you could use a python one-liner :)
Code:
import sys |
Quote:
Code:
num=854f353; if [ "${num//[^0-9]/}" == "$num" ]; then echo "Is a number"; fi |
All times are GMT -5. The time now is 03:39 AM. |