Shell Scripting for Devops Beginners: Part 2

In the last article, we learned about some of the basic Linux commands. In this blog post, we will cover Networking Commands, Process Management commands and loops, and conditional operators in Shell scripting, along with some interview questions.

We forgot to cover a couple of basic commands in the first part, so let’s cover that here and then jump to some cool topics.

Also learning about Absolute path and relative path, and that’s your homework.
Let’s dive right in

  1. Basic Commands
    Lets look at some basic operators
    • > (overwrite): '>‘ sends the output of a command into a file, replacing the file’s existing contents.
      Example :
      echo “hello” > file.txt

      If file.txt exists → it erases everything and replace with “hello”
      If it doesn’t exist → file.txt is created with the content “hello”
    • >> (append) : ‘>>’ sends the output of a command into a file, but adds it at the end, without deleting existing content.
      Example :
      echo “hello” >> file.txt

      If file.txt exists → “hello” is added at the bottom
      If it doesn’t exist → it is created with “hello” in it

We will read about other operators like (‘|’) pipe.

  1. Networking Commands
    lsof -i // To check open ports
    ping google.com // To check if a host is reachable 
    ipconfig // to check IP address
  2. Process Management

The above commands are very important in day-to-day development, especially when debugging slow systems or tracking down resource with heavy usage.

  1. What is Pipe(|)parameter ?
    Pipe parameter is used to send the output of the first commands to the second commands.
    if ps -ef returns all the processes then if we do | grep “google” it will give us the processes matching the given “pattern” Google.
    ps -ef | grep "google" // returns all lines which matches Google.

    Now, say if i want to retrieve only a specific column after finding the entire matching sentences. Like if i want the pid of the “google” senence then what would you do ?
    ps -ef | awk -F ‘{print $2}’. // will print every pid of all processes
    ps -ef | grep "google" | awk -F '{print $2}' // prints the pid of only process which have google
  2. Basic Conditions and Loops
    To understand this, let’s see some interview questions, so that it’’be easier for us to understand hoe these conditional operators and loops work.
  • Print numbers from [1, 30] divisible by 3
    for i in {1..30}
    do
      if [ $((i % 3)) -eq 0 ]
      then
        echo “$i is divisible by 3”
      fi
    done
  • Without $(( ))Bash will treat numbers as plain text, not math., and
    name=”pradyumna”
    echo $name // dollar $ is used for accessing vriable

    [ ] is used for conditional checks.
    x=1
    while [ $x -le 10 ]
    do
      echo “Number: $x”
      x=$((x + 1))
    done
  • Print the number of ‘S’ in MISSISSIPPI
    echo -n ‘MISSISSIPPI’ |    # print the word (no newline)
    grep -o ‘S’ |              # extract every ‘S’ and print one per line(purpose of -o)
    wc -l                      # count how many lines = number of S’s
  • Sort a file alphabetically
    sort file.txt

And that’s a wrap. That brings us to the end of the Shell Scripting for DevOps Beginners series.

These concepts are fundamental for DevOps, SRE, Cloud, and Backend roles.
If you found this article helpful, please consider leaving a like and sharing it with your friends. And if you want more such content delivered straight to your inbox, make sure to subscribe.

Subscribe to my Substack

Get thoughtful insights on technology, AI, and software engineering delivered straight to your inbox. No spam, ever.

Weekly Updates
I will send you an update each week to keep you filled in on what I have been up to.
No Spam
Your email stays private, I’ll never share or sell it.