Issue
#!/bin/bash
status_1="rebuild"
status_2="enabled"
status_3="disabled"
if grep -q "[alpha]" "/home/user/asd" && grep -q "[beta]" "/home/user/asd" && \
[[ "$status_1" == "rebuild" || "$status_1" == "reinstall" || "$status_1" == "install" ]] && \
[[ "$status_2" == "enabled" || "$status_3" == "enabled" ]];
then
echo yay
else
echo nay
fi
This should return yay only if lines "[alpha]" + "[beta]" are found in the file "asd" + if status_1 = rebuild or reinstall or install + if at least status_2 or status_3 = enabled.
However, it's always returning "yay", even if I remove "[alpha]" + "[beta]" lines from file "asd".
Solution
[alpha]
is a character class that matches any of the letters a
, l
, p
, or h
. Use -F
to interpret it as a fixed string rather than a regex.
if grep -qF "[alpha]" asd && grep -qF "[beta]" asd && \
Answered By - John Kugelman