Why does this work

for myfile in `find . -name "R*VER" -mtime +1`
    do
       SHELLVAR=`grep ^err $myfile || echo "No error"`
       ECHO $SHELLVAR
    done

and outputs

No error
    err ->BIST Login Fail 3922 err
    No error
    err ->IR Remote Key 1 3310 err

But this does not

for myfile in `find . -name "R*VER" -mtime +1`
    do
       SHELLVAR=`grep ^err $myfile || echo "No error"`
       awk -v awkvar=${SHELLVAR} '{print awkvar}'
    done

and outputs

awk: cmd. line:1: fatal: cannot open file `{print awkvar}' for reading (No such file or directory)

What am I missing?

Recommended Answers

All 2 Replies

Upon expanding $(SHELLVAR}, the awk invocation becomes

awk -v awkvar=No error '{print awkvar}'

That is awkvar is set to No, error is considered a program (and it is a valid program, a pattern without the action), and the rest becomes a filename to be processed.
Quoting ( awkvar="${SHELLVAR}" ) should help.

Also, something that isn't always obvious, awk operates on its input. Even if you fixed the quoting, it'll 'hang' waiting for input from the kbd. If you: echo | awk -v awkvar="${SHELLVAR}" '{print awkvar}' , then you might get the operation you expect.

I still trip on this on occasion even after using awk for a couple decades.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.