Bash Scripts with Arguments

Make your Mac great again. With full example code!

Photo by Stanislav Kondratiev on Unsplash

Prerequisites:

  • Some knowledge of the Mac terminal (guide HERE)
  • This tutorial is going to use VIM (guide HERE), but you could use any text editor

Terminology

Bash: “Bourne again shell”. There are many shells that can run UNIX commands, and the one used on the Mac Terminal is Bash

Justification

I love ffmpeg, and it is a great way to create animated gif files for use, well on Medium (this is my use).

A little help…

Where each of the following commands are prefixed by, $, which indicates that this should be typed into the terminal. To make this clear I’ve written it as a quote followed by the $ symbol so you will see the following (followed by a command to type) within this guide:

Prework: Installing ffmpeg

To do so you will need to install homebrew through

Implementing your bash script

Creating the bash script

Using VIM we create our bash script using the following command from the command line.

Add the shebang line

From within vim we need to tell the command line to run the code using bash.

#!/usr/bin/env bash

Add the script

FFMPEG has a number of parameters that we can pass to it. The reason for this script is to have less of them, and to make it a bit easier to use FFMPEG.

INPUT=${1?Error: no input filename given}
OUTPUT=${2?Error: no output.gif filename given}
FRAMESIZE=${3:-1200x720}

ffmpeg -i $INPUT -s $FRAMESIZE -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > $OUTPUT
echo "FFMPEG script has been run"

Update the script to have a help function (optional)

In the script, there is a function that will be called if we request help, and this simply echo (that is, prints) values to the console

Syntax: ffmpegscript input.mov output.gif FrameSizeWxH

Save the script

From within VIM we can (if we were editing a quick nudge of the escape key will allow us to do this) use the save command :wq

Make the script executable

From terminal run the following to make the script executable

Run the script

From the terminal we run the script. The ./ part of this command indicates that the file is in the same folder. Now when you type a command the shell looks up it’s list of directories as specified by the PATH variable (and we shall place it there in the next part of this guide).

Make the script Globally available

Make the script available globally

:wq

Making further edits

Where further edits are made (from the original folder) these changes need to be copied back to the /usr/local/bin folder so the new version can be run. The way to do this? cp once again!

The full script:

Conclusion:

Writing Bash scripts like this can save a great deal of time. This example script uses ffmeg to create an animated gif from a movie. We can access this script from anywhere; well done!

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store