Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-10-2003, 01:49 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2003
Location: Friedrichshafen, Germany
Posts: 9
Rep:
|
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
|
|
|
|
07-10-2003, 09:14 AM
|
#2
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
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. . .
|
|
|
|
07-10-2003, 11:26 AM
|
#3
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
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. (=
|
|
|
|
07-10-2003, 12:06 PM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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
|
|
|
|
07-10-2003, 02:59 PM
|
#5
|
|
LQ Newbie
Registered: Sep 2001
Location: Southern California, USA
Distribution: Debian unstable :)
Posts: 22
Rep:
|
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 
|
|
|
|
07-11-2003, 08:38 AM
|
#6
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:03 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|