Basic Linux Shell-Scripting

Table of contents

๐ป What is Kernel?
The Kernel manages the hardware like CPU, Motherboard, Ram, Storage(Hard-disk, SSD), Network Adapter etc.,
๐ What is Shell?
A shell interprets commands from the user and executes them by making system calls to the operating system kernel. shells include bash (the Bourne Again Shell), zsh, tcsh, and PowerShell.
The shell acts as an intermediate between the user and the operating system kernel, and the functionality of the kernel in an accessible way
๐ง What is Linux Shell Scripting? ๐
Shell scripting is a way to automate tasks in Linux. Shell scripts allow you to create small script programs that can automate & repetitively carry out complex functions and can be scheduled as cron-job.
It is a core of a shell script a text file containing a set of series of commands that the shell interpreter can execute. When you run a shell script, the commands in the script are executed line by line.
Shell scripts use their own shell's syntax and features, so you don't have to learn a new programming language. You just level age the commands and syntax you already know from the command line.
๐ป๐โ๏ธ What is Shell Scripting for DevOps?
Shell scripting plays an Imp role in DevOps. It allows DevOps engineers to automate repetitive tasks, manage configurations and simplify complex processes. Shell scripts can automate tasks like installing software, setting up environments, managing files, monitoring systems, etc.
Shell scripts are integrated with other DevOps tools and pipelines.
For example:- scripts can be run as part of CI/CD jobs, configuration management tools, container builds, etc.
๐ค What is #!/bin/bash? Can we โ๏ธ #!/bin/sh as well?
Both #!/bin/bash
and #!/bin/sh
is shebang 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 featureestic shell available on Linux.#!/bin/sh
- Use the Bourne shell (/bin/sh) interpreter. This is the most basic shell available on the Linux system.
๐๐ Write a Shell Script which prints
#!/bin/bash
echo "This is some text"
To Execute Script:
. <file-name>.sh
bash <file-name>.sh
./<file-name>.sh (forthis we need change file permission to execute)
๐โจ Write a Shell Script ๐:
1๏ธโฃ Accept user input ๐โโ๏ธ๐ฌ
read -p "Enter your name: " name
2๏ธโฃ Take input from arguments ๐ ฐ๏ธ๐ ฑ๏ธ
#!/bin/bash
echo "Your name is $1"
echo "Your age is $2"
3๏ธโฃ Display the variables ๐ฅ๏ธ๐ค
echo "Your name is $variable"
writing complete code using the above three syntaxes:
#!/bin/bash
read -p "Enter your details: " name
age= $1
city= $2
echo " your Name is $name "
echo "your age is $age"
echo "your city is $city"
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
read -p "Enter Your Marks:- " marks
if [[ $marks -gt 40 ]]
then
echo "Your are PASS:)"
else
echo "Your are FAIL!!!!!"
fi
Happy Learning!
Thanks for Reading :)
Afroz Shaik โจ
Saurabh Gore Ranjit kumar Nayak Sidharth Shukla Sudhanshu Motewar Prasad Suman Mohan Anjali Chahande Amulya Naidu
Subscribe to my newsletter
Read articles from Afroz Shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
