Today I wanted to write a script that I could execute from both *nix (using Bash or a similar shell) and on Windows (from a command prompt, i.e. a batch file). I found Max Norin’s solution which works, but has a few limitations, e.g. when run it outputs either the
word “off” when run in *nix or the word “goto” when run on Windows. There’s got to be a sneaky solution, right?
Here’s my improved version:
1
2
3
4
5
6
7
8
9
10
11
12
@goto(){
# Linux code here
uname -o
}
@goto $@
exit
:(){
@echooffrem Windows script hereecho%OS%
Mine exploits the fact that batch files can prefix commands with @ to suppress outputting them as they execute. So @goto can be a valid function name in
bash/zsh etc. but is interpreted as a literal goto command in a Windows Command Prompt. This allows me to move the echo off command –
which only has meaning to Windows – into the Windows section of the script and suppress it with @.
The file above can be saved as e.g. myfile.cmd and will execute in a Windows Command Prompt (or in MS-DOS) or your favourite *nix OS. Works in MacOS/BSD too, although
obviously any more-sophisticated script is going to have to start working around the differences between GNU and non-GNU versions of core utilities, which is always a bit of a pain!
Won’t work in sh because you can’t define functions like that.
But the short of it is you can run this on a stock *nix OS and get:
$ ./myfile.cmd
GNU/Linux
And you can run it on Windows and get:
> .\myfile.cmd
Windows_NT
You can’t put a shebang at the top because Windows hates it, but there might be a solution using PowerShell scripts (which use
hashes for comments: that’s worth thinking about!). In any case: if the interpreter strictly matters you’ll probably want to shell to it on line 3 with e.g. bash -c.
Why would you want such a thing? I’m not sure. But there is is, if you do.
This post is also available as an article. So if you'd rather read a
conventional blog post of this content, you can!
This video accompanies a blog post of the same title. The content is basically the same – if you prefer videos, watch this video. If you prefer blog posts, go read
the blog post. If you’re a superfan, try both and spot the differences. You weirdo.
There are a great number of things that I’m bad at. One thing I’m bad at (but that I’m trying to get better at) is being more-accepting of the fact that there are things that I am bad
at.
As a young kid, I was a smart cookie. I benefited from being an only child and getting lots of attention from a pair of clever parents, but I was also pretty bright and a quick learner
with an interest in just about anything I tried. This made me appear naturally talented at a great many things, and – pushed-on by the praise of teachers, peers, and others – I
discovered that I could “coast” pretty easily.
But a flair for things will only carry you so far, and a problem with not having to work hard at your education means that you don’t learn how to learn. I got bitten
by this when I was in higher education, when I found that I actually had to work at getting new information to stick in my head (of course, being older makes learning harder
too, as became especially obvious to me during my most-recent qualification)!
A side-effect of these formative experiences is that I grew into an adult who strongly differentiated between two distinct classes of activities:
Things I was good at, either because of talent or because I’d thoroughly studied them already. I experienced people’s admiration and respect when I practised these
things, and it took little effort to stay “on top” of these fields, and
Things I was bad at, because I didn’t have a natural aptitude and hadn’t yet put the time in to learning them. We don’t often give adults external
reinforcement for “trying hard”, and I’d become somewhat addicted to being seen as awesome… so I shied away from things I was “bad at”.
The net result: I missed out on opportunities to learn new things, simply because I didn’t want to be seen as going through the “amateur” phase. In hindsight, that’s
really disappointing! And this “I’m bad at (new) things” attitude definitely fed into the imposter syndrome I felt when I first
started at Automattic.
Being Better
Leaving the Bodleian after 8½ years might have helped stimulate a change in me. I’d carved out a role for myself defined by the fields I knew
best; advancing my career would require that I could learn new things. But beyond that, I benefited from my new employer whose “creed
culture” strongly promotes continuous learning (I’ve vlogged about this before), and from my coach who’s been great at encouraging me towards a growth mindset.
But perhaps the biggest stimulus to remind me to keep actively learning, even (especially?) when it’s hard, might have been the pandemic. Going slightly crazy with cabin fever during
the second lockdown, I decided to try and teach myself how to play the piano. Turns out I wasn’t alone, as I’ve mentioned before: the pandemic did strange things to us all.
I have no real experience of music; I didn’t even get to play recorder in primary school. And I’ve certainly got no talent for it (I can hear well enough to tell how awful my
singing is, but that’s more a curse than a blessing). Also, every single beginners’ book and video course I looked at starts from the assumption that you’re going to want to “feel” your
way into it, and that just didn’t sit well with the way my brain works.
I wanted a theoretical background before I even sat down at a keyboard, so I took a free online course in music theory. Then I started working through a
“beginners’ piano” book we got for the kids. Then I graduated to “first 50 Disney songs”, because I know how virtually all of them sound well enough that I’d be able to hear where I was
going wrong. Since then, I’ve started gradually making my way through a transcription of Einaudi’s Islands. Feeling like I’d got a good handle on what I was supposed to be
doing, I then took inspiration from a book JTA gave me and started trying to improvise.
Most days, I get no more than about 10 minutes on the piano. But little by little, day by day, that’s enough to learn. Nowadays even my inner critic perfectionist can
tolerate hearing myself play. And while I know that I’ll probably never be as good as, say, the average 8-year-old on YouTube, I’m content in my limited capacity.
If I’m trying to cultivate my wonder syndrome, I need to stay alert for “things I’m bad at” that I could conceivably be better at if
I were just brave enough to try to learn. I’m now proudly an “embarrassingly amateur” pianist, which I’m at-long-last growing to see as better than a being non-pianist.
Off the back of that experience, I’m going to try to spend more time doing things that I’m bad at. And I’d encourage you to do the same.
This morning two men from the council turned up at my door and asked if they could borrow my driveway to park their vehicle. We got chatting, and it turned out that they were going to
be working on footpath maintenance nearby. Realising where they meant, I asked for more information about their work: their plan was to remove the footbridge which acts as the home to
this geocache, and replace it with a new one a couple of metres over in order to bring the path in-line with its “correct” location!
So I wandered out with them and removed the geocache before they got started on removing the bridge. I might be able to replace it after the new bridge is built, but – based on their
description of the new bridge – it might need to be a different design of cache, so for now I’m archiving this one. This is perhaps my happiest cache-archiving ever.
This one was a challenge. Finding myself on a path alongside a wooden fence, I propped my bike up against the fence to search what seemed to be the obvious candidates, but no look. I
returned to my bike to retrieve my handlebar-mounted GPSr to try to get a better fix and soon found myself tramping through waist-high
nettles towards to GZ. Oh! There’s another path over here! That makes more sense. Found the cache pretty quickly once I’d got my silly self
into the right place! TFTC!
Good sized cache! My GPSr pointed me into the field at first and I had to double back. Log dry, but has clearly gotten wet in the past as done pages have begun to moulder. Okay to sign
still, though. TFTC.
Came near here on an evening’s cycle from Stanton Harcourt and, seeing that the path wasn’t too muddy, decided to come and explore the woods. Lots of birdsong tonight! Was glad of the
hint which saved me poking in the wrong holes. TFTC.
I love my electric car, but sometimes – like when I need to transport five people and a week’s worth of their luggage 250 miles and need to get there before the kids’ bedtime! – I still
use our big ol’ diesel-burning beast. And it was while preparing for such a journey that I recently got to thinking about the mathematics of refuelling.
It’s rarely worth travelling out-of-your-way to get the best fuel
prices. But when you’re on a long road trip anyway and you’re likely to pass dozens of filling stations as a matter of course, you might as well think at least a
little about pulling over at the cheapest.
You could use one of the many online services to help with this, of course… but assuming you didn’t do this and you’re already on the road, is there a better strategy than just trusting
your gut and saying “that’s good value!” when you see a good price?
Estimate your outstanding range R: how much further can you go? Your car might be able to help you with this. Let’s say we’ve got 82 miles in the tank.
Estimate the average distance between filling stations on your route, D. You can do this as-you-go by counting them over a fixed distance and continue from
step #4 as you do so, and it’ll only really mess you up if there are very few. Maybe we’re on a big trunk road and there’s a filling station about every 5 miles.
Divide R by D to get F: the number of filling stations you expect to pass before you completely run out of fuel. Round down,
obviously, unless you’re happy to push your vehicle to the “next” one when it breaks down. In our example above, that gives us 16 filling stations we’ll probably see before we’re
stranded.
Divide F by e to get T (use e = 2.72 if you’re having to do this in your head). Round down again, for the same reason as before.
This gives us T=5.
Drive past the next T filling stations and remember the lowest price you see. Don’t stop for fuel at any of these.
Keep driving, and stop at the first filling station where the fuel is the same price or cheaper than the cheapest you’ve seen so far.
This is a modified variant of the Secretary Problem because it’s possible for two filling stations to have the same price, and that’s reflected in the algorithm above by the
allowance for stopping for fuel at the same price as the best you saw during your sampling phase. It’s probably preferable to purchase sub-optimally than to run completely dry, right?
Of course, you’re still never guaranteed a good solution with this approach, but it maximises your odds. Your own risk-assessment might rank “not breaking down” over pure mathematical
efficiency, and that’s on you.
Almost nerdsniped myself when I discovered several #WordPress plugins that didn’t quite
do what I needed. Considered writing an overarching one to “solve” the problem. Then I remembered @xkcdcomic
927…