LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-14-2012, 03:50 AM   #1
luboiron
LQ Newbie
 
Registered: Oct 2006
Location: Sofia, Bulgaria
Distribution: Slackware
Posts: 13

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


Hello,
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:
-----------------------------
#!/bin/bash
# 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}
do
if <read the $i line from file and check if something exists>
then
<do something>
else
<do other thing>
fi
done
-------------------------------

Last edited by luboiron; 09-14-2012 at 05:42 AM. Reason: problem solved - update tilte
 
Old 09-14-2012, 04:00 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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:
Code:
awk '
# $0 contains the current line
if # do some check
...
' $FILE
will work too
 
1 members found this post helpful.
Old 09-14-2012, 04:13 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

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

Code:
for ((i=1;i<=NUM1;i++))
do
.
.
.
done
 
1 members found this post helpful.
Old 09-14-2012, 05:41 AM   #4
luboiron
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!
 
  


Reply



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

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

All times are GMT -5. The time now is 06:43 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