Re: Something wrong with echo? (218 Views)
Reply
Advisor
Steve Start
Posts: 17
Registered: ‎03-17-2004
Message 1 of 17 (218 Views)
Accepted Solution

Something wrong with echo?

Hi friends:

I copied something very similar to this from a script posted on the forums but there seems to be a problem with echo. All the fields are blank. What's wrong?

Here are the lines from the script:
date "+%Y %m %d %H %M %S" | read year month day hour minute second
echo "Year: $year Month: $month Day: $day Hour: $hour Min: $minute Sec: $second"

Here is the output:
Year: Month: Day: Hour: Min: Sec:

If I run the date command this is what I see:
$date "+%Y %m %d %H %M %S"
2006 08 17 12 10 57
$

What can be wrong. I am completely baffled.

Please help,
Steve
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 17 (218 Views)

Re: Something wrong with echo?

I don't see anything wrong. It should work just fine.
If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 17 (218 Views)

Re: Something wrong with echo?

Wait, I sense something in "The Force" that should have been obvious to me.

When you said "I am completely baffled.", did you really mean to say " I am completely bash'led." ?

I suspect that someone has turned you on to the wonderful world of bash. Well, bash guys will tell you it is working perfectly and your variables were set in a sub-process so that when you exit that sub-process the NEW variables are null.

If's it's bash then the answer is pick another shell. On HP-UX, the POSIX shell is rather hard to beat.


If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 17 (218 Views)

Re: Something wrong with echo?

Hi Steve:

There isn't anythign wrong with the script.

One way to obtain what you describe is to drop the pipe ("|") character.

Regards!

...JRF...
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 5 of 17 (218 Views)

Re: Something wrong with echo?

Shlaom Steve,

Nice name.

Run the help for the script. I think you are not using its options correctly.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Please use plain text.
Advisor
Steve Start
Posts: 17
Registered: ‎03-17-2004
Message 6 of 17 (218 Views)

Re: Something wrong with echo?

Hi guys:

Wow, you guys are quick! Clay, you are correct. I am using bash because I want to run it on linux and HPUX. If I switch to the Korn shell, it works on HPUX but still fails on linux. I really need to get all of these values at one time so that the seconds are correct as well. If I try to assign the variables one at a time, the seconds and minutes are sometimes wrong. When I saw a shell script reading multiple variables at one time I thought that would fix my problem.

Any ideas on a workaround?

Thanks,
Steve
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 7 of 17 (218 Views)

Re: Something wrong with echo?

I have a bash version of the script.

Its out of date but has no obvious problems.

http://www.hpux.ws/caljd.sh

http://www.hpux.ws/caljd.pl

A. Clay if you feel I should update this, let me know.

Its a one line change for the bash shell and all functionality seesms to work. On Linux.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 17 (218 Views)

Re: Something wrong with echo?

Hi Steve:

Portability? Well, how about Perl?. This makes one call for the time:

# perl -MPOSIX=strftime -le 'print strftime "Year: %Y Month: %m Day: %d Hour: %H Min: %M Sec: %S",localtime(time)'

Regards!

...JRF...
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,720
Registered: ‎06-21-2000
Message 9 of 17 (218 Views)

Re: Something wrong with echo?

I don't have bash to test with but would something like this work:

DATE=$(date "+%Y %m %d %H %M %S")

YEAR=$(echo ${DATE} | awk '{print $1}')
MONTH=$(echo ${DATE} | awk '{print $2}')
DAY=$(echo ${DATE} | awk '{print $3}')
HOUR=$(echo ${DATE} | awk '{print $4}')
MINUTE=$(echo ${DATE} | awk '{print $5}')
SECOND=$(echo ${DATE} | awk '{print $6}')

echo "Year: $YEAR Month: $MONTH Day: $DAY Hour: $HOUR Min: $MINUTE Sec: $SECOND"
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 10 of 17 (218 Views)

Re: Something wrong with echo?

Well Steve, my answer for Linux is zsh and that is what I run caljd.sh (as well as all my other scripts) under on Linux. Your script will work as is under zsh. I am well aware that bash is the absolute best, most wonderful shell out there. Just ask any Linux guy but the construct that you tried to use is very useful even if the way the POSIX shell implements is "wrong" (at least to the Linux crowd).

