Converting A Bash Shell CLI Into An Executable Script on macOS Finder

By | April 2, 2023

I was doing a certain task that required me to rename the whole files in the folder with an extension of JPG. Initially, as a person who loves to do tasks using CLI (command line interface), this is an easy task. Find the pattern of the files, that is only three digits of number, then using AWK to print the command utilizing mv internal command in bash, then execute it using sh. This is the snippet code for the bash shell:

ls | egrep '[0-9]{3}$' | awk '{ print "mv "$0" "$0".jpg" }' | sh

The script worked as a charm, nothing special on it.

Problem #1: It has to be executed on Terminal.app and type it

Yes, it can be easily saved as a shell script file and based on this stackoverflow answer, give the extension of .command. Don’t forget to allow this file to be executed using command chmod

chmod a+x renameToJPG.command

Otherwise, you will encounter this error while you double click it.

The script could not be executed because you do not have appropriate access privilege

The script can be executed and the Terminal.app window showed up with the unexpected result: the files were not renamed as expected

The script executed without error, but…
… the files were not renamed as expected

Problem #2: The script did not rename the files as intended

Yes, the script is not renaming the file in the same folder because by default the script was executed at home folder. Let’s create a simple script to:

  1. Get the folder where the script was saved
  2. Change working directory to the folder
  3. Execute script from there

This can be achieved by invoking command dirname in macOS. This command takes one mandatory argument, that is a string of filename, which we can get by referring to variable $0.

Hence, the script to test is:
echo "Current working directory where the script was executed: "$PWD
echo "How the script was invoked: "$0
target=$(dirname $0)
echo "The folder where the script located when executed: "$target
cd $target
echo "Current Working directory has been changed to: "$PWD

With the output was:

The script can be executed from the folder…

Then using the script, I thought it would solve the problem, but it is not. There’s a problem when the working folder contains spaces. Yes, that’s why I hate filename or folder name with spaces.

… but the script execution was failed due to spaces in the folder name

Problem #3: The script did not recognized the folder name with space

Looking at the previous output, it is obvious that the command to change directory or cd is not able to go to the correct folder, due to space being the separator for the next arguments in bash shell.

So, the script should be modified a little bit, so that for all parameters for the folder name, they should be surrounded with double quotes. The script should be changed something like this:

echo "Current working directory where the script was executed: "$PWD
echo "How the script was invoked: "$0
target=$(dirname “$0”)
echo "The folder where the script located when executed: "$target
cd “$target”
echo "Current Working directory has been changed to: "$PWD

Using the script above, I was able to rename multiple files as intended.

Notice the folder name is “Script Test” that contains a space? It worked.

…. And it’s done!

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.