Shell Scripting in Ubuntu: The Comprehensive Cheat Sheet

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Shell scripting in Ubuntu allows automation of repetitive tasks, improves productivity, and enhances the functionality of the system. This comprehensive guide is designed to introduce you to shell scripting from fundamental to advanced concepts, providing you with an extensive cheat sheet that you can refer to.

Basics of Shell Scripting

To start with shell scripting, you need to create a shell script file. This is a simple text file with a .sh extension which contains shell commands. The first line of your script should be the shebang (#!/bin/bash) which tells the system which interpreter to use to execute the file’s contents.

#!/bin/bash
echo "Hello, World!"

Save this as helloworld.sh and make it executable:

chmod +x helloworld.sh

And run with:

./helloworld.sh

You will see the output:

Hello, World!

Variables in Shell Scripting

In shell scripting, variables are used to store data that can be used throughout the script. They are case-sensitive and do not require a type declaration.

#!/bin/bash
name='Ubuntu User'
echo "Welcome, $name!"

Output:

Welcome, Ubuntu User!

Conditional Statements

Conditional statements allow you to perform different actions based on conditions. The if statement is one of the most basic conditionals in shell scripting.

#!/bin/bash
read -p "Enter your age: " age
if [ $age -ge 18 ]; then
echo "You are an adult."
else
echo "You are a minor."
fi

If you enter 22, the output will be:

You are an adult.

Loops

Loops allow you to execute a block of code multiple times. The for loop is typically used when you know in advance how many times you want to execute a statement or a block of statements.

#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

This prints:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

Functions

A function is a block of code that can be reused within a script. It can be defined and then called at any point in the script.

#!/bin/bash
function greet() {
echo "Hello, $1!"
}
greet "Ubuntu Friend"

This calls the greet function with “Ubuntu Friend” as an argument, producing the output:

Hello, Ubuntu Friend!

Advanced Scripting

As your scripts become more complex, you will need to handle script arguments, signals, and perform more complex maneuvers.

Script Arguments

#!/bin/bash
echo "Script name: $0"
echo "First arg: $1"
echo "Second arg: $2"

Run ./script.sh Ubunu Linux to see:

Script name: ./script.sh
First arg: Ubuntu
Second arg: Linux

Signal Trapping and Exit Status

#!/bin/bash
trap 'echo "Ignoring Signal"' SIGINT

sleep 10 # Pretend to be busy

exit 0

Running this script and then trying to interrupt it with Ctrl+C will display “Ignoring Signal”.

Using ‘case’ Statements

#!/bin/bash

fruit="apple"

case $fruit in
"apple")
 echo "Apple pie is tasty."
 ;;
"banana")
 echo "I like banana nut bread."
 ;;
*)
 echo "That's not on the menu."
 ;;
esac

The output for this code will be:

Apple pie is tasty.

While Loop with File Descriptor

#!/bin/bash

file="/var/log/syslog"
while read line; do
echo $line
done <$file

Using awk and sed for Text Processing

#!/bin/bash
echo "Text processing with awk and sed is powerful" | awk '{ print $5 }' | sed 's/awk/AWK/'

Output:

AWK

Conclusion

In this comprehensive cheat sheet, we explored the basics of shell scripting in Ubuntu to more advanced topics. As you practice these concepts, you will develop the ability to write scripts that can automate tasks, process text, and manage system signals efficiently. Remember, practice makes perfect in the world of shell scripting.