Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
For problem #2, I believe the answer is in the man page for `date` - look at the formatting modifiers further down the page. A snippet:
Code:
By default, date pads numeric fields with zeroes. The following optional flags may follow `%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
For problem #2, I believe the answer is in the man page for `date` - look at the formatting modifiers further down the page. A snippet:
Code:
By default, date pads numeric fields with zeroes. The following optional flags may follow `%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
Meh, stupid Mac OS X. The Mac OS X implementation of date doesn't allow me to do it. http://developer.apple.com/library/m...n1/date.1.html
I can't wait to get a new computer. For now however, I have to deal with it.
Code:
Andrew@MacBook-Pro:~$ echo "Today\'s date is $(date +%A\,\ %B\ %d\,\ %Y). It is currently $(date %-I\:%M\ %p)."
date: illegal time format
First problem solution: alias tyme="echo Today$'\''s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p).". The $'\'' is ANSI-C Quoting.
Second problem solution: use %-d and %-M instead of %d and %M. From the date man page: "By default, date pads numeric fields with zeroes. The following optional flags may follow `%': - (hyphen) do not pad the field".
Your first problem solution does not work because it sets the time when the alias is defined (as alias tyme would show) but a slightly modified form dos work: alias tyme="echo Today\'s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p)."
EDIT: your solution is neater than the ANSI-C Quoting solution -- would have sworn I'd tried it!
Last edited by catkin; 12-23-2010 at 09:05 PM.
Reason: bu -> but
It just replaces any space followed by one or more zeroes, with a space only. Seems over simplistic, but who knows, maybe it will catch all situations. And maybe not..
Good luck! (Sorry if you already tried this one - I did not examine ALL of your attempts intensely).
Last edited by GrapefruiTgirl; 12-23-2010 at 09:08 PM.
Reason: for clarity, I removed the garbage string I was echoing into the sed
By the way, you can save some backslashes, commas don't need escapes quotes can be used instead of escaping the spaces:
Code:
alias tyme="echo Today\'s date is $(date '+%A, %B %e, %Y'). It is currently $(date '+%l:%M %p')."
The %-x may not work, but there is still:
Code:
%d is replaced by the day of the month as a decimal number (01-31).
...
%e is replaced by the day of month as a decimal number (1-31); single digits are preceded by a
blank.
...
%I is replaced by the hour (12-hour clock) as a decimal number (01-12).
...
%l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by
a blank.
By the way, you can save some backslashes, commas don't need escapes quotes can be used instead of escaping the spaces:
Code:
alias tyme="echo Today\'s date is $(date '+%A, %B %e, %Y'). It is currently $(date '+%l:%M %p')."
The %-x may not work, but there is still:
Code:
%d is replaced by the day of the month as a decimal number (01-31).
...
%e is replaced by the day of month as a decimal number (1-31); single digits are preceded by a
blank.
...
%I is replaced by the hour (12-hour clock) as a decimal number (01-12).
...
%l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by
a blank.
It just replaces any space followed by one or more zeroes, with a space only. Seems over simplistic, but who knows, maybe it will catch all situations. And maybe not..
Good luck! (Sorry if you already tried this one - I did not examine ALL of your attempts intensely).
Thanks GrapefruiTgirl -- that works perfectly as well. Back to the sed guides I go...
First problem solution: alias tyme="echo Today$'\''s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p).". The $'\'' is ANSI-C Quoting.
Second problem solution: use %-d and %-M instead of %d and %M. From the date man page: "By default, date pads numeric fields with zeroes. The following optional flags may follow `%': - (hyphen) do not pad the field".
Your first problem solution does not work because it sets the time when the alias is defined (as alias tyme would show) but a slightly modified form dos work: alias tyme="echo Today\'s date is \$(date +%A\,\ %B\ %d\,\ %Y). It is currently \$(date +%I\:%M\ %p)."
EDIT: your solution is neater than the ANSI-C Quoting solution -- would have sworn I'd tried it!
Thanks!!
I swear I tried it too... Somehow it snuck past me.
Though, since that is ANSI-C, isn't that more 'correct' since it's standards compliant?
Damn, everytime I ask a question on LQ about bash scripting I walk away feeling more stupid than ever...
Nah - don't be too hard on yourself. You made a valiant effort in this one! Silly MAC - get a Linux.
Everyone who has some idea what they're talking about today, began at nothing in the beginning. You're further back on the scripting than some of us, and also you're further ahead than some of us; but there's ALWAYS something more to learn, no matter how much one knows..
Just to follow up on the thread, I ended up using this string:-
Code:
alias tyme="echo Today\'s date is $(date +%A\,\ %B\ %d\,\ %Y | sed 's/ [0]*/ /g'). It is currently $(date +%I\:%M\ %p | sed 's/ [0]*/ /g')."
While using ntubski's recommendation is probably the 'most proper' way to do it, I ended up using GrapefruiTgirl's solution since the output looks nicer in certain situations (replacing the zero with a space made two spaces in a row, which made it look uglier). I'm very happy with my alias now, even though I am WAY to OCD. I am going to keep this bookmarked for later.
Hope this helps anyone passing along (though I doubt anyone will come across this situation).
Thanks again.
Last edited by lupusarcanus; 12-24-2010 at 09:47 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.