E.Cross
E.Cross

Reputation: 2137

Eval awk command with single quotes

I have a function "checkExist" that takes in a command and executes it based on whether or not the output file already exists. I pass a command like this, where file1 and file2 are just the names of output files that the commands create, so if they already exist it will ask if you want to overwrite, else it will skip:

checkExist file1 file2 command1 command2

In actual use like this:

checkExist 1.txt 2.txt "echo $1 | awk '$5 <= 10 {print $3, $4}'" "echo $2 | awk '$5 <= 10 {print $3, $4}'"

$1 and $2 above are inputs to the script "smartfilter.sh" containing the function checkExist within. So they are just file inputs.

Later in the checkExist function if the user types 'Y/y' to overwrite, or the files don't already exist then it will

eval $3 &
eval $4 &
wait

And I get an error like so:

awk:  >= 10 {print , }
awk:  ^ syntax error
awk:  >= 10 {print , }
awk:               ^ syntax error
awk:  >= 10 {print , }
awk:                 ^ syntax error
awk: cmd. line:1:  >= 10 {print , }
awk: cmd. line:1:                  ^ unexpected newline or end of string

I know it is to do with the single quotations ' around the awk and eval not parsing them correctly. I have tried \' but that doesn't work either. Is there a proper way to do this?

Upvotes: 0

Views: 3998

Answers (1)

ams
ams

Reputation: 25599

checkExist 1.txt 2.txt "echo $1 | awk '\$5 <= 10 {print \$3, \$4}'" "echo $2 | awk '\$5 <= 10 {print \$3, \$4}'"

Upvotes: 2

Related Questions