LinuxQuestions.org
Review your favorite Linux distribution.
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-27-2007, 01:58 PM   #1
300zxkyle
LQ Newbie
 
Registered: Jun 2007
Distribution: OpenSuSE 10.2
Posts: 13

Rep: Reputation: 0
bash: read multiple lines in a file


I use this command to create a list of files.
Code:
ls *.ini > filenames
the contents look like:
Code:
$> cat filenames
file1.ini
file2.ini
file3.ini
file4.ini
I need to be able to read that list into an array in a bash script. How would I start about doing this. I've tried :
Code:
cat filenames | read fileList
But because it only reads a single line of input I only get the first filename. I'm new to shell scripting so I would appreciate any help.
 
Old 07-27-2007, 03:20 PM   #2
biophysics
Member
 
Registered: Jan 2002
Location: Germany
Distribution: Kubuntu
Posts: 444

Rep: Reputation: 30
for i in `cat filenames`
do
#do whatever you want to do
#wc $i
#
done

http://tldp.org/LDP/Bash-Beginners-G...ect_09_01.html
 
Old 07-27-2007, 04:25 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by 300zxkyle
I use this command to create a list of files.
Code:
ls *.ini > filenames
the contents look like:
Code:
$> cat filenames
file1.ini
file2.ini
file3.ini
file4.ini
I need to be able to read that list into an array in a bash script. How would I start about doing this. I've tried :
Code:
cat filenames | read fileList
But because it only reads a single line of input I only get the first filename. I'm new to shell scripting so I would appreciate any help.
Code:
#!/bin/bash
declare -a file_array
let count=0

while read LINE <filenames; do

    file_array[$count]=$LINE
    ((count++))
done


Cheers,
Tink
 
Old 07-27-2007, 06:08 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Tinkster
Code:
while read LINE <filenames; do

    file_array[$count]=$LINE
    ((count++))
done
...doesn't work for me, this does:
Code:
declare -a array; count=0; for LINE in $(<filenames)
do array[$count]="$LINE"; ((count++)); done
--
Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/"; }
 
Old 07-27-2007, 06:14 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Do you really need the list?


Code:
initfiles=($(cat list))

These examples assume that the filenames don't contain whitespace characters.

Last edited by jschiwal; 07-27-2007 at 06:24 PM.
 
Old 07-28-2007, 12:37 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by unSpawn
...doesn't work for me, this does:
Code:
declare -a array; count=0; for LINE in $(<filenames)
do array[$count]="$LINE"; ((count++)); done
That's what "making shortcuts w/o testing" gives me :}
I thought I could scipt the file-descriptor ... wrong
was I ...

In my old script there's a few more things going
on ...
Code:
declare -a file_array
let count=0
exec 5<filenames
while read LINE <&5; do
    echo $LINE
    file_array[$count]=$LINE
    ((count++))
done
exec 5>&-

Thanks spawny...


Cheers,
Tink

Last edited by Tinkster; 07-28-2007 at 12:57 AM.
 
Old 07-28-2007, 08:15 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
What does spawny mean?

Quote:
When someone flukes a pot in a game of pool the phrase 'you spawny git' would be ...
What does "flukes a pot" mean?
 
Old 07-29-2007, 04:38 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
What does spawny mean?
In this context it's got nothing to do with slang but it's a bastardisation of my handle. "To fluke a pot" should translate to something like a lucky shot.
 
  


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] edit lines in a file pieperp Programming 4 01-31-2007 08:56 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
Bash - How to make For read lines instead of words in a list ? landuchi Programming 10 02-15-2006 01:36 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM

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

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