The following code saves my_string
in the file my_filename
and
then checks whether pattern1
and pattern2
are in the file. The
first is; the second isn't.
#!/bin/bash
my_string="One two three four"
pattern1="two three"
pattern2="two four"
my_filename="my_temp_file"
safeGrepCommand() {
typeset command_to_run="$*"
typeset ret_code
# echo command_to_run=$command_to_run
eval $command_to_run
ret_code=$?
if [ $ret_code != 0 ]; then
printf "Pattern %s is not in the file %s.\n" "${my_pattern}" "${my_filename}"
exit $ret_code
fi
}
echo $my_string > $my_filename
grep_command1="grep --quiet ${pattern1} ${my_filename}"
safeGrepCommand "$grep_command1"
grep_command2="grep --quiet ${pattern2} ${my_filename}"
safeGrepCommand "$grep_command2"
rm -f $my_filename
I am expecting to see the output
Pattern two four is not in the file my_temp_file.
Instead I see
grep: three: No such file or directory
grep: four: No such file or directory
As you'll see if you uncomment the echo line inside the function, the problem is that the quotation marks are not seen by grep.
command_to_run=grep --quiet two three my_temp_file
command_to_run=grep --quiet two four my_temp_file
How do I get bash to pass the quotation marks to grep? Alternatively, how do I specify "two three" as a regexp with [:space] and not worry about quotation marks.