[lug] sed from cron job fails (works from command line)

Hugh Brown hugh at math.byu.edu
Mon Oct 22 18:42:29 MDT 2007


When bash is called as sh it behaves differently. The man page has this 
to say: "If bash is invoked with the name sh, it  tries  to  mimic  the 
  startup behavior  of  historical  versions  of sh as closely as 
possible, while conforming to the POSIX standard as well."

So, you can try setting the shell to /bin/bash, but I doubt that will 
fix your problem.

I just created this script:

#!/bin/sh
line="Oct 19 06:39:18 localhost postfix/smtpd[6853]: NOQUEUE: reject: 
RCPT from unknown[122.35.234.34]: 450 Client host rejected: cannot find 
your hostname, [122.35.234.34]; from=<spammer at spamsolutions.com> 
to=<niceguy at mydomain.com> proto=SMTP helo=<140414104> "
echo "$line" | sed -e 's/\(.*RCPT\ from\ 
\)\([a-Z0-9\.\-]*\)\[\([0-9\.]*\)]:\ \(.*;\).*from=<\(.*\)>\ 
to=<\(.*\)>\ proto.*helo=<\(.*\)>.*/\2\t\3\t\5\t\6\t\7\t\4/g'


and ran it as a script and out of atd and it worked fine.

I was also playing with your question about awk and your sample doesn't 
work for me.

I did have to deal with something similar with sed which was solved by 
using sed -e " " instead of using the single quote.  For example:

CUPSSERVER=myprintserver
  sed -e "s|BrowseOrder allow,deny|# Only allow browsing from the print 
server\nBrowseOrder deny,allow\nBrowseAllow $CUPSSERVER|" cupsd.conf.old 
 > cupsd.conf

That worked as a bash script on RH5 linux boxes.

HTH,

Hugh

Ben wrote:
> Thanks everyone for all the suggestions. Unfortunately, nothing yet has 
> worked.
> 
> In /etc/crontab I already have then line:
> 
> SHELL=/bin/sh
> 
> and /bin/sh is a symlink to /bin/bash
> 
> I tried having my script run /etc/skel/.bashrc and 
> /etc/skel/.bash_profile  before running sed, but that didn't make any 
> difference. (/etc/skel  holds the defaults user config files).
> 
> I added "-x" to the first line #!/bin/sh -x  I didn't learn anything 
> from the debugging, but maybe someone else can:
> 
> ++ grep reject /var/log/mail.log.0
> 
> ++ tail -n 1
> ++ head -n 1
> + line=Oct 19 06:39:18 localhost postfix/smtpd[6853]: NOQUEUE: reject: 
> RCPT from unknown[122.35.234.34]: 450 Client host rejected: cannot find 
> your hostname, [122.35.234.34]; from=<spammer at spamsolutions.com> 
> to=<niceguy at mydomain.com> proto=SMTP helo=<140414104>
> + echo Oct 19 06:39:18 localhost 'postfix/smtpd[6853]:' NOQUEUE: reject: 
> RCPT from 'unknown[122.35.234.34]:' 450 Client host rejected: cannot 
> find your hostname, '[122.35.234.34];' 
> 'from==<spammer at spamsolutions.com>' 'to=<niceguy at mydomain>' proto=SMTP 
> 'helo=<140414104>'
> + sed -e 's/\(.*RCPT\ from\ \)\([a-Z0-9\.\-]*\)\[\([0-9\.]*\)]:\ 
> \(.*;\).*from=<\(.*\)>\ to=<\(.*\)>\ 
> proto.*helo=<\(.*\)>.*/\2\t\3\t\5\t\6\t\7\t\4/g'
> sed: -e expression #1, char 139: Invalid range end
> + i=2
> + '[' 2 -le 20 ']'
> 
> and as I mentioned in the original post, the offending sed line (which 
> works from the command line) is:
> 
> cat somefile|sed -e 's/\(.*RCPT\ from\ 
> \)\([a-Z0-9\.\-]*\)\[\([0-9\.]*\)]:\ \(.*;\).*from=<\(.*\)>\ 
> to=<\(.*\)>\ proto.*helo=<\(.*\)>.*/\2\t\3\t\5\t\6\t\7\t\4/g'
> 
> While I'm asking about silly escape characters and the such. Is there a 
> good way to cat the $variable line of a file inside a bash script? On 
> the command line I'd use awk: cat file|awk '(print NR==500) {print }' -- 
> but if I put that in a bash script and replace 500 with $i (and set 
> i=500) it doesn't work because bash sees $i inside quote and so doesn't 
> replace $i with its value. My hack-ish solution is
> 
> |head -n $i |tail -n 1
> 
> but this is hardly efficient with large files.
> 
> Thanks for the help, and if anyone has more suggestions, I'd appreciate it.
> 
> Ben
> 
> 
> 
> Jeffrey Haemer wrote:
>> Any luck?
>>
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 




More information about the LUG mailing list