Operating System - Linux
1752586 Members
4233 Online
108788 Solutions
New Discussion юеВ

Re: cron environment issue

 
SOLVED
Go to solution
Tim Nelson
Honored Contributor

cron environment issue

The below is an example of a strange issue I am running into only when running the below from cron and as root. Manually as root all works, via cron through another user also works. This is only on one system. Other systems do not seem to have a problem.

The idea is to separate the char from the numeric.

#!/usr/bin/ksh
ii=123g
num=`echo $ii|tr -d [a-z]`
char=`echo $ii|tr -d [0-9.]`
echo "string is $ii"
echo "num is $num"
echo "char is $char"


The output is as follows when running as root from cron.
string is 123g
num is 123g <<-- should just be 123
char is g

Any help/ideas would be appreciated.
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: cron environment issue

Hi Tim:

I'd be interested in your 'locale' settings when you run along with the 'whence' output of 'echo' and 'tr'. Add this to your script:

# locale
# whence echo
# whence tr

...and compare under the conditions you describe.

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: cron environment issue

Tim,
from "man tar"
c1-c2 Stands for the range of collating elements c1 through c2, inclusive, as defined by the current setting of the LC_COLLATE locale category.

[:class:] Stands for all the characters belonging to the defined character class, as defined by the current setting of LC_CTYPE locale category. The following character class names will be accepted when specified in string1: alnum, alpha, blank, cntrl. digit, graph, lower, print, punct, space, upper, or xdigit, Character classes are expanded in collation order.

Probably down to LC_COLLATE and/or LC_CTYPE.

Have you tried:
num=`echo $ii|tr -d [:alpha:]`
char=`echo $ii|tr -d [:digit:]`
Tim Nelson
Honored Contributor

Re: cron environment issue

output below. also included SHELL as it

echo
/usr/bin/tr
shell is /usr/bin/sh
string is 123g
num is 123g
char is g


Tim Nelson
Honored Contributor

Re: cron environment issue

Same results Peter. Locale returns the below and all are set to "C".

LANG=
LC_CTYPE="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_MESSAGES="C"
LC_ALL=


OldSchool
Honored Contributor

Re: cron environment issue

set LANG=C and try it
OldSchool
Honored Contributor

Re: cron environment issue

disregard my last.....
Tim Nelson
Honored Contributor

Re: cron environment issue

No luck Oldschool.

echo
/usr/bin/tr
shell is /usr/bin/sh
string is 123g
num is 123g
char is g

Notice that stripping off the numeric works. The trouble is with the non-numeric.

Peter Nikitka
Honored Contributor
Solution

Re: cron environment issue

Hi,

I really recommend to have the char-class quoted:
NOT
num=`echo $ii|tr -d [a-z]`
but
num=$(echo $ii|tr -d '[a-z]')

If you have one-letter file(s) a x c in your current directory, the filename matching will blow the 'tr'.

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"
A. Clay Stephenson
Acclaimed Contributor

Re: cron environment issue

Let's apply some rigorous quoting (you should be doing this anyway) and see what happens:

ii=123g
num=$(echo ${ii} | tr -d "[a-z]")
echo "num is ${num}"
If it ain't broke, I can fix that.