[gridengine users] reading arguments using awk with escaped variables

Reuti reuti at staff.uni-marburg.de
Tue Apr 17 09:37:51 UTC 2012


Am 17.04.2012 um 11:08 schrieb George Georgalis:

> On Mon, Apr 16, 2012 at 10:37 PM, Sara Rolfe <smrolfe at u.washington.edu> wrote:
>> I am new to both scripting and SGE, so I don't understand why, but I need to
>> escape all the variables in my script.  For example,
>> 
>> awk "NR==$SGE_TASK_ID" /myPath/fileList.txt
>> 
>> produces a blank output, but if I escape the env variable, like:
>> 
>> awk "NR==\$SGE_TASK_ID" /myPath/fileList.txt
>> 
>> then I get the correct line from the text file.  The problem is when I try
>> to assign this output to a variable.  I still need to use the escape, but I
>> think it's not being passed correctly.
> 
> 
> I usually use single quotes for awk. It prevents shell expansion of
> the symbols. (Shell expansion applies to double quoted arguments but
> not single quoted ones.)

I second this. Using -v to pass arguments to awk (they are known in the BEGIN block already) or list them after the script (they are only available outside of the BEGIN block) and using single quotation marks for the script is the careful way to avoid the mentioned side effects.

But I would accept an exception here if the only rule is a test for the NR field as it's still clear what is done.

--

Do you submit an array job? Otherwise $SGE_TASK_ID won't be set to 1 but "undefined". Then:

${SGE_TASK_ID/undefinded/1}

might help and give you a 1 for non-array jobs, hence:

#!/bin/sh
echo $(awk "NR==${SGE_TASK_ID/undefined/1}" file)

is working fine for me.

-- Reuti


> .George
> 
> 
> -- 
> George Georgalis, (415) 894-2710, http://www.galis.org/
> 
> _______________________________________________
> users mailing list
> users at gridengine.org
> https://gridengine.org/mailman/listinfo/users
> 





More information about the users mailing list