- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Something wrong with echo?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:14 AM
08-17-2006 04:14 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:16 AM
08-17-2006 04:16 AM
Re: Something wrong with echo?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:20 AM
08-17-2006 04:20 AM
SolutionWhen 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.
- Tags:
- bash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:20 AM
08-17-2006 04:20 AM
Re: Something wrong with echo?
There isn't anythign wrong with the script.
One way to obtain what you describe is to drop the pipe ("|") character.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:22 AM
08-17-2006 04:22 AM
Re: Something wrong with echo?
Nice name.
Run the help for the script. I think you are not using its options correctly.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:32 AM
08-17-2006 04:32 AM
Re: Something wrong with echo?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:40 AM
08-17-2006 04:40 AM
Re: Something wrong with echo?
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:45 AM
08-17-2006 04:45 AM
Re: Something wrong with echo?
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 04:45 AM
08-17-2006 04:45 AM
Re: Something wrong with echo?
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:11 AM
08-17-2006 05:11 AM
Re: Something wrong with echo?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:40 AM
08-17-2006 05:40 AM
Re: Something wrong with echo?
of course I'm sure there are people that will disagree with this solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:41 AM
08-17-2006 05:41 AM
Re: Something wrong with echo?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:48 AM
08-17-2006 05:48 AM
Re: Something wrong with echo?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:51 AM
08-17-2006 05:51 AM
Re: Something wrong with echo?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:57 AM
08-17-2006 05:57 AM
Re: Something wrong with echo?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2006 05:59 AM
08-17-2006 05:59 AM
Re: Something wrong with echo?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2006 11:27 AM
08-25-2006 11:27 AM
Re: Something wrong with echo?
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
- Tags:
- command substitution