ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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..
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
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. . .
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. (=
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.