Shell Scripting
Connecting to a linux terminal
We'll be using Docker Compose to mount the repository into an Ubuntu Image. Create a docker-compose.yaml
file in the root of the repository and copy the contents below.
version: '3.9'
services:
devel:
image: ubuntu:20.04
working_dir: /repository
volumes:
- './:/repository'
command: bash
You'll probably notice that this looks very similar to the docker-compose.yaml
file you wrote in lab 1. The main differences are that we are using version 20.04
of the ubuntu
image, and running bash at startup.
We also changed the name to devel
to better represent how we will be using the container.
You can run the devel
service.
docker-compose run devel
Creating <folder_name>_devel_run ... done
Now, as you are connected to the ubuntu terminal, you should see your working directory is /repository
and your files match those on the host machine.
pwd
/repository
ls
README.md course-catalog.txt docker-compose.yaml
Shell Scripting
Why use shell scripting?
Automation: Perform routine tasks, like backups, updates, and system monitoring.
Efficiency: Reduce the time spent manually typing commands.
Reproducibility: Ensure that tasks are performed the same way each time.
Integration: Combine tools and commands into cohesive workflows.
Getting started
Let's practice some linux commands that we can use to navigate the file system.
Earlier, we used the pwd
command to show our location in the filesystem. We could also use echo
to show the PWD
environment variable.
echo $PWD
/repository
Create a file named tmp.sh
.
touch tmp.sh
Install the vim editor. Enter "yes" on the prompt that appears.
apt-get update && apt-get install vim
Open the file with the vim editor.
vim tmp.sh
Type "i" to enter insert mode, then write the following line into the file.
echo $HOME
Save and close the file. In the editor press escape (esc) and type: :wq
List the contents of the current directory:
ls -l
Now let's execute the file using the following method:
./tmp.sh
Did it work? Why or why not?
How about we change the permissions of the file 'tmp.sh' to make it executable and check again?
chmod +x tmp.sh
Execute the file:
./tmp.sh
Check the contents of 'tmp.sh'.
cat tmp.sh
Delete the tmp.sh
file.
rm tmp.sh
List the contents of the current directory. It shouldn't show the tmp.sh
file anymore as we deleted it.
ls
README.md course-catalog.txt docker-compose.yaml
Commonly used shell commands
ls - lists contents of a directory
cat - displays contents of a file. It can also be used to combine the contents of 2 files into 1.
echo - used to print the string or the value of the variable that follows it
cd - change directory to a different one
mkdir - create a directory
rmdir - delete a directory
touch - create a file
grep - search for a file or specific text within a file
man - manual for the commands. You should use this to learn more about the different commands.
tee - used to redirect the output of a command to a file along with the standard output on the terminal.
Constructs in Scripting
- Shebang:
Shebang is the first line of the script that defines the interpreter for the script that follows. For a shell script, we include the type of shell we would like to use for the provided script. Even though it is optional, adding a shebang improves the readability of a script. For example -
#!/bin/bash
- Variables:
Declaring and assigning values to variables -
name = "Jack"
Accessing the variable requires you to use a $ before the variable name -
echo $name
- Comments
To write comments you need to use # -
# This is a comment
Programming Constructs -
i. Conditionals -
if [ "$name" = "Jack" ]; then
echo "Hi, Jack!"
else
echo "Who are you?"
fiii. Loops -
# for loop
for i in 1 2 3 4 5; do
echo $i
done
# while loop
n=1
while [ $n -le 5 ]; do
echo $n
n=$((n + 1))
doneiii. Reading input
read -p "Enter your name: " user
echo "Hello, $user!"iv. Redirecting output
ls > output.txtiv. Functions
# Functions without arguments
print(){
echo "Hello World"
}
print
# Functions with arguments
hello(){
echo "Good Morning $1!"
}
input() {
read -p "Enter your name: " name
echo $name
}
hello $(input)
Activity
Try writing a basic shell script to create a "Guess the number" game.
Initally, you need the script to pick a random number for the user to guess.
Then, the script should allow the user to enter a number.
You can provide feedback about a "hit" or a "miss".
You could also let the user know if their guess is "hot" or "cold" depending on how closer or farther they are from the number.
Finally, you can limit the number of guesses a user has in each iteration of the game.
Here is a sample solution for you to look at once you are done writing your solution.