LinuxQuestions.org
Visit Jeremy's Blog.
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 07-16-2013, 06:52 AM   #1
exithere
LQ Newbie
 
Registered: Jul 2013
Posts: 5

Rep: Reputation: Disabled
[BASH] Read line from a file to use within a script


Hi all,

So I have a script which I'm using to take stats out of a program and write them out to a log file. I have the script working but I would like to enhance it.

So at the moment I have the following code

Code:
get_stats () {
./ggsci >> $STATSLOG 2>&1 <<EOF
stats replicat $REPNAME, latest, table test.table1
exit
EOF
}
I have multiple tables I would like to run this code against so I would like to take out test.table1 and use a variable which would take the table names out of a separate file.

So my code would look like this, but I don't know how to define the $TABNAME..

Code:
get_stats () {
./ggsci >> $STATSLOG 2>&1 <<EOF
stats replicat $REPNAME, latest, $TABNAME
exit
EOF
}
I have a file called tables_list.log which has the following entries

Quote:
test.table1
test.table2
test.tab
test.invoice
test.stats
So I would like my get_stats function to run 5 times then exit, once for each table listed in the tables_list.log file.

Could someone help me out with how to do this? I've only posted a section of my code but I'm happy to post more If it will help.
 
Old 07-16-2013, 07:38 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
Give this a try:

Code:
for TABNAME in $(cat tables_list.log); do
get_stats () {
./ggsci >> $STATSLOG 2>&1 <<EOF
stats replicat $REPNAME, latest, $TABNAME
exit
EOF
}
done
 
1 members found this post helpful.
Old 07-16-2013, 07:49 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I thnik the process suggested is ok, but I would probably place the loop inside the function
 
1 members found this post helpful.
Old 07-16-2013, 09:08 AM   #4
exithere
LQ Newbie
 
Registered: Jul 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks very much, that does exactly what I need.
 
Old 07-19-2013, 05:03 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
for TABNAME in $(cat tables_list.log); do
get_stats () {
./ggsci >> $STATSLOG 2>&1 <<EOF
stats replicat $REPNAME, latest, $TABNAME
exit
EOF
}

Don't Read Lines With For (and Useless Use Of Cat too). You should always use a while+read loop when reading data from a file or command. More here:

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001


Also, don't put the function definition inside the loop. You don't need to redefine it every time. Functions are "write once, use many" structures.

There are two possible choices here. You could put the loop inside the function, or you could keep the function as is, and run it multiple times in a loop.

Code:
#First choice:

get_stats () {

    local TABNAME TABFILE=/path/to/tables_list.log

    while read -r TABNAME || [[ -n $TABNAME ]]; do

        ./ggsci >> "$STATSLOG" 2>&1 <<-EOF
		stats replicat $REPNAME, latest, $TABNAME
		exit
	EOF

    done <"$TABFILE"

}
Code:
#Second choice:

get_stats () {

    ./ggsci >> "$STATSLOG" 2>&1 <<-EOF
		stats replicat $REPNAME, latest, $1
		exit
	EOF

}

TABFILE=/path/to/tables_list.log

while read -r TABNAME || [[ -n $TABNAME ]]; do

    get_stats "$TABNAME"

done <"$TABFILE"
It's a good idea to define all function-only variables as local. It keeps the possibility of conflicts down. (I'm assuming here that STATSLOG and REPNAME are global variables, otherwise they too should be defined locally.)

The [[ -n $TABNAME ]] test ensures that the last line of the file will always be processed, even if there's no final newline. See the above link for more on that.

The "<<-" form of a here document allows you to indent the rest of it with tabs (and only tabs), without affecting the resulting input. All leading tabs will be removed before processing. It helps keep your formatting clean.


Finally, QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

(Note, however, that variables inside heredocs should NOT be quoted, since no shell processing other than variable/command substitution occurs inside them. They're just more text inside a heredoc.)

Last edited by David the H.; 07-19-2013 at 05:09 AM. Reason: Stupid double-posting error!
 
  


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 shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
[SOLVED] Bash script to read line by line and execute commands Striketh Programming 4 11-06-2011 11:38 AM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM

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

All times are GMT -5. The time now is 02:10 PM.

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