I just want to pass a shell variable that stores name of a file to awk command. When I searched this problem on the net I see many different options but none of them worked for me. I tried the followings:
#!/bin/bash
for i in "$@"
do
case $i in
-p=*|--producedfile=*)
PFILE="${i#*=}"
shift # past argument=value
*)
# unknown option
;;
esac
done
echo "PRODUCEDFILE = ${PFILE}"
awk -v FILE=${PFILE} '{print FILE $0}' #DIDNT WORK
awk '{print FILE $0}' ${PFILE} # DIDNT WORK
awk -v FILE=${PFILE} '{print $0}' FILE #DIDNT WORK
$PFILEis the name of the fileAwkshould process on (or) just be passed to the body?awk '{print FILE $0}' ${PFILE}work? RememberFILEvariable is undefined here. I am sure this would have printed the contents ofinputfile.txtone line at a time-p=inputfile.txt, then there's no need for the shift. You wouldshiftif you called it with-p inputfile.txt, in which case the${i#*=}doesn't work. Drop theshift.