BASH: How do you "split" the date command? - date

Cygwin user here (though if there's a suitable solution I will carry it over to K/Ubuntu, which I also use).
I have a Welcome message in my .bashrc that looks like the following:
SAD=(`date +%A-%B-%d-%Y`)
DUB=(`date -u +%k:%M`)
printf "Today's Date is: ${SAD}.\n"
printf "Dublin time is now ${DUB}. (24-Hour Clock)\n"
After numerous attempts to use whitespaces in the variable SAD above, I gave in and used hyphens. But I am, understandably, not satisfied with this band-aid solution. The problem, as I recall, was that every time I tried using quoted space, \s or some similar escape tag, along with the variables listed in the appropriate section of the GNU manpage for date, the variable for Year was either ignored or returned an error. What I do'nt want to have to do is resort to the full string as returned by date, but rather to keep the order in which I have things in the code above.
As I write this, it occurs to me that setting the IFS around this code for the Welcome message may work, provided I return it to defaults afterwards (the above appears at lines 13-17 of a 68-line .bashrc). However, I can't recall how to do that, nor am I sure that it would work.
Generally-speaking, .bashrc files are in valid BASH syntax, aren't they? Mine certainly resemble the scripts I've either written myself or tested from other sources. All I'm missing, I suppose, is the code for setting and unsetting the field separators around this message block.
No doubt anyone who comes up with a solution will be doing a favor not only to me, but to any other relative newbies on the Net taking their first (or thirteenth through seventeenth) steps in the world of shell scripting.
BZT

Putting
SAD=$(date "+%A %B %d %Y")
DUB=$(date -u +%k:%M)
echo "Today's Date is: ${SAD}."
echo "Dublin time is now ${DUB}. (24-Hour Clock)"
in my .bash_profile prints
Today's Date is: Thursday February 18 2010.
Dublin time is now 12:55. (24-Hour Clock)
I think that's what you want.

the problem is your array declaration.
SAD=(date +%A-%B-%d-%Y) just means you are putting the string "date" as element 0, and "+%A-%B-%d-%Y" as element 1. see for yourself
$ SAD=(date +%A-%B-%d-%Y) #<-- this is an array declaration
$ echo ${SAD[0]}
date
$ echo ${SAD[1]}
+%A-%B-%d-%Y
if you want the value of "date" command to be in a variable, use $(..), eg
$ SAD=$(date +%A-%B-%d-%Y)
$ echo ${SAD}
Thursday-February-18-2010

To get spaces, you need to quote the argument to date so that it's a single string. You're also erroneously declaring SAD and DUB as arrays, when what you really meant to do was evaluate them. Try this:
[/tmp]> $(date "+%A %B %d, %Y")
Thursday February 18, 2010

date +%A\ %B\ %d\ %Y

I found the combination that works:
SAD=$(date "+%A %B %d %Y")
echo $SAD
Thursday February 18 2010
Yet another instance when:
It pays to ask a question
It helps to know where to put your double quotes.
date obviously does not know from quoted space, but Bash does, so
it's a matter of "whispering in the right ear."
Thank you ghostdog74.
BZT

Related

Unix string to date conversion, what is wrong

I know the topic has already emerged and some of the posts give a good summary like the one here: Convert string to date in bash . Nevertheless, I encounter a problem presented below with an example I should solve:
date +'%d.%m.%y' works as desired and returns 05.12.20 but the inverse operation I should use to convert strings to date fails:
date -d "05.12.20" +'%d.%m.%y'
date: invalid date ‘05.12.20’
and this is exactly what I need. The Unix date formatting I have also checked on https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/ but it seems to be in line with that. What is the problem? I also tried to supply time zone indicators like CEST but they did not solve the problem.
Try
date -d "05-12-20" +'%d.%m.%y'
UNIX date expects either - or / as a date separator.
However, if your input really must be in the format "05.12.20" (i.e. using .), then you can convert it to the format expected by UNIX date:
date -d `echo "05.12.20" | sed 's/\./-/g'` +'%d.%m.%y'

Perl Strange -M Flag in 'If' statement

What is this flag?
if (-M ..filepath..)
what is the '-M' flag?
perldoc -f -M will answer your question...
This is the modification "age" of the file, in fractional days. That is, it is the number of days since the file was modified, as of the script start time (or as of some other time, if you explicitly set the $^T variable).
I sure hope that the actual code is along the lines of -M filepath > ...; just testing -M's result for truth is pointless.
Script start time minus file modification time (aka file modification age), in days.
In other words, it returns the age of OPERAND in days when the program started.
Also see a full list of file test operators in perldoc perlfunc (-X section)
Modification age (measured in days)
from http://www.devshed.com/c/a/Perl/File-Tests-in-Perl/
if we have something like this:
$age = -M FILE;
$age will contain the days since the file was modified.

zsh filename globbling/substitution

I am trying to create my first zsh completion script, in this case for the command netcfg.
Lame as it may sound I have stuck on the first hurdle, disclaimer, I know how to do this crudely, however I seek the "ZSH WAY" to do this.
I need to list the files in /etc/networking but only the files, not the directory component, so I do the following.
echo $(ls /etc/network.d/*(.))
/etc/network.d/ethernet-dhcp /etc/network.d/wireless-wpa-config
What I wanted was:
ethernet-dhcp wireless-wpa-config
So I try (excuse my naivity) :
echo ${(s/*\/)$(ls /etc/network.d/*(.))}
/etc/network.d/ethernet-dhcp /etc/network.d/wireless-wpa-config
It seems that this doesn't work, I'm sure there must be some clever way of doing this by splitting into an array and getting the last part but as I say, I'm complete noob at this.
Any advice gratefully received.
General note: There is no need to use ls to generate the filenames. You might as well use echo some*glob. But if you want to protect the possible embedded newline characters even that is a bad idea. The first example below globs directly into an array to protect embedded newlines. The second one uses printf to generate NUL terminated data to accomplish the same thing without using a variable.
It is easy to do if you are willing to use a variable:
typeset -a entries
entries=(/etc/network.d/*(.)) # generate the list
echo ${entries#/etc/network.d/} # strip the prefix from each one
You can also do it without a variable, but the extra stuff to isolate individual entries is a bit ugly:
# From the inside, to the outside:
# * glob the entries
# * NUL terminate them into a single string
# * split at NUL
# * strip the prefix from each one
echo ${${(0)"$(printf '%s\0' /etc/network.d/*(.))"}#/etc/network.d/}
Or, if you are going to use a subshell anyway (i.e. the command substitution in the previous example), just cd to the directory so it is not part of the glob expansion (plus, you do not have to repeat the directory name):
echo ${(0)"$(cd /etc/network.d && printf '%s\0' *(.))"}
Chris Johnsen's answer is full of useful information about zsh, however it doesn't mention the much simpler solution that works in this particular case:
echo /etc/network.d/*(:t)
This is using the t history modifier as a glob qualifier.
Thanks for your suggestions guys, having done yet more reading of ZSH and coming back to the problem a couple of days later, I think I've got a very terse solution which I would like to share for your benefit.
echo ${$(print /etc/network.d/*(.)):t}
I'm used to seeing basename(1) stripping off directory components; also, you can use echo /etc/network/* to get the file listing without running the external ls program. (Running external programs can slow down completion more than you'd like; I didn't find a zsh-builtin for basename, but that doesn't mean that there isn't one.)
Here's something I hope will help:
haig% for f in /etc/network/* ; do basename $f ; done
if-down.d
if-post-down.d
if-pre-up.d
if-up.d
interfaces

Internationalized date formatting with Zend_Date ( Japanese )

Disclaimer: you might need to install
a font/typeface which supports
Japanese if you see messed up
characters.
I'm trying to replicate what I've been doing so far with setlocale and strftime:
setlocale(LC_ALL, 'ja_JP.utf8');
$time = mktime();
echo strftime('%x', $time), '<br>';
Output:
2010年01月06日
Using Zend_Date - but I haven't been able to reproduce the same formatting with the japanese symbols for year, month and day.
Attempt #1:
$locale = new Zend_Locale('ja_JP');
$date = new Zend_Date( strtotime('yesterday'), null, $locale);
//echo $date->toString('YYYY abcdefghijklmnopqrstuvwxy M dE');
echo $date->get('YYYY MMM DD');
Output:
2010 1月 004
Attempt #2:
echo $date->get(Zend_Date::DATE_FULL);
Output:
2010年1月5日火曜日
My first attempt I can't seem to find a working constant to produce the YEAR and day symbols. The latter uses a standardized format but I need to customize it so there's a 0 preceding the month, and I want to be more in control.
In the future I may want to make it flexible so for example, en_US dates won't have those letters coming after the year/month/day but it would only apply to languages such as Japanese and others, where it's more common, or if I misunderstood and it isn't really common then please inform me.
Thanks in advance.
Seems what I needed was the DATE_LONG constant, which internally points to 'FFFF' - I'm trying to learn the inner workings of how the Date class corresponds with the Locale class to generate the whole string including the symbols now.
Update: I kept trying to find where it actually used date units instead of date formats, found the right data I need:
<dateFormatLength type="long">
<dateFormat>
<pattern>y年M月d日</pattern>
</dateFormat>
</dateFormatLength>
So it parses this and replaces the y, M, d, returns the formatted date.

How can I change the format of a date from the command line?

What's the quickest way to convert a date in one format, say
2008-06-01
to a date in another format, say
Sun 1st June 2008
The important bit is actually the 'Sun' because depending on the dayname, I may need to fiddle other things around - in a non-deterministic fashion. I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0).
[Background: The reason that I want to do it from the command line, is that what I really want is to write it into a TextMate command... It's an annoying task I have to do all the time in textMate.]
$ date -d '2005-06-30' +'%a %F'
Thu 2005-06-30
See man date for other format options.
This option is available on Linux, but not on Darwin. In Darwin, you can use the following syntax instead:
date -j -f "%Y-%m-%d" 2006-06-30 +"%a %F"
The -f argument specifies the input format and the + argument specifies the output format.
As pointed out by another poster below, you would be wise to use %u (numeric day of week) rather than %a to avoid localization issues.
Reading the date(1) manpage would have revealed:
-j Do not try to set the date. This allows you to use the -f flag
in addition to the + option to convert one date format to another.
Thanks for that sgm. So just so I can come back to refer to it -
date -j -f "%Y-%m-%d" "2008-01-03" +"%a%e %b %Y"
^ ^ ^
parse using | output using
this format | this format
|
date expressed in
parsing format
Thu 3 Jan 2008
Thanks.
date -d yyyy-mm-dd
If you want more control over formatting, you can also add it like this:
date -d yyyy-mm-dd +%a
to just get the Sun part that you say you want.
date -d ...
doesn't seem to cut it, as I get a usage error:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0), and as far as the man goes, date -d is just for
-d dst Set the kernel's value for daylight saving time. If dst is non-
zero, future calls to gettimeofday(2) will return a non-zero for
tz_dsttime.
If you're just looking to get the day of the week, don't try to match strings. That breaks when the locale changes. The %u format give you the day number:
$ date -j -f "%Y-%m-%d" "2008-01-03" +"%u"
4
And indeed, that was a Thursday. You might use that number to index into an array you have in your program, or just use the number itself.
See the date and strftime man pages for more details. The date manpage on OS X is the wrong one, though, since it doesn't list these options that work.