0

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?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Fest3er
0

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.

Edited by nezachem: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.