Basic Linux Shell-Scripting

Afroz ShaikAfroz Shaik
3 min read

๐Ÿ’ป 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

13
Subscribe to my newsletter

Read articles from Afroz Shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Afroz Shaik
Afroz Shaik