LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-10-2003, 01:49 AM   #1
d-fens
LQ Newbie
 
Registered: Jul 2003
Location: Friedrichshafen, Germany
Posts: 9

Rep: Reputation: 0
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
 
Old 07-10-2003, 09:14 AM   #2
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
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. . .
 
Old 07-10-2003, 11:26 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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. (=
 
Old 07-10-2003, 12:06 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
 
Old 07-10-2003, 02:59 PM   #5
inkedmn
LQ Newbie
 
Registered: Sep 2001
Location: Southern California, USA
Distribution: Debian unstable :)
Posts: 22

Rep: Reputation: 15
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
 
Old 07-11-2003, 08:38 AM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to check root user? kushalkoolwal Programming 4 09-22-2005 12:15 AM
How to check ICMP code through shell script? Thakowbbery Linux - Networking 2 07-19-2005 09:52 AM
Shell Script to Check apache installed or not. apt Programming 7 12-06-2004 11:07 PM
Shell script ip address format check. rooch84 Linux - Software 6 08-18-2004 09:14 AM
how to run a shell script type program redragon7964 Linux - Newbie 1 04-06-2004 02:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration