Let's start with Shell scripting

Let's start with Shell scripting

Week 2 #90Days of DevOps challenge #devops #trainwithshubham

Shell scripting is the process of creating scripts that can be interpreted by a command-line shell or a terminal emulator. The shell script is a program written in a shell programming language to automate tasks or perform a series of commands on a Unix or Linux system.

What is Shell Scripting?

Shell scripting is a way of automating tasks that would otherwise require you to manually enter commands into a terminal. It involves writing a script in a language that the shell can understand and then running that script to perform various tasks.

A shell script can be as simple as a few commands strung together, or it can be a complex program with functions, variables, and loops. Shell scripts are typically used to automate tasks such as file manipulation, system administration, and software installation.

Types of Shell

The shell is the interface between the user and the operating system. It allows the user to interact with the operating system by typing commands or running scripts. There are many different types of shells available, but the most common ones are:

  1. Bash

    The Bourne-Again SHell is the default shell on most Linux distributions. It is also available on macOS and other Unix-like operating systems. Bash is a powerful shell that offers a lot of features, including command-line editing, history, job control, and more.

  2. Zsh

    The Z SHell is a powerful shell that offers many features, including command-line editing, history, globbing, and more. It is also highly customizable and supports many plugins and themes.

  3. Fish

    The Friendly Interactive SHell is a modern shell that offers a lot of features out-of-the-box, including auto-completion, syntax highlighting, and more. It is also highly customizable and supports plugins and themes.

Writing Shell Scripts

To write a shell script, you will need to choose a shell and a text editor. Most Linux and Unix systems use the Bash shell, which is short for Bourne-Again SHell, and comes pre-installed on many systems.

Once you have chosen a shell and a text editor, you can create a new file with the .sh extension. This extension tells the system that the file is a shell script.

The first line of your shell script should be a "shebang" line, which tells the system which shell to use to interpret the script. For Bash, the shebang line looks like this:

#!/bin/bash

After the shebang line, you can start writing your shell script. Here is an example of a simple shell script that prints "Hello, world!" to the terminal:

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

This script contains only one command, which is the echo command. The echo command simply outputs its arguments to the terminal.

Here are some basic concepts and tips for shell scripting:

  1. Choosing a shell:

    Shell scripts can be written in different shell languages like bash, zsh, ksh, etc. Bash is the most commonly used shell, and it's available on most Linux and Unix systems.

  2. Shebang:

    Start your shell script with a shebang line that specifies the interpreter that should be used to execute the script. For example, #!/bin/bash will tell the system to use the Bash interpreter to execute the script.

     #!/bin/bash
    
     # This is a comment
     echo "Hello, world!"
    
  3. Variables:

    Use variables to store values that can be used later in the script. Variables can be assigned values using the = operator, and they can be referenced using $variable_name.

     #!/bin/bash
    
     MY_VARIABLE="Hello, world!"
     echo $MY_VARIABLE
    
  4. Command-line arguments:

    You can pass arguments to a shell script by specifying them after the script name when executing the script. These arguments can be accessed in the script using special variables like $1, $2, $3, etc.

  5. Control structures:

    Shell scripts support control structures like if-else, loops, and switch statements. These structures can be used to execute different commands based on certain conditions.

     #!/bin/bash
    
     if [ -e "/etc/passwd" ]; then
       echo "The file /etc/passwd exists."
     else
       echo "The file /etc/passwd does not exist."
     fi
    
  6. Functions:

    Functions in shell scripts are similar to functions in other programming languages. They allow you to group commands together and execute them as a single unit.

    To define a function, you can use the function keyword, followed by the name of the function and the commands to be executed. For example:

     #!/bin/bash
    
     function hello {
       echo "Hello, world!"
     }
    
     hello
    

    In this example, we define a function called hello that simply prints the string "Hello, world!". We then call the function using the function name.

  7. Comments:

    Add comments to your shell script to make it more readable and to explain what the script does. Comments can be added using the # symbol.

  8. Testing:

    Always test your shell script thoroughly before using it in a production environment. You can use shell debugging tools like set -x and set +x to trace the execution of the script and to identify errors.

Running Shell Scripts

To run a shell script, you will need to make it executable. You can do this by running the following command:

chmod +x script.sh

This command gives the script executable permissions, which allows you to run it as a program.

To run the script, simply type its name into the terminal:

./script.sh

This will execute the script and output "Hello, world!" to the terminal.

Useful Shell Scripts

Now that you know the basics of shell scripting, let's look at some examples of useful scripts.

  1. Backup Script

A backup script is a simple but powerful tool that can save you a lot of time and headache in the event of data loss. Here is an example of a backup script that copies a directory to a backup location:

#!/bin/bash

# Set the source and destination directories
source="/home/user/data"
dest="/backup"

# Create the backup directory if it doesn't exist
if [ ! -d "$dest" ]; then
  mkdir -p "$dest"
fi

# Copy the data to the backup location
rsync -av --delete "$source" "$dest"

This script uses the rsync command to copy the data from the source directory to the destination directory. The -a option tells rsync to copy the files and directories recursively, preserving file permissions, and the -v option tells rsync to output its progress to the terminal. The --delete option tells rsync to delete any files in the destination directory that are not in the source directory.

We can use following example also. We define the backup directory and the source directory. We then use the tar command to create a compressed archive of the source directory and save it in the backup directory

#!/bin/bash

BACKUP_DIR="/backup"
SOURCE_DIR="/home/user/documents"

tar -cvzf $BACKUP_DIR/documents.tar.gz $SOURCE_DIR
  1. System Monitoring Script

A system monitoring script shell program can be used to check the status of various system components and resources such as CPU usage, memory usage, disk usage, and network connections. Here's an example of how to write a system monitoring script shell program:

#!/bin/bash

# CPU Usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')

# Memory Usage
MEMORY_USAGE=$(free | awk '/Mem/{printf("%d\n"), $3/$2*100}')

# Disk Usage
DISK_USAGE=$(df -h / | awk '{print $5}' | sed -n 2p | tr -d %)

# Network Connections
NET_CONNECTIONS=$(netstat -an | grep ESTABLISHED | wc -l)

# Print Results
echo "CPU Usage: $CPU_USAGE%"
echo "Memory Usage: $MEMORY_USAGE%"
echo "Disk Usage: $DISK_USAGE%"
echo "Network Connections: $NET_CONNECTIONS"

In this example, we use various Unix commands to gather system information:

  • top command to get CPU usage

  • free command to get memory usage

  • df command to get disk usage

  • netstat command to get network connections

We then print the results to the console using the echo command.

You can run this script on a schedule using a tool like cron to automate system monitoring and alert you if any of the monitored resources reach critical levels.

Thank you for reading. #devops #trainwithshubham