Day 4 - Basic Linux Shell Scripting

Day 4 - Basic Linux Shell Scripting

Day 4 of 90daysofdevops

What is Shell Scripting for DevOps?

The kernel is the main component of a Linux operating system (OS) and is the core interface between a computer’s hardware and its processes and it has complete control over everything in the system.
The shell is the Linux command line interpreter. It provides an interface between the user and the kernel and executes programs called commands. Shell accepts human-readable commands from users and converts them into something which the kernel can understand.

Shell scripting is a program to write a series of commands used to automate the tasks written in the .sh file. Shell scripting is an important part of process automation in Linux and it is used to automate repetitive tasks and streamline the development and deployment process. The scripting file starts with #!/bin/bash

A shell script involves the following basic elements

  • Shell Keywords – if, else, break, etc.

  • Shell commands – cd, ls, echo, pwd, touch etc.

  • Functions Control flow – if..then..else, case and shell loops, etc.

What is #!/bin/bash? Can we write #!/bin/sh as well?

Any shell script is identified by Shebang or hashbang(#!). Shebang is a combination of bash(#) and bang(!), followed by the shell path.
The shebang(#!) at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter.

/bin/sh and /bin/bash are both Unix/Linux shell interpreters, but they have some differences in their behavior and syntax. /bin/sh is a POSIX-compliant shell that is designed to be small and efficient, while /bin/bash is a more feature-rich shell that includes many additional features and extensions.

For example, some features such as arrays and some string operations are not available in /bin/sh, but they are available in /bin/bash. Additionally, /bin/bash provides more extensive support for interactive shell features such as command history, auto-completion, and job control.

We can write #!/bin/sh in place of #!/bin/bash, the difference is just in shells 'sh' means Bourne Shell and 'bash' means Bourne Again Shell. Bash shell scripting is a kind of shell scripting only. You can say, it's a subset of shell scripting. The Bourne Shell is another commonly used shell in Unix-based operating systems.

Write a Shell Script that prints I will complete #90DaysOofDevOps challenge

ubuntu@ip-172-31-2-150:~/Day4$ nano script.sh
#!/bin/bash
echo "I will complete #90DaysofDevOps challenge"
ubuntu@ip-172-31-2-150:~/Day4$ bash script.sh
I will complete #90DaysofDevOps challenge

Write a Shell Script to take user input, input from arguments and print the variables.

ubuntu@ip-172-31-2-150:~/Day4$ nano input.sh
#!/bin/bash
echo "My name is $1 $2"
ubuntu@ip-172-31-2-150:~/Day4$ chmod 777 input.sh
ubuntu@ip-172-31-2-150:~/Day4$ ./input.sh Shreya Gupta
My name is Shreya Gupta

Write an Example of If else in Shell Scripting by comparing 2 numbers.

ubuntu@ip-172-31-2-150:~/Day4$ nano comparing_numbers.sh
#!/bin/bash
read -p "Enter first number:" a
read -p "Enter second number:" b

if [ $a -gt $b  ]
then
  echo "$a is greater than $b"
elif [ $a -eq $b ]
  then
    echo "$a is equal to $b"
else
  echo "$b is greater than $a"
fi

ubuntu@ip-172-31-2-150:~/Day4$ chmod 777 comparing_numbers.sh
ubuntu@ip-172-31-2-150:~/Day4$ ./comparing_numbers.sh
Enter first number:2
Enter second number:4
4 is greater than 2
ubuntu@ip-172-31-2-150:~/Day4$ ./comparing_numbers.sh
Enter first number:3
Enter second number:3
3 is equal to 3
ubuntu@ip-172-31-2-150:~/Day4$ ./comparing_numbers.sh
Enter first number:7
Enter second number:2
7 is greater than 2

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops