Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoneybun committed Dec 5, 2023
1 parent 16e210a commit 2bad323
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions content/bash-scripting-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ tableOfContents: true
---

# Scripting Basics

Check failure on line 17 in content/bash-scripting-basics.md

View workflow job for this annotation

GitHub Actions / Lint (Markdown)

Multiple top-level headings in the same document [Context: "# Scripting Basics"]

We will be writing a script for [BASH (Bourne Again Shell)](https://en.wikipedia.org/wiki/Bash_(Unix_shell)). There are many different languages and tools to create scripts for your tasks. Some languages are better suited for specific tasks than others. BASH is a great starting point, as it uses the same language as your built in terminal commands that already exist in your Linux OS.

**NOTE:** This tutorial assumes you are running Pop!_OS or Ubuntu, but the script will work on any Linux/Unix OS with BASH.

## Creating a working script

There are 2 important details for writing a BASH script that make it functional. The first is called the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)). The `shebang` tells your OS what scripting language is being used (in this case, we are using BASH). The `shebang` is usually the first line in the script.

Open your text editor of choice (I will be using `gedit`, as it comes with Pop!_OS by default). In the text editor, add the following line to as the very first line in your script:

```
#!/usr/bin/env bash
```

The line above is the `shebang`. There are many ways to create scripts, and the `shebang` will be crucial for the OS to handle your script correctly.

Let's create a directory to save your scripts. This will keep your Home Directory tidy, and also keep a dedicated place for things you are working on. In my case, I have chosen to create a directory named `scripts` in my Home Directory. Next, let's save our script that we are writing to this newly created `~/scripts/` directory. You can name this script whatever you like, but for the sake of this tutorial, I am going to name this script `basics.sh`. Note that I gave the file a `.sh` extension. This is not necessary for the script to work, but it will help you as a user know that this is a BASH script because it ends with `.sh`. Other scripting languages use similar naming conventions (Python scripts end with `.py`, or LUA scripts end with `.lua`).

The second crucial detail for your OS to properly run your script is to make it executable. As of right now, your script is just a regular text file. In order for your OS to run your script, we will need to make this text file `executable`. This can be done by navigating to our `~/scripts/` directory with our File Manager. Once you locate your script file, right click on the file to bring up with context menu and select "Properties". This will bring up the various details about the file we are looking at. Click on the tab labelled "Permissions". There will be a checkbox labelled "Allow executing file as program". Make sure to check this box. Once this is enabled, the script will be able to run.

At the moment, the script will do nothing if we decided to execute it, so let's add some output and then test our script. Open the script and add the following line below your first `shebang` line:

```
echo "The script executed correctly."
```
Expand All @@ -42,34 +47,45 @@ We can now attempt to run our script. Open the Terminal from your Application La
```
cd ~/scripts
```

You can now run your script by running the following command in your Terminal:

```
./basics.sh
```

If it worked, your terminal should output the following line:

`The script executed correctly.`

Congratulations! You have created a working BASH script. Of course, outputting a single line does not provide much functionality, so we will be doing just that in the next section.

## Adding functionality

At this point, our script does one thing: It outputs a single line in our Terminal. This works, but it does not provide any use case. There are many things you can do with a script if you can get creative and combine several commands together to create something useful. There is a lot to cover, and there are too many things that can be created for me to cover in this. If you would like to explore more ideas, a good place to find useful commands you can use in your script is by searching for what you need to accomplish, and then after finding the appropriate command, add it to a script.

### Variables

Let's start with asking the user for information like their name and favorite color. In order to output this information, we will need to first ask for this input, and then after it is input, we will store the information into a `variable`. Variables can hold values, and can be changed after the variable is created, so you can adjust values later on.

In our script file, we should have 2 lines already created: the `shebang` and our `echo` command that outputs a line. Let's change the `echo` command to match the "theme" of our script. Open the script in your text editor and edit the 2nd line with the `echo` command to ask the user for their First Name:

```
echo "What is your first name?"
```

Next, we will be using the `read` command to allow the user to input text. The text they enter will be stored in a variable that we can choose the name of. In this case, we will call the variable `firstName`. You can name the variable whatever you would like, but be sure to adjust your script accordingly. In our text editor, create a new line below our first `echo` command and input the following line:

```
read firstName
```

If we were to run our script in our Terminal, it will first output a line that asks "What is your first name?". It will then sit and wait for the user to input text. Once the user inputs text and presses Enter, the script will end. We have not given the script any commands to use this information that the user has input. Let's add some output that shows what the user has typed in their Terminal. Add a new line to your script:

```
echo "Your first name is: $firstName"
```

Notice that in order to use our `firstName` variable, we use the `$` in front of it. This will tell our terminal that we intend to use the variable called `firstName`. If I were to run the script, it will ask for my first name. I will input the name "Garrett", and the script will then output the following line:
`Your first name is: Garrett`

Expand All @@ -79,7 +95,9 @@ echo "What is your favorite color?"
read favoriteColor
echo "Your favorite color is: $favoriteColor"
```

At this point, you should have a working script that asks the user for their name and their favorite color and then reacts to this information by outputting a sentence that reflects this information. The full script at this point should look like this:

```
#!/usr/bin/env bash
echo "What is your first name?"
Expand All @@ -91,20 +109,26 @@ echo "Your favorite color is: $favoriteColor"
```

### If-Then Statements

Sometimes we want different things to happen depending on what inputs/variables are stored. For instance: if a file exists, then do *a*, but if the file does not exist, then do *b*. Or if a number is higher than *x* then do *a*, but if the number is lower than *x*, then do *b*. We can do this with an `if` statement.

For this, we will be asking the user to input a number. If the number is higher than *x* we will do *a*, but if the number is lower than *x*, we will do *b*.

Add a new line to your script. We will be making the target number 23, but you can pick any number you want. We will be putting the target number into a variable called `targetNumber`. Add the following line to your script:

```
targetNumber=23
```

We will now ask the user to input a number, and store that number to a variable called `guess`. Add the following lines to your script:

```
echo "Guess the number:"
read guess
```

We now need to create and `if` statement to compare the `targetNumber` to the user input `guess` variable. The script will output different sentences depending on if the user is too low, too high, or if they guess correctly. Add the following lines to your script:

```
if [ $guess == $targetNumber ]
then
Expand All @@ -122,9 +146,11 @@ fi
If you run the script, it will ask the user for their name and their favorite color. It will then ask the user to input a number. After the user inputs the number, depending on if it was lower, higher or correct, the script will dynamically adapt to the user input.

## Conclusion

BASH scripting can be very useful in many different cases. I use scripting to automatically create a new work note with the current date as the title of the note document. I also have scripts to open certain applications or change my desktop theme. I have developed scripts to convert audio files to a specific format, or fill out PDF documents quickly. I am still learning myself, so don't get discouraged or frustrated. Try to keep at it and as you make mistakes, you will learn for the next script you write. There are so many resources to help with your project. I have listed a few places to help with this below.

## Useful places to find information

Scripting can get advanced and complicated really quickly. There are numerous resources online for tips, tutorials, and forums to get help. Here are a few places to find ideas:

- [GNU Bash Manual](https://www.gnu.org/software/bash/manual/bash.html)
Expand Down

0 comments on commit 2bad323

Please sign in to comment.