Skip to main content

Exploring CLI: Better Bash Scripts with Bashly Part 3

 

This will be the last article I do on bash scripting for now. I want to shift gears for a few and explore some other languages. This project is called 'rmspaces'. It's another simple little program created with bash and Bashly for removing the spaces in filenames. It's super simple and does something super simple but useful. Yet, as simple as it may have seemed, I actually had a little trouble building this one that I will get into soon.

Just as with the last two articles, I used Bashly to generate the finished script.

The following bit I was able to re-use from the previous script. It checks for input being piped or redirected to stdin and if that is empty then it will parse the command line and separate multiple files passed as a comma separated list or a single item and assigns it to an array:

The meat of the work is the part after that where the script splits the filename from its extension then after changing that string puts it back together with the modified string then renames the file. This is the part where I had some trouble. MacOS likes to put strange non-printables in filenames. So what looks like a normal space is something else. I ran into this renaming screenshots. Between the time and AM/PM strings of the filenames it places a 'Narrow No-Break Space' (U+202F). Using '${variable// /}' doesn't affect this space character. Running the string through sed and processing it as a space causes some really strange behavior. To fix this, I had to set allowed characters in the sed command.
Once over that hurdle it was a fairly simple little script. I really enjoyed playing with Bashly. It made creating my own custom CLI commands easy. The code for this project can be viewed and cloned from the Github Repo below.

Comments

Popular posts from this blog

2012 iMac Upgrade

I’ve always been fascinated by the longevity of Apple products. They build incredible machines, and unfortunately, Apple’s support lifecycle often leaves them abandoned – a growing mountain of e-waste. Recently, I was scrolling through the marketplace, and I spotted a treasure: a 2012 21.5” iMac for a ridiculously low $100. It was a gamble, a leap of faith, and frankly, I had no idea what I was getting into. I dove headfirst, did some quick research, and the result was...well, let’s just say it was an adventure. I’ll share the full story of my $100 Mac rescue – including the initial challenges, the surprising discoveries, and what I learned along the way. iMac 13,1 Specifications When introduced on October 23, 2012, this particular iMac came in three options: 2.7 GHz Core i5 (I5-3330S) Order: MD093LL/A  Model: A1418 (EMC 2544) RAM: 8 GB VRAM: 512 MB Storage: 1 TB HDD 2.9 GHz Core i5 (I5-3470S) Order: MD094LL/A Model: A1418 (EMC 2544) RAM: 8 GB VRAM: 512 MB Storage: 1 TB HDD 3.1 GHz...

Exploring CLI: Better Bash Scripts with Bashly Part 2

  I enjoyed the srcset project so much that I want to create a set of scripts for creating and managing my projects. In recent Linux news it's been said that the home directory will have a Projects folder by default. I have always created this folder anyway and the scripts I will be creating assume this path exists or will create it if it doesn't. This post will spotlight the new-website command. Just as with the last article, I used Bashly  to generate the finished script. To recap what I said in the first part of this series, I aim for my scripts to follow some basic guidelines when possible and ideal: a command does one thing and does it well a command should be able to accept data from pipe The new-website script, like the previous srcset project, is simple. I bounce back and forth between different project directory structures for websites and I am aiming to try and stick to one convention and have some uniformity (writing this I am thinking of changing it already in the ...

Apple Magic Keyboard 2 Battery Replacement

Facebook Marketplace is a great way to get some good stuff super cheap. Sellers are often not very honest or not knowledgable about the items they have which can make it a little tricky to navigate. Sometimes, they are both of those things and even if someone is attempting to rip you off you can still come out on top with a little technical knowledge. Recently I found a listing for this Apple Magic Keyboard (lighting port) combo listed for $35. Well, as you can see in the image below this combo still sells on Amazon for $120+ used. The items on marketplace even came with the original packaging. I was skeptical, but even if only one of the pair worked I was coming out on top so I offered $25 and got it. The whole exchange was weird. I picked up the items and went straight home and put both on a charger for a while to see what I had. The mouse worked perfectly. The keyboard however, did not. One thing about the newer Apple Keyboards I can't stand is the absence of an indicator light ...