Now we're ready to get to the fun and awesome part of the command line, scripting!

A script is nothing more than a series of commands or instructions that we can execute at any given time. To explain how we can code one, we'll use a simple example that will allow us to create a file with some content in it by running a single command with the name of the file and the text to write.

This is a really simple example that we’ve seen until now can also be done without the need for the script, but bear with me on this one, we’re going to complicate it a bit.

  • The first thing to do is create a .sh file. You can put it wherever want. I called mine createFile.sh.

  • Then open it on your text/code editor of choice.

  • In our first line, we'll write the following: #!/bin/sh

This is called a shebang, and its function is to declare what shell is going to run this script.

Remember previously on the Git CLI intro when we mentioned that we can use a given shell for general interaction and another given shell for executing a script? Well, the shebang is the instruction that dictates what shell runs the script.

As mentioned too, we're using a "stripped down" shell (also known as sh shells) to run the scripts as they're more efficient.

If we wanted this script to run with bash the shebang would be #!/bin/bash

  • Our next line will be fileName=$1

Here we're declaring a variable called fileName, and assigning it to the value of the first parameter the script receives.

A parameter is a set of characters entered after the script/command. Like with the cd command, we need to specify a directory parameter in order to change the directory (ie: cd testFolder).

A way we can identify parameters within a script is by using a dollar sign and the order in which that parameter is expected.

If I'm expecting more than one parameter I could write:

paramOne=$1 paramTwo=$2 paramThree=$3 ...

  • So we're expecting the file name as a parameter of our script. But what happens if the user forgets to enter it? We need to plan for that so next we're going to code a conditional that keeps asking the user to enter the file name until that parameter is received.

We can do that like this:

while [ -z "$fileName" ] do echo 'Provide a file name' read -r -p $'File name:' fileName done

What we're doing here is:

  1. While the fileName variable is not assigned (while [ -z "$fileName" ])

  2. Write to the console this message (echo 'Provide a file name')

  3. Then read whatever input the user provides and assign the input to the fileName variable (read -r -p $'File name:' fileName)

Now that we have our file name in place, we can do the same thing for the second argument to provide the text that we’re going to write and store the value to the variable called text like this text=$2

In order to write the content to the file that we want we add the following command echo $text >> $fileName

In the end, the content of our file would be like this:

#!/bin/sh fileName=$1 text=$2 while [ -z "$fileName" ] do echo 'Provide a file name' read -r -p $'File name:' fileName done echo $text >> $fileName

Exercise

Create a shell script with a helper menu to curl the geo data based on country code and zip code from the following endpoint (https://api.zippopotam.us/us/33162) by providing the country code in lowercase and zip code as arguments and outputting the response.