I have written a script to search an .ini file for some specific word.
The .ini file has one of two different names, (let's say config.ini or configuration.ini), so I have to check in both.
I am doing it with the following if
sentences by using logical OR
:
HAS_SOME_WORD=FALSE
if [ "$(grep -v '^;\|^\[' "path_to_file/config.ini" | \
grep -c '\\some_word')" -ge 1 ] \
|| [ "$(grep -v '^;\|^\[' "path_to_file/configuration.ini" | \
grep -c '\\some_word')" -ge 1 ]; then
HAS_SOME_WORD=TRUE
else
HAS_SOME_WORD=FALSE
fi
I am avoiding the lines starting by ";" or "[" as they must not be included in the desired search, while looking for the word "\some_word".
I want is to exclude the grep error messages when one of the two files does not exist, i.e:
grep: path_to_file/config.ini: No such file or directory
or:
grep: path_to_file/configuration.ini: No such file or directory
I have been able to avoid them by redirecting the output to /dev/null
when executing the script:
./search_script.sh 2>/dev/null
However I would like to include this redirection in the if
code itself, not when invoking the script.
How shall I implement that? Is there a more efficient way of doing what I'm trying?
I have tried to add the -q
parameter to grep
in order to avoid the error messages printed, but it had no effect. Also tried adding 2>/dev/null
redirection at the end of each if
sentence, but I'm afraid that I haven't applied the correct syntax.
2>/dev/null
redirection in eachif
branch should work. Can you show us exactly what you tried?if [ "$(grep -v '^;\|^\[' "path_to_file/config.ini" | \ grep -c '\\some_word')" -ge 1 2>/dev/null ]; then
(For both if's)