I want to check if a file contains a specific string or not in bash. I used this script, but it doesn’t work:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What’s wrong in my code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You don’t need
[[ ]]here. Just run the command directly. Add-qoption when you don’t need the string displayed when it was found.The
grepcommand returns 0 or 1 in the exit code depending onthe result of search. 0 if something was found; 1 otherwise.
You can specify commands as an condition of
if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.As you can see you run here the programs directly. No additional
[]or[[]].