I have a list of filenames in a file and want to do let the user decide what to do with each. In bash, iterating over filenames is not trivial in itself, so I followed this answer:
#!/bin/bash
while IFS= read -r THELINE;
do
read -n 1 -p "Print line? [y/n] " answer;
if [ ${answer} = "y" ];
then
echo "${THELINE}";
fi;
done < tester;
When I try to execute this (on a non-empty file), I get an error at the if
:
line 5: [: =: unary operator expected
My best guess is that answer
is not set properly, which would be caused by using two calls of read
in a "nested" fashion since the following works as expected:
#!/bin/bash
for THELINE in $(cat "tester");
do
read -n 1 -p "Print line? [y/n] " answer;
if [ ${answer} = "y" ];
then
echo "${THELINE}";
fi;
done;
What is going on here?
I run bash 4.2.24(1)-release (x86_64-pc-linux-gnu)
on 3.2.0-37-generic #58-Ubuntu x86_64 GNU/Linux
.
${answer} = "y"
with"${answer}" == "y"
– replay Feb 24 '13 at 16:10while
is, that might mess up things. – vonbrand Feb 24 '13 at 19:04read
command. – Gilles 'SO- stop being evil' Feb 24 '13 at 22:43