What are we learning?
By the end of this lesson you will have:
Created and edited text files inside a Linux BASH shell
Created and run basic BASH scripts
Reviewed some basic programming constructs
The Lesson
Resources: BASH Scripting for Beginners | BASH Syntax Cheat Sheet | Official BASH Reference
Aliases:
Since BASH is a shell to the kernel and a scripting language, it can be very powerful
Let's create an alias to update our system so that we don't have to type sudo apt update and sudo apt upgrade every time we want to check for updates.
We're going to edit/create a file called .bash_aliases
You can use vim or nano
We create an alias with alias <name>='<command>'
Example: alias update='sudo apt update'
We'll create one for upgrading as well: alias upgrade='sudo apt -y upgrade'
The alias will take place when we exit and restart the terminal or we can use source .bash_aliases
Now to upgrade the computer we only need to run update && upgrade
Scripts:
Let's make our own executable program using BASH
Right now we can run the himom.sh script using bash himom.sh but wouldn't it be great if we could just run himom.sh?
Where does the bash executable live? Run: which bash
Edit your himom.sh script and add a "she-bang" at the top to express how to run it
#!/usr/bin/bash
echo Hi Mom!Save and exit the editor
Try to run the script: himom.sh (it won't run)
The file is still just a text file and is not listed as "executable". What proof? Check with ls -l
To make the file executable, use the chmod (change mode) tool: chmod +x himom.sh
Great! Now it'll run, right? himom.sh ... nope. Command not found?
Binaries (programs) should be in /bin or /usr/bin
The ADD script:
Create/edit the file add.sh and add the she-bang
At first, let's just make the program add 9 + 5: echo 9 + 5 (save and exit, make the file executable and run it)
Why didn't it do the math?
Edit the file again - this time, we'll tell BASH that "9 + 5" is an expression: echo $[9 + 5] (save, exit, execute)
Great! But that is a boring program... Let's make it add any two numbers...
Edit the file and let's change the expression to this: echo $[$1 + $2] (save, exit)
Run the program like this: ./add.sh 9 33
If everything goes well, it should output 42
BASH is a programming (scripting) language, so it has all the constructs you're used to, including if-statements!
Let's modify / rewrite the add.sh script so that it checks if we've provided enough arguments.
#!/usr/bin/bash
if [[ $# == 0 ]] then
echo "Null"
elif [[ $# == 1 ]] then
echo $1
elif [[ $# == 2 ]] then
echo $[ $1 + $2 ]
else
echo "Too many arguments"
fi
Test your program!
Your Tasks
1. Dog Treats Create the script treats.sh such that it is executable and it solves the following problem:
Barley the dog loves treats. At the end of the day he is either happy or sad depending on the number and size of treats he receives throughout the day. The treats come in three sizes: small, medium, and large. His happiness score can be measured using the following formula: 1 x S + 2 x M + 3 x L
where S is the number of small treats, M is the number of medium treats and L is the number of large treats.
If Barley’s happiness score is 10 or greater, then he is happy. Otherwise, he is sad. Determine whether Barley is happy or sad at the end of the day.
Input Specification
Your program will take three command-line arguments (no more, no less). Assume correct input. The first argument contains the number of small treats, S, the second is the number of medium treats, M, and the third is the number of large treats, L, that Barley receives in a day.
Output Specification
If Barley’s happiness score is 10 or greater, output happy. Otherwise, output sad.
Samples:
~$ ./treats.sh 0 0 0
sad
~$ ./treats.sh 3 1 0
sad
~$ ./treats.sh 3 2 1
happy
(for a 4+)
2. Sum Using the references linked at the top of this page and the images to the right, create the BASH script sum.sh such that it follows these constraints:
It is executable via ./sum.sh
If no command-line arguments are given, it outputs 0
The script should sum all command-line arguments and output the result.
It is assumed that all command-line arguments given are an element of the Integers.
Examples:
~$ ./sum.sh
0
~$ ./sum.sh -8
-8
~$ ./sum.sh 1 2 3 -7
-1
Submitting Code
Go to our Google Classroom > Classwork and find the "3 - BASH Scripting" task. Complete and submit!