I have this bash script:
for opt in string1 string2 string3 ... string99
do somestuff
It works, but I would like to replace the explicit listing of my strings with a file which actually contains all the strings; something like this:
strings=loadFromFile
for opt in $strings
do somestuff
How should I do this?
5 Answers 5
while read VAR
is probably best here, as it handles per-line input. You can redirect it from a file, e.g.:
while IFS= read -r THELINE; do
echo "..$THELINE"
done </path/to/file
That'll give you each line prepended with ".."
For your example case:
while IFS= read -r opt; do
#somestuff $opt
done </path/to/file
See Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for explanations.
-
3Putting cats into pipes is bad behavior from a memory management point of view. especially when using "filter" built-in commands. @rush's answer is better, and what I use on a daily basis.Didi Kohen– Didi Kohen2012年09月19日 14:15:38 +00:00Commented Sep 19, 2012 at 14:15
-
3It also puts the
while
loop into a subshell, which can cause confusing behaviour if update variable values in the loop (the values will disappear when the subshell exits)glenn jackman– glenn jackman2012年09月19日 15:48:37 +00:00Commented Sep 19, 2012 at 15:48 -
If
somestuff
includes anotherread
, this seems not to work properly.Raphael– Raphael2013年02月23日 19:04:42 +00:00Commented Feb 23, 2013 at 19:04 -
@raphael - in what way does it not "work properly"? I just tried nested reads and it works fine.IBBoard– IBBoard2013年02月24日 13:52:38 +00:00Commented Feb 24, 2013 at 13:52
-
@IBBoard See this new question.Raphael– Raphael2013年02月24日 15:59:16 +00:00Commented Feb 24, 2013 at 15:59
while IFS= read -r opt
do
some_stuff
done < file_with_string
See Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for explanations.
The while IFS= read -r line; do ...; done < aFile
is the best answer
If your strings do not contain whitespace or \[*?
, you could do
for word in $(< aFile); do something; done
$(< file)
is a bash feature that reads the file (like cat
except without having to spawn a new process).
Why don't you use the readarray
builtin (requires bash >= 4.0
)?
readarray < FileNameFromWhereToLoad # push every line of
# 'FileNameFromWhereToLoad' onto
# $MAPFILE by default
for i in $MAPFILE ; do
echo $i
done
-
This also requires newer version of bash.Didi Kohen– Didi Kohen2012年09月19日 14:12:56 +00:00Commented Sep 19, 2012 at 14:12
-
i wouldn't call
bash >= 4
new (release date was February 2009)!user1146332– user11463322012年09月19日 14:31:03 +00:00Commented Sep 19, 2012 at 14:31 -
2Since he's using 2.05a, it is newer than what the asker has. In addition, some companies have avoided GPLv3 versions of bash, so do not have it in their system.Didi Kohen– Didi Kohen2012年09月19日 14:36:17 +00:00Commented Sep 19, 2012 at 14:36
-
Do you have a crystal ball around? But yes, i agree with you about your last statement, i will add the version requirement of
readarray
to my answer.user1146332– user11463322012年09月19日 14:39:48 +00:00Commented Sep 19, 2012 at 14:39 -
2I didn't read that. Thanks for the hint ;) Maybe my answer is of value for somebody who will search for an answer for a similar question!?user1146332– user11463322012年09月19日 14:56:36 +00:00Commented Sep 19, 2012 at 14:56
My advice:
cat INPUTFILE| {
declare -a LINES
mapfile -t LINES
for line in "${LINES[@]}"
do
somestuff
done
}
-
Syntax error: bad substitutionmichelemarcon– michelemarcon2012年09月19日 13:34:39 +00:00Commented Sep 19, 2012 at 13:34
-
1Works for me. @michelemarcon, is your
bash
4.00 or never?manatwork– manatwork2012年09月19日 13:36:30 +00:00Commented Sep 19, 2012 at 13:36 -
GNU bash, version 2.05a.0(1)-release (arm-unknown-linux-gnu)michelemarcon– michelemarcon2012年09月19日 13:42:44 +00:00Commented Sep 19, 2012 at 13:42
-
5That is ancient. Please specify this requirement in the question.manatwork– manatwork2012年09月19日 13:46:10 +00:00Commented Sep 19, 2012 at 13:46
-
osx 10.12.2 has ancient bash ; $ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16) Copyright (C) 2007 Free Software Foundation, Inc.AnneTheAgile– AnneTheAgile2016年12月23日 16:56:34 +00:00Commented Dec 23, 2016 at 16:56
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
while IFS= read
used so often, instead ofIFS=; while read..
? and Inwhile IFS= read..
, why does IFS have no effect?