Skip to main content

Command Palette

Search for a command to run...

Shell Scripting

Updated
2 min read
Shell Scripting
V

Hello! I'm a student passionate about becoming a Cloud and DevOps Engineer. I have a solid foundation in AWS Cloud, Linux, Docker, and Kubernetes.

Introduction

In the previous blog, we talked about the basics of Linux. This blog is about shell scripting.

Shell scripting is very helpful for automating day-to-day tasks.
For example, if you want to print numbers from 1 to 10, you can simply use the
echo command 10 times. Or, if you want to do a health check of your instance every hour, you can do it manually, but DevOps is about automating, so it is crucial to learn shell scripting.

Writing first Shell-Script

  • log into your Linux machine whether it is in cloud or you have installed it locally.

  • Create a file using touch command with extension .sh

    touch first-shell-script.sh

    check the file using ls command, or you can use ls -ltr

  • to see what arguments one can use with a command or to get information about the command use man which stands for manual. For example you want to know about touch command type man touch and it will provide all the information about touch command. press q to quit.

  • Now to write our Script inside the file we just created we need to open it using an editor, we will be using vi editor

    type vi first-shell-script.sh , it opens the file in command mode, then press i

  • Write the script.

    now let's understand this script -

  • the first line !#/bin/bash is Shebang that tells which executable will execute this file as we saw in previous blog that there are different types of shell like sh, zsh, ksh, dash etc.

  • the echo command is used to print in the console.

  • after writing the script press esc and then :wq!
    esc command will take you out of insert mode and :wq! command will save the file and quit.

  • Now we have to change file permission.

    chmod 777 first-shell-script.sh which will give permission for read, write and execute to owner, file's group member and other.

and then execute the script ./first-shell-script.sh

we got our first script working fine.

Some Important commands for Checking instance

  1. free -m : shows the info about RAM.

  2. nproc : list the no. of CPU

  3. df : report file system disk space usage

  4. top : list all the processes and their memory, RAM usage.

Thankyou for reading.

More from this blog

Vishal Kerketta

14 posts