🚀Day 05- Linux Basic Shell Scripting

🚀Day 05-  Linux Basic Shell Scripting

What is Shell?

Shell is a command language interpreter that executes commands read from input devices such as keyboards or from files and gives them to the operating system to perform. Shell allows users to communicate efficiently and directly with their operating systems. The shell gets started when the user logs in or starts the terminal.

Type of Shell

Bourne Shell (sh): Developed by Stephen Bourne, from Bell Labs (AT&T from where Unix came from), this was, through several years, the most used Shell over the Unix operating system. Also known as “Standard Shell”, because, for a couple of years, it was the only available Shell. Even nowadays it’s the most popular and present on all Unix and Linux distributions.

Bourne Again Shell (bash): This is the most powerful Shell whose user’s number grows all around, maybe because it’s the Linux default Shell. This shell is called a superset of the Bourne shell, a set of add-ons and plug-ins. This means that the Bourne Again shell is compatible with the Bourne shell: commands that work in sh, also work in bash. However, the reverse is not always the case.

Korn Shell (ksh): Developed by David Korn, also from Bell Labs, it’s a superset derived from sh, which means it has all the features that sh does and also a bunch of improvements. As it is 100% compatible with sh, it is taking over a lot of shell programmers and users. Not to mention, it’s the one we will base our course on.

Zsh: It is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh

C Shell (csh): Developed by Bill Joy from Berkley University is the most common Shell on *BSD/Xenin environments. The command structure is very similar to the C programming language. Its major fault was that it was not compatible with the sh Shell.

tcsh or TENEX C shell: a superset of the common C shell, enhancing user-friendliness and speed. That is why some also call it the Turbo C shell.

The file/etc/shells gives an overview of known shells on a Linux system:

What is Shell Scripting?

Script: a program written for a software environment to automate the execution of tasks.

â–Ș Shell Script is a series of commands written in a text file that has an extension .sh

â–Ș When the script is executed, those commands will be executed one line at a time automatically

â–Ș Shell script is interpreted, not compiled.

When to use Shell Scripting?

Automation of Repetitive Tasks: Shell scripts allow you to automate repetitive tasks, such as file backups, log file monitoring, and data processing.

System Administration: System administrators use shell scripts to manage and configure various aspects of the Linux system, including user accounts, permissions, and system settings.

Batch Processing: Shell scripts are useful for batch processing tasks where you need to execute a series of commands or operations in sequence.

File and Directory Operations: Shell scripts simplify file and directory operations, such as copying, moving, renaming, and deleting files.

Text Processing and Parsing: Shell scripting is handy for text processing tasks, including searching, replacing, and parsing text files.

Customization and Configuration: You can use shell scripts to customize and configure your Linux environment according to your preferences. This is particularly useful for setting up development environments or configuring server settings.

Backup and Restore: Shell scripts are commonly used for automating backup and restore processes. This includes creating regular backups of important data and restoring it when needed.

Log File Analysis: Shell scripts can be employed to analyze log files, extract relevant information, and generate reports. This is valuable for monitoring system activities and diagnosing issues.

Software Installation and Updates: Shell scripts can automate the installation and update processes of software packages. This is useful for maintaining consistent software configurations across multiple systems.

Networking Tasks: Shell scripts can automate networking tasks, such as setting up network connections, configuring firewalls, and managing network services.

Resource Monitoring: Shell scripts can be used to monitor system resources, such as CPU usage, memory usage, and disk space, and take appropriate actions based on the observed conditions.

Task Scheduling: The Linux cron system allows you to schedule the execution of tasks at specific times or intervals using shell scripts.

User Interaction: Shell scripts can include user prompts and inputs, enabling interaction with the script during execution.

Custom Application Deployment: Shell scripts are often part of application deployment processes, helping to automate the deployment and configuration of custom software.

When NOT to use Shell Scripting?

  1. Resource-intensive tasks, especially where speed is a factor (sorting, hashing, recursion, etc ..)

  2. Procedures involving heavy-duty math operations, especially floating point arithmetic, arbitrary precision calculations, and complex numbers (use C++ or FORTRAN instead)

  3. Complex applications, where structured programming is a necessity (type-checking of variables, function prototypes, etc.)

  4. Extensive file operations required (Bash is limited to serial file access, and that only in a particularly clumsy and inefficient line-by-line fashion.)

  5. Need native support for multi-dimensional arrays, and data structures, such as linked lists or trees

  6. Need to use libraries or interface with legacy code

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

#!/bin/bash
# A script example
echo "I will complete #90DaysOofDevOps challenge!" # print something

Explanation:

  1. #!: "Shebang” line to instruct which interpreter to use. In the current example, bash. For sh, it would be: #!/bin/sh

  2. All comments begin with "#".

  3. Print "I will complete #90DaysOofDevOps challenge!" to the screen.

What is #!/bin/bash? can we write #!/bin/sh as well?

Both #!/bin/bash and #!/bin/sh is shebang or hashbang lines that tell the operating system which interpreter to use to execute the shell script.

  • #!/bin/bash - Use the Bash shell interpreter to run the script. Bash is the most common and widely used shell available on Linux.

  • #!/bin/sh - Use the Bourne shell (/bin/sh) interpreter. Bourne shell, which is an older and simpler shell. It's not as powerful as Bash, but it's still found on many systems and can handle basic tasks.

What is the Command Line Argument in Shell Script?

Command-line arguments are parameters that are passed to a script while executing them in the bash shell. They are also known as positional parameters in Linux.

Write a Shell Script to take user input from arguments and print the variables

#!/bin/bash
# Take user input
echo "Enter your name: "
read username

# Take a user input as an argumnets while executing script.
arg1=$1
arg2=$2

#print the name
echo "Your Name is $username"
echo "First Argument: $arg1"
echo "Second Argument: $arg2"

In some cases, the script needs to interact with the user and accept inputs. In the above shell scripts, we have used the read statement to take input from the user. read command is used to get the input from the user (Making scripts interactive). Save the script and give it a ".sh" extension. This tells your computer it's a shell script. We have also passed 2 arguments while running the script.

for e.g /bin/bash <scriptname.sh> 10 20

Write a Shell Scripting comparing 2 numbers using an if-else statement.

#!/bin/bash
echo "Enter the 1st number"
read number1

echo "Enter the 2nd number"
read number2

if [ $number1 -gt $number2 ]
then
  echo "The number '$number1' is greater than the number '$number2'."
else
    echo "The number '$number2' is greater than the number '$number1'"
fi

The above script will take 2 numbers as user input and then compare those two numbers with each other then print the greater number.

Thank you for reading. I hope you will find this article helpful. if you like it please share it with others

Mohd Ishtikhar Khan : )

Â