Skip to main content

Command Palette

Search for a command to run...

Basic Shell scripts

Published
5 min read
Basic Shell scripts

First Script

A shell script that interacts in a conversational way, using commands like date, df -h, and free -h. It includes sleep for pauses, uses pipelines (|), and shows how to use awk to process the output.

We will begin writing the script using the vim editor. (Note: You can use any editor like vi, nano, or vim.)

vim sysinfo_chat.sh

#!/bin/bash
####################################
# Author: Anju
# Date: 10/4/2025
# version: V1
# this script will show how we can use echo, date, df -h & slepp shell commands
###################################

echo "Shree: hey Sanju, could you please confirm today's date?"
sleep 2 #means pause for 2 seconds

echo "Sanju: if I am not mistaken today is $(date | awk '{print $3,$2,$6}') , but why do you look so worried"
sleep 2

echo "Shree: Thanks, yrr. It's just that on this date every month, I have to check the memory and disk space usage"
sleep 2

echo "Sanju: And how do you usually perform this task?"
sleep 2

echo "Shree: It's quite simple; we just need to execute these below mentioned commands and here you go."
sleep 2
echo "memory usage"
free -h | awk '{print $1,$2,$3,$4}'| column -t
echo ""
echo "Disk space usages"
df -h | awk '{print $1,$3,$4}' | column -t
echo "Sanju: Wow, that’s really easy!"
  • Ensure the Script is Executable: Make sure the script is executable. If not, use this command to make it executable:

      chmod 700 sysinfo_chat.sh
    

    and now run this script

      ./sysinfo_chat.sh
    

Here’s a brief explanation of the main shell commands used in this script

  • echo: Prints text or variables to the terminal. It is used to display messages in the script.

  • date: Displays the current date and time. It's used to show today's date in your script.

  • sleep: Pauses the script for a specified amount of time (in seconds). It’s used to add pauses between outputs for better flow.

  • free -h: Displays memory usage in a human-readable format (e.g., MB, GB). It’s used to show memory statistics.

  • df -h: Shows disk space usage in a human-readable format. It’s used to display available and used disk space.

  • awk: A powerful text-processing tool. In your script, it's used to format and display specific columns from the output of free -h and df -h.

Second Script

Write a shell script that uses variables and takes user input (using the prompt -p option).

  • Variable: A variable in shell scripting is a named storage location that holds a value, which can be a string, number, or other types of data.

  • User Input: User input refers to data provided by the user during script execution, typically taken using the read command with the -p option to prompt for input.

#!/bin/bash
####################################
## Author: Anju
## Date: 11/4/2025
## version: V1
## we will write a shell script that uses variables and takes user input (using the prompt -p option)
####################################
height=5.5 #variable

echo "Shree: hey Anju, what's your height?" #print this sentence
sleep 2 # 2 second break

echo "Anju: Hahaha! My height is $height."
sleep 2

echo "Anju: By the way, what was your rank in the UPSC exam?"
sleep 2

read -p "Please enter the Rank: " Rank #with this command we will take input from user
echo "Shree: I secured the $Rank rank!"
sleep 2

and now run this script

./user_input.sh

Explanation of new commands used in this script

  • height=5.5: Assigns the value 5.5 to the variable height.

  • read -p "Please enter the Rank: " Rank: Prompts the user for input with the message "Please enter the Rank:" and stores the input in the variable Rank.

Third Script

Write a script that takes multiple arguments when executing and uses an if-else statement as well.

  • Argument: Arguments allow users to pass input values to a script for flexibility (e.g., $1, $2 for first, second inputs).

  • if-else: The if-else statement makes decisions based on conditions, executing different code based on whether the condition is true or false.

#!/bin/bash
#########################
# Author: Anju
# Date: 11/4/2025
# version: V1
# script that takes multiple arguments when executing and uses an if-else statement as well
###

echo "Shree ki rank $1 hai"
sleep 2
echo "Anju ki rank $2 hai"
sleep 2
echo "chiki ki rank $3 hai"
sleep 2
echo "kiki ki rank $4 hai"

# 1st if-else block

if [ $1 -eq 1 ]; then #spcae must be there between if and [], else it will throw an syntex error
        echo "Shree is topper"
else
        echo "Shree Topper nahi hai"
fi

# 2nd if-elif-else

if [ $1 -eq 1 ]; then  #spcae must be there between if and [], else it will throw an syntex error
        echo "Shree is Topper"
elif [ $2 -eq 1 ]; then
        echo "Anju is topper"
elif [ $3 -eq 1 ]; then
        echo "chiki is topper"
elif [ $4 -eq 1 ]; then
        echo "kiki is Topper"
else
        echo "No one is topper"
fi

Now we will run this script with user arguments.

./user_arrgument.sh 4 3 1 2 #here 4 3 2 & 1 is user arrguments

Mistakes I made while writing this script:

  • ✅ Space between if and [, like if [ ... ]

  • ✅ No semicolon before then when using newline (you can use ; then if everything is on the same line)

  • ✅ Quoting variables like "$1" to avoid unexpected behaviour

done :)

More from this blog

DevOps journey

34 posts

In this DevOps journey, we’ll explore a range of DevOps tools and related projects.