Operating System - HP-UX
1751744 Members
5691 Online
108781 Solutions
New Discussion юеВ

Can anyone find a bug in this code?? shell script

 
amonamon
Regular Advisor

Can anyone find a bug in this code?? shell script

I have done a script and IT WORKS JUST PERFECT from command line...but in cron it has problems executing...


nawk -F"|" '
{ s=substr($104,2,18)}
{b[s] ++s}
END { for (i in b) print i, b[i] } ' $1 > /path/to/files/TranId_w$2

q=`cat /path/to/files/TranId_w$2 | wc -l`
echo $q > /path/to/files/zaCGSN


nawk -F"|" -v x=$2 -v w=$(cat zaCGSN) '
{u=u+1; l=l+$66}
($66 != 0)&&($110 == 1)&&($111 == 0) { a=a+1; s=s+$66}
END { print x"|"u"|"l"|"w"|"s } ' $1 >> statistic



rm /path/to/files/TranId_w$2
rm zaCGSN

./ftp_PC statistic


and I execute it ./script1 file1 20061212
I think problem is somewhere in second nawk..becouse in cron I can see TranId_w$2 created every day but statistic is not updated..

can anyone help me with debug..

Regards..
8 REPLIES 8
Ivan Krastev
Honored Contributor

Re: Can anyone find a bug in this code?? shell script

When starting scripts from cron use full names - for exmple instead of cat use /sbin/cat.

ivan
J.C. Sepp_1
Occasional Contributor

Re: Can anyone find a bug in this code?? shell script

Hi Amonamon,

When you run it from the command line the script inherits the variables defined in your shell, most notably the PATH.
Cron has no such environment, you need to tell it everything it should know. So either you could define a PATH variable at the beginning of your job, or you should fully qualify all commands you use. So, if you run nawk from /usr/local/bin make sure you specify "/usr/local/bin/nawk" instead of just using "nawk".

HtH,

Jan
There are 10 kinds of programmers: those who understand binary and those who don't (Shawn Powers)
Bill Hassell
Honored Contributor

Re: Can anyone find a bug in this code?? shell script

As mentioned, your command line is actually a shell login environment. To see what you are missing, type the command:

env

In cron, that same command produces:

HOME=user's-home-directory LOGNAME=user's-login-id PATH=/usr/bin:/usr/sbin:. SHELL=/usr/bin/sh

And that's it. When you write *any* script, it is a good idea to never assume anything, whether it is $PATH or $PWD. Changes I would make:

Always use the interpreter line as line 1:

#!/usr/bin/sh

Always explicitly state your PATH - never use the current one:

export PATH=/usr/bin

Never use any patch that is relative (ie, ./ftp)

The grave accents have been deprecated for 10 years. Use the construct $() as in:

q=$(cat /path/to/files/TranId_w$2 | wc -l)


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: Can anyone find a bug in this code?? shell script

Hi,

in addition to the above suggestions, I want to warn about the construct w=$(...) in:
nawk -F"|" -v x=$2 -v w=$(cat zaCGSN) '...

If the file zaCGSN contains one or more blanks, there will be a syntax error this (n)awk command.
Use
nawk -F"|" -v x=$2 -v w="$(cat zaCGSN)" '...
instead.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"

Re: Can anyone find a bug in this code?? shell script

I see that in

echo $q > /path/to/files/zaCGSN

you use the full path to the file "zaCGSN", and in

nawk -F"|" -v x=$2 -v w=$(cat zaCGSN) '

you use a relative path as if you were in the same directory.

With no full path to the file I suppose that the cat command will fail.
OldSchool
Honored Contributor

Re: Can anyone find a bug in this code?? shell script

....and the full path to "ftp_PC"

Re: Can anyone find a bug in this code?? shell script

Hello Amonamon,

I guess that the second parameter you pass to your script looks like a date.

If you want this to be excecuted by cron with an updated date you, perhaps, wrote in your crontab something like :

/path/script1 file1 $( date +'%Y%m%d' )

This does not work well because the % character is interpreted as a separator by cron.

Regards,

JPH

Peter Nikitka
Honored Contributor

Re: Can anyone find a bug in this code?? shell script

Hi,

to complete JPH remark:
if you want to use the percent sign in a crontab commandline, you have to double it:
/path/script1 file1 $(date +'%%Y%%m%%d')

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"