Mastering Shell Scripting: A Beginner's Guide to Linux Automation
What are Shell Commands?
Shell commands are instructions that allow users to interact with the operating system's shell in Linux. These commands, such as echo
, mkdir
, and ls
, enable users to perform various tasks like displaying messages, creating directories, and listing files.
In shell scripting, a series of these commands are written in a .sh
file to automate tasks, such as device management and storage operations.
Linus Torvalds created Linux, and Steve Bourne developed the original shell, known as "sh" (located at /bin/sh
). Over time, improvements led to the creation of "BASH" (Bourne Again Shell), and other shells like csh
, ksh
, and fish
followed.
What is Kernel?
The kernel, written in C, is the core of the operating system. The shell acts as an intermediary, allowing users to communicate with the kernel through shell commands.
How to create Shell Script?
To create a shell script, you need a text editor like Vim or Nano, with Vim being slightly more advanced. A simple script might look like this:
A Simple scripting file:
$vim hello.sh
#!/bin/bash
#Script for new file hello.txt
echo “Amit, Hey!”
echo “Learners: Hub”
To execute this hello.sh scripting file we need to make shell script executable:
$chmod 754 hello.sh
or $ls -l
-rwxr-xr—
“it will look like this”
user- 7: stands for read, write, execute
group- 5: stands for read, execute
*others- 4: stands for read
To execute the script, you must make it executable with chmod 754 hello.sh
, and then run it using ./hello.sh
or bash hello.sh
.
Q. Take input from user ?
#!/bin/bash
echo “Enter the name”
read username
echo “You entered $username”.
or
read -p “enter username “ username
echo “You entered $username”
Q. What are arguments in shell scripting?
In shell scripting, arguments are the inputs provided to a script or command at the time of execution.
These inputs allow the script to receive data or parameters that can be used within the script to perform specific tasks. Arguments are accessed using positional parameters like $1
(first argument), $2
(second argument), $3
(third argument), etc.
Note: The script's name is stored in $0
Q. In the below example we will add user with help argument:
#!/bin/bash
# Script to add a user with their name
sudo useradd -m $1
echo "Hello, $1! new user added. Welcome to the shell scripting tutorial."
./greet.sh Amit
This will output:
Hello, Amit new user added. Welcome to the shell scripting tutorial.
In this example, $1
is the first argument passed to the script, which is "Amit" in this case.
Q What are conditionals in Linux shell scripting?
Conditionals in Linux shell scripting are constructs that allow the script to make decisions based on certain conditions.
The most common conditional statements in shell scripting are if
, else
, elif
, and case
.
if statement: Executes a block of code if a specified condition is true.
if [ condition ]; then # commands to execute if condition is true fi
if-else statement: Provides an alternative block of code to execute if the condition is false.
if [ condition ]; then # commands if condition is true else # commands if condition is false fi
if-elif-else statement: Allows multiple conditions to be checked in sequence.
if [ condition1 ]; then # commands if condition1 is true elif [ condition2 ]; then # commands if condition2 is true else # commands if none of the conditions are true fi
case statement: Similar to switch-case in other programming languages, it matches a variable against a set of patterns.
case $variable in pattern1) # commands for pattern1 ;; pattern2) # commands for pattern2 ;; *) # default commands ;; esac
These conditionals help in controlling the flow of execution in a script.
In short if, elif,else,fi.
Loops in shell scripting
Loops in shell scripting are used to execute a block of code repeatedly, based on a specified condition or for a certain number of iterations. We can automate repetitive tasks and makes data processing efficient.
The most common types of loops in shell scripting are for
, while
, and until
.
#!/bin/bash
for ((num=1,num <=5,num++))
do
mkdir "demo$num"
done
$rm -r day*
- to delete all the files created with day
Q. What if take arguments while starting file in for loop?
#./for_loop.sh day 01 90
#!/bin/bash
for((num = $2; num <= $3 ; num++))
do
mkdir “$1$num”
done
Q. What if take arguments while starting file in for loop?
#!/bin/bash
num = 0
while [[$num -le 5]]
do
echo "lol"
num = $num+1
done
Q. Example of while loop ?
#!/bin/bash
num = 0
while [[$num -le 10 ]]
do
echo $num
num = $((num+2))
Functions in Shell Scripting
Functions in shell scripting are reusable blocks of code that perform a specific task.
./function.sh amit rohit
#!/bin/bash function is_loyal(){ read -p "Amit" name read -p "Rohit" twoname if [[$name == "amit"]] then echo "loyal" elif [[$twoname -ge 100]]; then echo "loyal" else echo "Not loyal" fi } is_loyal