Shell Scripting

Shell Scripting

Day 4

Table of contents

No heading

No headings in the article.

Shell scripting refers to writing a series of commands for the Unix shell or command-line interpreter to execute. It allows users to automate tasks, run multiple commands simultaneously, and create complex workflows without having to manually execute each command separately. Shell scripting is an essential skill for anyone working in a Unix-based operating system, such as Linux or macOS.

Shell scripting is a powerful tool for system administrators, developers, and other IT professionals who need to automate repetitive tasks or perform complex operations regularly. With shell scripting, you can create scripts that can:

  • Configure system settings

  • Install software packages

  • Manage user accounts

  • Monitor system resources

  • Backup and restore data

  • Automate system maintenance tasks

  • And much more

Shell scripts can be written in various scripting languages, including Bash, KornShell, and Zsh. However, Bash is the most popular shell scripting language, and it is the default shell in most Unix-based operating systems.

To get started with shell scripting, you need to open a text editor and write the commands you want to execute in sequence. These commands can be system commands or custom scripts you have created. For example, you can create a script that installs a specific package and configures it for your system:

The first line must be #!/bin/bash which is called Shebang as well.

#!/bin/bash

# Install the package
apt-get install my-package

# Configure the package
my-package configure --option=value

Once you have written your script, you need to make it executable using the chmod command:

chmod +x my-script.sh

You can then execute your script by typing its name at the command prompt:

./my-script.sh

One of the most powerful features of shell scripting is the ability to use variables and control structures to create more complex scripts. Variables allow you to store and manipulate data within your script, while control structures allow you to create loops and conditional statements to execute different commands based on certain conditions.

  • Write a Shell Script that prints I will complete the #90DaysOofDevOps challenge

      $cat demoscript.sh
      #!/bin/bash
    
      echo "I will complete the #90DaysOofDevOps challenge"
    
      $ sh demoscript.sh
      I will complete the #90DaysOofDevOps challenge
    
  • Write a Shell Script to take user input, input from arguments and print the variables.

      #!/bin/bash
    
      echo Hello, who am I talking to?
      read varname
      echo It\'s nice to meet you $varname
    
      ===================================================
      o/p
      >Hello, who am I talking to?
      Human
      >It's nice to meet you Human
    
  • Write an Example of If else in Shell Scripting by comparing 2 numbers

      #!/bin/bash
      a=2
      b=7
      if [ $a -ge $b ]
      then
        echo "The variable 'a' is greater than the variable 'b'."
      else
        echo "The variable 'b' is greater than the variable 'a'."
      fi
    
      ================================================
      o/p
      The variable 'b' is greater than the variable 'a'.
    

For example, you can create a script that reads a list of files from a directory and performs a specific operation on each file:

#!/bin/bash


# Define the directory to search
dir="/home/ubuntu"

for FILE in *;
do
# Loop through the files in the directory
echo $FILE
done

This script uses a for loop to iterate through each file in the specified directory.

In conclusion, shell scripting is an essential skill for anyone working in a Unix-based operating system. It allows you to automate tasks, run multiple commands simultaneously, and create complex workflows without having to manually execute each command separately. With shell scripting, you can create scripts that can configure system settings, install software packages, manage user accounts, monitor system resources, back up and restore data, automate system maintenance tasks, and much more. By mastering shell scripting, you can become a more efficient and productive IT professional.

Thank you for reading the blog.