I love the Cygwin package on Windoze. I'm more of a Unix guy and can't stand the Windows command prompt. I much prefer a standard shell such as bash across all platforms. At my current job, I'm doing most of my development on Windows and our setup makes it so that I have to rely on Git bash more but I still have Cygwin installed and use it for things that aren't so tied to work. Git bash pisses me off sometimes with relation to copy and paste, especially inside the vi editor! What's up with that?
So this post's focus is on the date command. One of my few peeves about Cygwin is the default behavior of the command on Windows:
Tue, Apr 3, 2018 7:26:52 AMThe whole reason for using Cygwin was to have a shell environment that looks and acts like Unix. This does not look like Unix. And GNU being what it is, Git bash works the same way! Yes, my OCD is obvious... some people might say Well, that's close but I don't like this behavior! Unix is supposed to bring order to chaos!
Sure, there are options to override the behavior and I could have swore that they had a single simple option (or maybe some kind of environment variable??) to print in Unix form:
$ date +"%a %b %e %H:%M:%S %Z %Y" Tue Apr 3 07:28:44 EDT 2018 $This is not simple and it ticks me off that I have to do this. Computers are supposed to make things easier!
It's also easy to create an alias to specify this option but what if you want to specify a different format? I came up with a simple script to invoke the date and conditionally add the Unix-style format option:
#! /usr/bin/python | |
import sys | |
import subprocess | |
cmd = sys.argv[1:] | |
if not any(word.startswith("+") for word in cmd): | |
cmd.insert(0, "+%a %b %e %H:%M:%S %Z %Y") | |
cmd.insert(0, "date") | |
exit(subprocess.Popen(cmd).wait()) |
I plan to store this script as ~/bin/unixdate and use an alias in Windows shells to invoke this whenever I type date.
2019/09/13 update: I came up with a technique to create an alias for Windoze only:
# windoze aliases if expr match "$(uname -s)" '.*[Ww][Ii][Nn]' >/dev/null 2>&1 then alias date=unixdate fi
I did this because I recently started a new job and after using the Windoze laptop for about a month, I realized I had neglected to create an alias to use my unixdate script.
Yes, I could have been more specific and just checked for cygwin but I mostly only using Cygwin on Windoze so I'm happy with this. I am adding this to misc/.bashrc in my toys repo.
Shortly after I published this blog, I realized I was only testing for a "+" option where the format can be expressed in excruciating detail. I forgot that that there were other options so I fixed that although I'm not going to bother updating the script shown here. If you're really interested, you can check out the script in my tools & tools repo: https://github.com/pfuntner/toys/blob/master/bin/unixdate
ReplyDeleteFollow the Gist at https://gist.github.com/pfuntner/ed3a7f9591c214e05ce320bf7a9df59d#file-unixdate for yet another snag in the script. Oh what a tangled web, indeed!
ReplyDelete