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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-27-2007, 01:58 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2007
Distribution: OpenSuSE 10.2
Posts: 13
Rep:
|
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.
|
|
|
|
07-27-2007, 04:25 PM
|
#3
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
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
|
|
|
|
07-27-2007, 06:08 PM
|
#4
|
|
Moderator
Registered: May 2001
Posts: 24,779
|
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/"; }
|
|
|
|
07-27-2007, 06:14 PM
|
#5
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
07-28-2007, 12:37 AM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
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.
|
|
|
|
07-28-2007, 08:15 AM
|
#7
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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?
|
|
|
|
07-29-2007, 04:38 AM
|
#8
|
|
Moderator
Registered: May 2001
Posts: 24,779
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:30 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|