2

In my bash script, I am attempting to parse through a status file and detect errors based on some keywords. I store these prefixes in a list, and then loop through them.

Bash script:

status_page="/path/to/file.txt"
list="aaa bbb ccc ddd"

for pre in $list
do
    echo "grep '\w\w\w${pre}-.*\.lin failed' ${status_page}" # debug
    if grep '\w\w\w${pre}-.*\.lin failed' ${status_page}; then
        echo "Found error!"
        exit 8;
    fi
done

/path/to/file.txt:

xyzfff-tool.lin failed
xyzggg-exec.lin failed
rstccc-tool.lin failed

The bash script should catch the line rstccc-tool.lin failed, but it skips right over it.

For debugging, I print the grep commands verbatim, and when I copy that line and issue the command in my shell (tcsh), it returns that line...

Shell:

$ grep '\w\w\wccc-.*\.lin failed' /path/to/file.txt
    rstccc-tool.lin failed
$ echo $?
0

If grep can find the line when I issue the command normally, how come it won't find it when the bash script is calling grep?

0

2 Answers 2

6

The variable won't be expanded in single quotes. Try with double quotes:

if grep "\w\w\w${pre}-.*\.lin failed" "${status_page}"; then
Sign up to request clarification or add additional context in comments.

1 Comment

Sure enough... that solved it all. Thanks for the quick solution. :) Will accept the answer in... 9 minutes.
2

The ${pre} portion of that script is not parsing it correctly. I believe you need that line to say:

if grep '\w\w\w'${pre}'-.*\.lin failed' ${status_page}; then

... where the ${pre} is outside the quotation, such that bash will do the correct string replacement before sending it to grep.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.