Operating System - HP-UX
1829819 Members
1989 Online
109993 Solutions
New Discussion

/usr/bin/tr -cd [0-9] is not working for some users

 
SOLVED
Go to solution
skt_skt
Honored Contributor

/usr/bin/tr -cd [0-9] is not working for some users

HP-UX 11.00

here is the script...

---------
#!/bin/ksh
#=====================================================================
# Name: Alc_payroll_option1.sh
#
# Description: This process is called when option 1 from
# the payroll menu is chosen


SMTLOC=`echo $SMTLOC`
LOCNAM=`echo $LOCNAM`
export UEC
sdate=`date +"%D %T"`
user=`whoami`

trap 'read pause"Control-C will not terminate menu press enter to Return."' INT
trap 'read pause"Control-/ will not terminate menu press enter to Return."' QUIT
trap 'read pause"Control-Z will not terminate menu press enter to Return."' TSTP

clear
echo "Are You Sure You Want To Start The Payrules Processing? (Y/N) \c"
read ok
if [ $ok = 'Y' -o $ok = 'y' ]; then
echo " Payrules Processing for : $SMTLOC - $LOCNAM\nEnter Week: \c"
read week
week_numeric=`echo $week | tr -cd [0-9]`
echo "You Entered Week Number $week_numeric is that correct? (Y/N) \c"

----------
If i enter week=4 then i expect week_numeric=4 . But week_numeric is returning blank.

Also tried manually for that user outside the script like this

[kumarts@/home/kumarts] #echo $week_numeric
ksh: week_numeric: parameter not set
[kumarts@/home/kumarts] #week=4
[kumarts@/home/kumarts] #week_numeric=`echo $week | tr -cd [0-9]`
[kumarts@/home/kumarts] #echo $week_numeric

[kumarts@/home/kumarts] #


also tried week_numeric=`echo $week | /usr/bin/tr -cd [0-9]`


this is impacting only some set of users.All the users have the /usr/bin/ksh defined.

Any one has Suggestions??
7 REPLIES 7
VK2COT
Honored Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

Hello,

a) Firstly, your Ksh script is not valid.
It is missing the "fi" to end the loop.

This is what you have:

if [ $ok = 'Y' -o $ok = 'y' ]; then
echo " Payrules Processing for : $SMTLOC - $LOCNAM\nEnter Week: \c"
read week
week_numeric=`echo $week | tr -cd [0-9]`
echo "You Entered Week Number $week_numeric is that correct? (Y/N) \c"

This is what you SHOULD have:

if [ $ok = 'Y' -o $ok = 'y' ]; then
echo " Payrules Processing for : $SMTLOC - $LOCNAM\nEnter Week: \c"
read week
week_numeric=`echo $week | tr -cd [0-9]`
echo "You Entered Week Number $week_numeric is that correct? (Y/N) \c"
fi

Once you do it, it works fine.

b) Secondly, since you did not export
variable to the environment, it is not
visible outside the script.

To export any Shell variable, you do:

blah=whatever
export blah

c) What Shell are you using to test it?
I just did this and it worked fine:

# week=4

# week_numeric=`echo $week | tr -cd [0-9]`

# echo $week_numeric
4

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Dennis Handly
Acclaimed Contributor
Solution

Re: /usr/bin/tr -cd [0-9] is not working for some users

>SMTLOC=`echo $SMTLOC`

I'm not sure what you at trying to do here? Remove redundant whitespace?

>week_numeric=`echo $week | tr -cd [0-9]`

The first thing wrong is that you aren't quoting the tr(1) string:
week_numeric=$(echo $week | tr -cd "[0-9]")

The second thing is that you aren't using $().

>VK2COT:It is missing the "fi" to end the loop.

I assume that was covered by "...". :-)

>b) since you did not export variable to the environment

Why does a variable need to be exported?

>I just did this and it worked fine:

Because you didn't figure out all possible ways for the script to fail. :-)
skt_skt
Honored Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

Failed one
week_numeric=`echo $week | tr -cd [0-9]`

Success one
week_numeric=$(echo $week | tr -cd "[0-9]")
week_numeric=`echo $week | tr -cd "[0-9]"`
VK2COT
Honored Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

Hmm,

All this works fine. Again tested (this time
on my Fedora 10) server:

$ week=4

$ week_numeric1=`echo $week | tr -cd [0-9]`

$ week_numeric2=$(echo $week | tr -cd "[0-9]")

$ week_numeric3=`echo $week | tr -cd "[0-9]"`

$ echo $week_numeric1 $week_numeric2 $week_numeric

4 4 4

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Dennis Handly
Acclaimed Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

>VK2COT: All this works fine.

As soon as I mentioned quoting, it should be obvious why it failed. You need to create a file named "0" ... "9" to get it to fail.
Peter Nikitka
Honored Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

Hi,

in addtion to Dennis' comment:
You'll get different strange results, creating more than one file named 0...9 and depending on the input string (here: 11iv1):

# touch 1
# touch 0
# xx=week201
# echo $xx | tr -cd [0-9]
tr: The combination of options and String parameters is not legal.
Usage: tr [ -c | -cds | -cs | -ds | -s ] [-A] String1 String2
tr [ -cd | -cs | -d | -s ] [-A] String1

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"
skt_skt
Honored Contributor

Re: /usr/bin/tr -cd [0-9] is not working for some users

yes, that was the problem.