Shell Script For Beginners (2023)

Shell Script For Beginners (2023)

What are Shell Scripts?

A shell script is a program written in the shell programming language, typically to automate tasks that are performed frequently or repetitively. They are executed by a shell, which is a command-line interpreter, on the operating system.

Real Life examples of tasks that can be automated with shell scripts include:

  • Automating backups of files and directories

  • Running multiple commands with a single script

  • Automating system maintenance tasks, such as disk usage monitoring

Automating the deployment of software applications.

Write your First Shell Script.

Here's a simple example of a shell script that prints "Hello, World!" to the console:

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

To run the script, you would make the file executable by running the following command:

chmod +x script.sh

And then you can execute the script with:

./script.sh

This will output "Hello, World!" to the console.
Congratulations, You have written your first Shell Script. Now let us learn about VARIABLES in Shell Script

Variables in Shell Script

Variables in shell scripts are used to store data or values that can be used later in the script. Variables in shell scripts are declared and assigned values with the = operator, like this:

#!bin/bash
name="Azahar"

To access the value stored in a variable, you use the dollar sign ($) followed by the name of the variable, like this:

#!bin/bash
name="Azahar"

echo "Hello, $name"

This will output "Hello, Azahar" to the console.

It's important to note that shell variables are case-sensitive. For example, NAME and name are considered two different variables.

There are also special shell variables that are automatically set by the shell, such as:

  • $0: The name of the script is executed

  • $1, $2, etc.: The positional parameters passed to the script, where $1 is the first argument, $2 is the second argument, and so on.

    example : ./file-name arg1 arg2

  • $#: The number of positional parameters passed to the script

  • $@: All the positional parameters passed to the script

#!/bin/bash

echo "The script name is: $0"
echo "The first argument is: $1"
echo "The second argument is: $2"
echo "The total number of arguments is: $#"
echo "The arguments are: $@"

When you run the script with ./script.sh Azahar Mahaboob, it will output the following:

The script name is: ./script.sh
The first argument is: Azahar
The second argument is: Mahaboob
The total number of arguments is: 2
The arguments are: Azahar Mahaboob

Taking Inputs from users

In shell scripts, you can take input from the user using the read command. The read command reads a line of input from the standard input (usually the keyboard) and stores it in a variable.

Here's an example of a shell script that prompts the user for their name and then prints a greeting:

#!/bin/bash

echo "What's your name?"
read name
echo "Hello, $name"

When you run the script, it will output the following:

What's your name?
Azahar Mahaboob
Hello, Azahar Mahaboob

The read command can also be used to read multiple values at once by specifying multiple variables, like this:

#!/bin/bash

echo "Enter your name and age:"
read name age
echo "Hi, $name! You are $age years old."

When you run the script, it will prompt you to enter your name and age:

Enter your name and age:
Azahar 23
Hi, Azahar! You are 23 years old.

Arithmetic Operations

Arithmetic operations in shell scripts allow you to perform mathematical calculations and store the results in variables. In shell scripts, arithmetic operations are performed using the expr command or within double parentheses ((( ))).

Here are some common arithmetic operations that you can perform in shell scripts:

  • Addition: expr $a + $b or ((a + b))

  • Subtraction: expr $a - $b or ((a - b))

  • Multiplication: expr $a \* $b or ((a * b))

  • Division: expr $a / $b or ((a / b))

  • Modulo (remainder): expr $a % $b or ((a % b))

It's important to note that the expr command evaluates expressions using integer arithmetic and only returns the integer part of the result. To perform floating-point arithmetic, you need to use the bc command.

Here's an example of a shell script that performs arithmetic operations:

#!/bin/bash

a=10
b=20

addition=`expr $a + $b`
subtraction=`expr $a - $b`
multiplication=`expr $a \* $b`
division=`expr $a / $b`
modulo=`expr $a % $b`

echo "Addition: $addition"
echo "Subtraction: $subtraction"
echo "Multiplication: $multiplication"
echo "Division: $division"
echo "Modulo: $modulo"

When you run the script, it will output the following:

Addition: 30
Subtraction: -10
Multiplication: 200
Division: 0
Modulo: 10

Note: The division operation returns the integer part of the result, which is 0. To get the exact result, you would need to use floating-point arithmetic, as mentioned earlier.

Thank you for reading this article about shell scripts! I hope you found it informative and helpful. If you're just starting out with shell scripting, we have something for you. So, be sure to check out my other blogs.