How do I pass a blank line from a file as an argument to a bash script within another script?
Here are the details. script bar reads in lines from inputfile, stores them in an array, and passes the array elements as options and/or arguments to script foo. inputfile looks something like this:
-l file1
dirA
-c
-lc file2
file2
-cd file3
file4
(etc.) Here is what happens. Script bar reads the file line by line and creates the array properly, including two empty elements for the blank lines. (I have checked this by echoing each array element.) But when bar traverses the array in a loop and passes each element to script foo, the two empty elements are skipped. So the cases where script foo should be invoked with zero arguments and zero options simply do not happen. All the other runs of script foo run as expected.
I thought perhaps the problem was that because the input file had nothing except a newline character, perhaps those elements in the array were just null rather than containing an empty string. So I tried putting spaces in the input file “blank” lines to see if it would make any difference, but the result was the same.
What do I need to do so that bar properly calls foo with no argument and no options when the input file has a blank line, or at least, a line with just whitespace characters?
I hope the question is clear. Thanks for your help!
EDIT. Someone suggested I post the script, so here is the relevant portion:
# read in input lines from file, add to array
index=0
while read line
do
tests[index]=$line
(( index++ ))
done < $@
# run tests
for stuff in ${tests[@]}
do
foo ${stuff} > /dev/null 2>&1
# capture and process the exit status of each foo run
done
I know this looks as though I haven’t set IFS. That’s because this is only a part of my script. I am quite sure that the issue is here somewhere; the array does get properly built with the two elements with blank lines, but those elements are skipped in the for ... in loop.
If you add double quotes where needed, it should work. (I’m only guessing because you did not show how you iterate over the lines).