Basic Linux Shell Scripting

Basic Linux Shell Scripting

What is Shell?

Shell is a command line interface for users. It provides an environment for a user to execute the commands and interact with the kernel.

There are several shells are available for Linux systems like –

  • BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.

  • CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.

  • KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications etc.

What is Shell Scripting ?

Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

Why do we need shell scripts

There are many reasons to write shell scripts:

  • To avoid repetitive work and automation

  • System admins use shell scripting for routine backups

  • System monitoring

  • Adding new functionality to the shell etc.

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

The sharp sign (#) and the bang sign (!) thats why called is the shebang. (sharpbang). We can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

Yes, you can write #!/bin/sh instead, which specifies that the script should be run using the default shell on most Unix-like systems, which is usually a Bourne shell or a compatible shell like Bash or Dash. However, there are some differences in the syntax and features supported by different shells, so you should use the appropriate shebang line for the shell that you are using and for the syntax and features that your script requires.

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

ubuntu@ip-172-31-13-215:/scripts$ cat devops.sh  
#!/bin/bash
echo "I will complete 90DaysOfDevOps Challenge"
ubuntu@ip-172-31-13-215:/scripts$ sudo chmod +x devops.sh  
ubuntu@ip-172-31-13-215:/scripts$ ./devops.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-13-215:/scripts$ cat variable.sh  
#!/bin/bash
read -p "Enter your name: " name
echo "My name is ${name}"
ubuntu@ip-172-31-13-215:/scripts$ sudo chmod +x variable.sh  
ubuntu@ip-172-31-13-215:/scripts$ ./variable.sh Priyanka
My name is Priyanka

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

ubuntu@ip-172-31-13-215:/scripts$ cat compare_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 [ $b -gt $a ]
  then
    echo "$b is greater than $a"
else
  echo "$a is equal to $b"
fi
ubuntu@ip-172-31-13-215:/scripts$ sudo chmod +x compare_numbers.sh
ubuntu@ip-172-31-13-215:/scripts$ ./compare_numbers.sh  
Enter first number: 6
Enter second number: 3
6 is greater than 3
ubuntu@ip-172-31-13-215:/scripts$ ./compare_numbers.sh  
Enter first number: 7
Enter second number: 7
7 is equal to 7
ubuntu@ip-172-31-13-215:/scripts$ ./compare_numbers.sh 
Enter first number: 6
Enter second number: 9
9 is greater than 6

Thank you for reading this Blog. If you found this blog helpful, please like, share, and follow me for future blogs. Any feedback or remarks will be highly appreciated.

I would like to connect with you at https://www.linkedin.com/in/priyanka-yadav-1966a11b3/