using awk to assign a value to a variable doesn't work with cron

1.5k views Asked by At

I'm having problems when running a script from cron. First I found difficulties accessing to SQLite; now it's AWK commands that are running me crazy.

The problematic line is this:

sens=`awk -F, '{ if($2 == '${num}' && $4 == '$tipogalis' && $9 == "0")print $1 }' /usr/xbow/xserve/galtel/relasens`

Don't want to bother you with the details; it's the main line of a while loop that has to read the value of a column inside a file. It works perfectly from the command line, but running as a cron job gives no value to the variable "sens".

I already checked out that all the variables inside the line are read OK (num, tipogalis, etc.), so I'm pretty sure the problem is related with the amount of "&&" or with the "print" function.

Just in case someone wants to suggest something about the enviroment vars, I already added the following lines at the beggining of the script:

LANG=en_US.UTF-8
export LANG

But made no difference.

Any other suggestion, please? I know the problem must be really tiny. Devil is always in details...

2

There are 2 answers

2
glenn jackman On

I assume you have verified that the num and tipogalis variables hold the correct values when you run in cron.

I'm guessing you're missing quotes around the constants in the awk if statement.

sens=`awk -F, '{ if($2 == "'${num}'" && $4 == "'$tipogalis'" && $9 == "0")print $1 }' /usr/xbow/xserve/galtel/relasens`

I'd use the -v option to pass the value into awk instead of piecing together the quoting.

sens=$(awk -F, -v val2="$num" -v val4="$tipogalis" '$2 == val2 && $4 == val4 && $9 == "0" {print $1}' /usr/xbow/xserve/galtel/relasens)
0
Rorro On

In the end the correct way was this one:

sens=awk -F, '{ if($2 == '${num}' && $4 == '$tipogalis' && $9 == '0')print $1 }' /usr/xbow/xserve/galtel/relasens

but the problem was not the line. My call was made to a $9 that was never equal to '0' due to an internal problem.

I'm sorry. This post can be even completely erased to avoid confusing other users.