Skip to main content

Basic Of Linux Shell Scripting

Day 4 of Linux Basics and Shell Scripting.

Updated 2 min readMarkdown
Basic Of Linux Shell Scripting

What is Shell Scripting?

Shell is a programming language designed for users to communicate with Kernel. The shell script is a list of human-readable commands converted into something the kernel can understand.

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

#!/bin/bash This is known as shebang in Unix. Shebang is a collection of characters "#!" and /bin which has a list of all commands necessary to write a shell script.

#!/bin/bash is used to instruct the operating system to use bash as the command line interpreter.

#!/bin/sh t is used to execute the file using sh, a Bourne shell, or a compatible shell.

Basic Shell Script Challenges you can try:

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

Solution:

  1. Open a vim editor to write shell script as shown below

    Start writing your script. echo command will print your desired output on screen.

    Execute your shell script as shown below.

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

Solution:

  1. Open a vim editor to write shell script as shown below

    The read command will allow you to accept user input.

    $<variable> will store value of your variable.

    Execute your shell script as shown below.

    The read command will prompt you for user input.

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

Solution:

  1. Open a vim editor to write shell script as shown below

    Use read command to accept two numbers to compare from user as shown below.

    If else statement to compare user entered inputs.

    Syntax:

     if [$num1 -gt $num2]
          then
          print "$num1 is greater"
          else
          print "$num2 is not greater"
     fi
    

    Execute your script as shown below.

    Enter two numbers of your choice and script will compare both.

No comments yet. Be the first to comment.