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
Post a Comment