Table of contents
It is a #day4 of #90DaysOfDevOps🐧
let's start 🚀
Before jumping into writing shell script we need to understand what is Linux shell scripting and what is shell scripting for DevOps 🙄
Linux shell scripting !!😳
A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
now try to understand what is shell scripting for DevOps !!😃
Shell Scripting is a way to automate tasks in a Linux operating system. It is a sequence of code or text that contains commands as input from users and execute them. Shell Scripting is a program to write a series of commands for the shell to execute. A shell script is usually created for command sequences that a user needs to use repeatedly to save time.
What is #!/bin/bash?
#!/bin/bash is the first line of the script and is called Shebang. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.
Can we write #!/bin/sh
as well?
Yes, we can use #!/bin/sh to specify the path to the Bourne Shell (sh) interpreter. The Bourne Shell is another commonly used shell in Unix-based operating systems.
Now the wait is over as well as we are ready to write our first shell script😃🤩
Write a Shell Script which prints I will complete the #90DaysOofDevOps challenge
ubuntu@ip-172-34-13-300:~/final-script$ vim
task.sh
ubuntu@ip-172-34-13-300:~/final-script$ chmod 770
task.sh
ubuntu@ip-172-34-13-300:~/final-script$ ./
task.sh
output :
I will complete the #90daysofdevops challenge
Write a Shell Script to take user input, input from arguments and print the variables.
ubuntu@ip-172-34-13-300:~/final-script$ ./c_l-arg.sh Thank you for reading this article
output :
$# :9
$$ :1020
$0:./c_l-arg.sh
$*: Thank you for reading this article
$@: Thank you for reading this article
$3:for
Write an Example of If else in Shell Scripting by comparing 2 numbers
ubuntu@ip-172-34-13-300:~/final-script$ vim
if-else.sh
inside vim editor
#!/bin/bash
echo "enter the first number:"
read a
echo "enter the second number:"
read b
if [ $a -gt $b ]
then
echo "$a is greater than $b"
else
echo "$b is greater than $a"
fi
:wq
ubuntu@ip-172-34-13-300:~/final-script$ chmod 770
if-else.sh
ubuntu@ip-172-34-13-300:~/final-script$ ./
if-else.sh
output :
enter first number: 10
enter second number: 20
20 is greater than 10
Thank you very much for giving your valuable time for reading this article !!☺😊
Arijit Manna