pass shell parameters to awk does not work

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?

Perhaps try quoting:

awk -v awkvar=${SHELLVAR}

i.e.

awk -v awkvar="${SHELLVAR}"

It also helps if you give awk a file to work with:

awk -v awkvar="${SHELLVAR}" "$myfile"

It's generally better not to use a for-loop for this, but a while-loop:

find ... | while read myfile; do
  ...
done

"for" can't handle filenames with spaces so well, and there are only so many files it can take as arguments.

1 Like

Cool. I changed to a while loop and fed $myfile to the awk command. It worked. Thank you.