Patrick's approach will certainly work but here is a workaround that will work in all cases but I don't like it because it creates a temporary file.

TDIR=${TMPDIR:-/var/tmp}
T1=${TDIR}/x${$}_1.tmp

trap 'eval rm -f ${T1}' 0 1 2 15
date "+%Y %m %d %H %M %S" > ${T1}
read year month day hour minute second < ${T1}
echo "Year: ${year} Month: ${month} Day: ${day} Hour: ${hour} Min: ${minute} Sec: ${second}"

Because there is no sub-process, all shells should be happy. Notice that I put {}'s around all of your variables; sometimes they are needed; sometimes not but they never hurt.

As mentioned, Perl is the portable solution and I view it as my portable solution for caljd.sh (ie caljd.pl) although I am not certain where Steven's reference to caljd.sh comes from with respect to this thread.



If it ain't broke, I can fix that.
Please use plain text.
Honored Contributor
Marvin Strong
Posts: 492
Registered: ‎03-01-2004
Message 11 of 17 (218 Views)

Re: Something wrong with echo?

You could always install pdksh on your linux box and then you have ksh, which you could then run your script asis on both hp and linux.

of course I'm sure there are people that will disagree with this solution.
Please use plain text.
Advisor
Steve Start
Posts: 17
Registered: ‎03-17-2004
Message 12 of 17 (218 Views)

Re: Something wrong with echo?

Hi guys:

I really don't know Perl and I don't want to convert all of my scripts to Perl right now. Patrick's idea works and Clay's idea of using the zsh worked. I didn't even know there was a z shell. Clay, the temp file worked but shouldn't there be a rm after the file is read?

Thanks,
Steve
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 13 of 17 (218 Views)

Re: Something wrong with echo?

You could certainly put an explicit rm command after the read but it isn't necessary. Note this line:
trap 'eval rm -f ${T1}' 0 1 2 15

This means that if the process receives a SIGHUP (1), a SIGINT (2), or a SIGTERM (15) that the rm command will be executed BUT note the '0' -- that is triggered on a normal exit so that when the process exits (even without a signal) the trap is still executed and the rm is done. I do see that it would also be wise to add a '3' (SIGQUIT) to the list of signals in the trap.
If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 14 of 17 (218 Views)

Re: Something wrong with echo?

Hi (again) Steve:

I didn't mean to necessarily suggest that a total conversion to Perl was necessary. I meant to point out that it is a portable solution that bears consideration.

Clay's approach automatically removes the temporary file he creates when your script exits. Look at the 'trap' statement.

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 15 of 17 (218 Views)

Re: Something wrong with echo?

Hi Steve:

I should add that your *shell* script could always call a perl snippet to do the work needed, no differently then shell scripts call 'awk' or 'sed programs. For instance:

TIMES=`perl -MPOSIX=strftime -le 'print strftime "Year: %Y Month: %m Day: %d Hour: %H Min: %M Sec: %S",localtime(time)'`

echo ${TIMES}

Regards!

...JRF...
Please use plain text.
Advisor
Steve Start
Posts: 17
Registered: ‎03-17-2004
Message 16 of 17 (218 Views)

Re: Something wrong with echo?

Hi guys:

Thanks to everybody for all the quick answers. I will probably use zsh for now on linux and posix shell on HPUX but I may convert everything to the temp file method.

Thanks,
Steve
Please use plain text.
Trusted Contributor
Eric Raeburn
Posts: 137
Registered: ‎11-26-2002
Message 17 of 17 (218 Views)

Re: Something wrong with echo?

Don't know if anyone's still following this thread, but this works on both Linux/bash and HPUX/ksh:

typeset -i i=0

t=$(date "+%Y %m %d %H %M %S")
for x in $t ; do
ts[i]=$x
i=i+1
done

echo year=${ts[0]}
echo month=${ts[1]}
echo day=${ts[2]}
echo hr=${ts[3]}
echo min=${ts[4]}
echo sec=${ts[5]}


-Eric
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation