2

I would like to test the software in development by running a particular script, say script file.ext. But I was told that I an not allowed to run the script if a config file, say file.conf, contains a particular string (not a comment with a leading #) which states the system is in production mode.

So in bash, how can I do something like

If file1.conf contain a string in production and that string is is not of the form something ... # something in production something then execute script file1.ext.

Or, can I somehow get an alert every time the designated file contains the string in production, not in a comment?

I guess I need somehow use the command inotifywait. It would be also nice to have a script that not only checks the file file1.conf but alerts every time that some file with extension .conf shows I'm in a production mode.

2

One way to achieve this is with grep, e.g.,

grep "^[^#]* in production " file.conf || script file.ext

The part behind the || is only executed if the part before it does not exist with exit code 0 and grep has exit code 0 only if it finds its pattern (the first argument).

I chose a simple pattern "^[^#]* in production ". The character ^ stands for the beginning of the line, [^#] stands for any char except # and the quantifier * says there can be any number of them. The rest of the pattern is just the text you would expect to mark production mode.

In order to avoid all the output grep is producing you can send it to /dev/null:

grep "^[^#]* in production " file.conf > /dev/null || script file.ext

If you want to print a message, if you are in production mode and the script is not run you could do

grep "^[^#]* in production " file.conf > /dev/null && \
    echo "We are in production mode. The script was not run." || \
    script file.ext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.