I need to find multiple keywords in log file (AND conditions) and followed the recommendations of putting args into array. However, the script throws No such file or directory. To prove my args are in order, I cut and paste the #debug line into cmd and it works.
#!/bin/bash
filter_list=(mod_jk "Dec 04") # array
for i in "${!filter_list[@]}" # with array keys
do
if [ $i -eq 0 ]; then
grep_args=(-Ewi "\"${filter_list[$i]}\"" "\"$log_path\"")
else
grep_args+=("|") # syntax error near unexpected token `|' if added below instead
grep_args+=(grep -Ewi "\"${filter_list[$i]}\"") # cannot include pipe | here
fi
done
grep "${grep_args[@]}" # actual
echo "grep ${grep_args[@]}" # debug
output
grep: "/home/user/log_samples/Apache_2k.log": No such file or directory
grep: |: No such file or directory
grep: grep: No such file or directory
grep: "Dec 04": No such file or directory
grep -Ewi "mod_jk" "/home/user/log_samples/Apache_2k.log" | grep -Ewi "Dec 04"