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


Search this Thread
Old 09-14-2012, 03:50 AM   #1
LQ Newbie
Registered: Oct 2006
Location: Sofia, Bulgaria
Distribution: Slackware
Posts: 13

Rep: Reputation: 0
Bash script integer value problem [SOLVED]

As a newbie in a shell scripting I have the following problem:

I am writing a bash script to read from file line by line. The problem is that I need to check every line form this file for some condition, and because the file is changing its length in time (log file for example) the number of the rows in it varies.
So I basically want to get the number of lines and add it in a "for" cycle, but it always gives an error, because it takes the number as a string and not as an integer value.
So the only thing I can't figure out how to do is to make the "for" cycle take the value as an integer in some way so it can process it.

Any help is greatly appreciated.

Thanks in advance!

Here is a sample of the code:
# get the number of rows in the file and print them on the screen
wc -l $FILE > $FILE2
cat $FILE2 | awk 'BEGIN { FS = " " } { print $1 } END { }'

# trying to enter manually the number of rows as an integer
echo -n "Enter the number above > "
read NUM1

# doing the for cycle
for i in {1..$NUM1}
if <read the $i line from file and check if something exists>
<do something>
<do other thing>

Last edited by luboiron; 09-14-2012 at 05:42 AM. Reason: problem solved - update tilte
Old 09-14-2012, 04:00 AM   #2
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,841

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
wc -l $FILE will itself print the number of lines
instead of: cat $FILE2 | awk '...' you can write awk '...' $FILE2
END { } can be ignored
If you want to combine them together you can also write: wc -l | awk 'BEGIN { FS = " " } { print $1 }'

But a single awk would be sufficient without anything else:
awk '
# $0 contains the current line
if # do some check
will work too
1 members found this post helpful.
Old 09-14-2012, 04:13 AM   #3
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Posts: 1,355

Rep: Reputation: 135Reputation: 135
In your for cycle you can mention like this:

for ((i=1;i<=NUM1;i++))
1 members found this post helpful.
Old 09-14-2012, 05:41 AM   #4
LQ Newbie
Registered: Oct 2006
Location: Sofia, Bulgaria
Distribution: Slackware
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks a lot, divyashree, your suggestion done the trick!


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
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 06:01 AM
[SOLVED] Bash Shell Script - Store a variable as a string not an integer RML1992 Linux - General 8 09-12-2012 09:19 AM
Test for integer in BASH minil Programming 18 02-01-2012 12:45 AM
bash scripting and integer variables. stf92 Linux - Software 1 06-06-2010 06:30 PM
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM

All times are GMT -5. The time now is 11:29 AM.

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