Shell Scripting - Basics

Shell Scripting - Basics

What is Shell?

A shell is a user interface for interacting with the kernel. It allows users to execute commands, manage files and directories, and perform other tasks on the system.

The shell acts as a mediator between the user and the kernel. It translates the user's commands into instructions that the kernel can understand. It also provides feedback to the user about the results of the commands.

Some popular shells include:

  • Bash

  • Zsh

  • Fish

  • Tcsh

So, What is Shel Scripting?

Definition: Shell scripting is a programming language that allows users to automate tasks on Unix-like operating systems. Shell scripts are text files that contain a series of commands that are executed by the shell when the script is run.

Importance

Shell scripting is a powerful tool that can be used to save time and improve efficiency. It can be used to automate a wide variety of tasks, such as:

  • Managing files and directories

  • Running programs and applications

  • Sending and receiving email

  • Monitoring system resources

  • Automating system administration tasks

Shell scripting is also a relatively easy language to learn, making it a good choice for beginners.

Use cases

Here are a few examples of how shell scripting can be used:

  • A system administrator can use shell scripts to automate the deployment of software updates or to monitor system performance.

  • A developer can use shell scripts to automate the build and testing process for their software.

  • A user can use shell scripts to automate their personal workflow, such as backing up their files or sending themselves a reminder email every day.

#!/bin/bash (shebang)

The shebang is a special line at the top of a shell script that tells the operating system which shell to use to execute the script. The most common shebang is #!/bin/bash, which tells the operating system to use the Bash shell.

Can we write/modify #!/bin/bash

Yes, we can write or modify the #!/bin/bash line in a shell script. However, it is important to note that changing the shebang will change the shell that is used to execute the script. For example, if we change the shebang to #!/bin/sh, then the script will be executed using the Bourne shell.

Important things to know before Shell scripting, also known as Memory commands

Here are three common memory commands that can be used in shell scripts:

  • free: This command displays the amount of free and used memory on the system.

  • df: This command displays the amount of free and used disk space on the system.

  • top: This command displays a list of the processes that are currently running on the system, sorted by CPU usage.

Awk

Awk is a text processing tool that can be used to manipulate and extract data from text files. Awk is often used in shell scripts to parse and process log files, or to generate reports.

Syntax:

awk ‘{print <expression>}' <file>

Example:

df -H | awk ‘{print $1 “ ” $5}’

This command will display a list of all mounted file systems, along with the amount of free and used space on each file system.

Grep

Grep is a text search tool that can be used to find and extract lines of text that match a specified pattern. Grep is often used in shell scripts to filter log files or to search for specific files.

Syntax:

grep <pattern> <file>

Example:

grep ‘error’ /var/log/syslog

This command will print all lines of the file /var/log/syslog that contain the word "error".

Find

Find is a utility that can be used to search for files and directories on a Linux system. Find is often used in shell scripts to locate specific files or to automate file management tasks.

Syntax:

find <path> -<options>

Example:

find /home/user -name ‘myfile.txt’

This command will find all files named myfile.txt in the directory /home/user.

Cut

Cut is a utility that can be used to extract fields from lines of text. Cut is often used in shell scripts to parse text files or to generate reports.

Syntax:

cut -d <delimiter> -f <fields> <file>

Example:

cut -d ‘,’ -f 1,3 /path/to/file.csv

This command will print the first and third fields of the CSV file /path/to/file.csv.

Basic Shell Script to demonstrate creating multiple directories

#!/bin/bash

for ((i=$2;i<=$3;i++))

do
{
 mkdir "$1"$i
}
done

It is a simple loop to create a series of directories. The loop starts at the value of the variable $2 and iterates up to the value of the variable $3, inclusive. For each iteration, the loop creates a directory with the name $1$i, where $1 is the first argument passed to the script and $i is the current value of the loop counter.

Here is an example of how to use the script:

./create_directories.sh Day 1 50

This will create 50 directories with the names Day1, Day2, ..., Day50.

You can also use the script to create directories in a specific location. For example, to create the directories in the /tmp directory, you would use the following command:

./create_directories.sh /tmp dir 1 10

This will create the same 10 directories, but they will be located in the /tmp directory.

#!/bin/bash

source_dir=/home/ubuntu/scripts
target_dir=/home/ubuntu/backups

current_timestamp=$(date "+%Y-%m-%d-%M-%S")

backup_dir=$target_dir/$current_timestamp.tgz

echo "$current_timestamp"

tar czf $backup_dir $source_dir

echo "Backup complete!"
#!/bin/bash

This line tells the operating system which shell to use to execute the script. In this case, we are telling the operating system to use the Bash shell.

source_dir=/home/ubuntu/scripts

This line defines the source directory that will be backed up. In this case, we are defining the source directory to be /home/ubuntu/scripts.

target_dir=/home/ubuntu/backups

This line defines the target directory where the backup will be stored. In this case, we are defining the target directory to be /home/ubuntu/backups.

current_timestamp=$(date "+%Y-%m-%d-%M-%S")

This line gets the current timestamp and stores it in the variable current_timestamp. The date command is used to get the current date and time, and the + operator is used to specify the format of the timestamp.

backup_dir=$target_dir/$current_timestamp.tgz

This line creates a backup directory name using the current timestamp. The .tgz extension indicates that the backup will be a compressed archive file.

echo "$current_timestamp"

This line prints the current timestamp to the console.

tar czf $backup_dir $source_dir

This line uses the tar command to create a compressed archive of the source directory in the backup directory. The c option tells tar to create an archive, the z option tells tar to compress the archive, and the f option specifies the name of the archive file.

echo "Backup complete!"

This line prints a message to the console indicating that the backup is complete.

Takeaway

Shell scripting is a powerful tool that can be used to automate a wide variety of tasks. By understanding the basics of shell scripting and the commands discussed in this blog post, you can start to write your shell scripts to improve your productivity and efficiency.