0

in the continuation of this, Awk doesn't match all match all my entries, I am now trying to write a script to execute this on different machine. In the script, I want to run /usr/xpg4/bin/awk if it exists else regular awk. I can't do just a simple if else because my script is too complex - I wan't to do something user friendly and it has some options. So I record the proper awk in a variable like this :

command='awk '"'"'match($0,/^[[:alpha:]_][[:alnum:]_]*\**[[:space:]]+[[:alpha:]_][[:alnum:]_]*[[:space:]]*\([^)]*\)/) { print substr($0,RSTART,RLENGTH) ";\n" }'"'";

after what I try to execute it

code=$($command $file);

I get this error :

awk: command line:1: 'match($0,/^[[:alpha:]_][[:alnum:]_]*\**[[:space:]]+[[:alpha:]_][[:alnum:]_]*[[:space:]]*\([^)]*\)/)
awk: command line:1: ^ bad character « ' » in expression

It doesn't mean anything if I take them off...

Community
  • 1
  • 1

1 Answers1

0

Roughly, don't do it like that.

AWK=/usr/xpg4/bin/awk
if [ ! -x "$AWK" ]
then AWK="awk"
fi

Then you can use:

code=$("$AWK" '…your awk script…' "$file")

Or you can put your script into a file, script.awk, and use:

code=$("$AWK" -f script.awk "$file")

We can debate the merits of the double quotes around the use of "$AWK"; there are pros and cons.

If you need different awk scripts for the different sub-species of (Solaris?) awk, then you can create different script.awk files and still use the common notation with -f script.awk to execute the scripts.

And there's no obligation to use the name script.awk; it is just illustrative. Indeed, if you create it on the fly, you should ensure it is uniquely named (e.g. by adding $$, the current process ID, into the name). Beware of security issues. I'm not sure if Solaris comes with mktemp command to create a temporary file securely.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278