Operating System - Linux
1745786 Members
3531 Online
108722 Solutions
New Discussion юеВ

Something wrong with echo?

 
SOLVED
Go to solution
Steve Start
Advisor

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

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

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.
James R. Ferguson
Acclaimed Contributor

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...
Steven E. Protter
Exalted Contributor

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
Steve Start
Advisor

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
Steven E. Protter
Exalted Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Patrick Wallek
Honored Contributor

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

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.