Linux shell scripting is the art of writing small programs (scripts) to automate tasks in the command-line interface, which involves using commands, variables, loops, and conditionals to create efficient and automated workflows.

This article is an extension of our first article, understanding linux shell scripting. In that piece, we introduced you to scripting. Continuing from there, we aim to maintain the momentum and not disappoint you in this article.

Script 1: Drawing a Special Pattern

The following “” Bash script prompts the user to input a number between 5 and 9. If the input is within this range, the script proceeds to create a pattern of dots in two stages: an ascending pattern and a descending pattern.

The script utilizes nested loops for this purpose, creating a visually appealing pattern. Finally, a message is displayed, indicating support from whenever assistance is needed.

#!/bin/bash MAX_NO=0
echo -n "Enter a number between (5 to 9): "
read MAX_NO if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ]; then echo "Please enter a number between 5 and 9. Try again." exit 1
fi clear for ((i=1; i<=MAX_NO; i++)); do for ((s=MAX_NO; s>=i; s--)); do echo -n " " done for ((j=1; j<=i; j++)); do echo -n " ." done echo ""
done ###### Second stage ###################### for ((i=MAX_NO; i>=1; i--)); do for ((s=i; s<=MAX_NO; s++)); do echo -n " " done for ((j=1; j<=i; j++)); do echo -n " ." done echo ""
done echo -e "nnttt Whenever you need help, is always there"

Most of the above ‘keywords‘ should be familiar to you, and many are self-explanatory. For example, ‘MAX‘ sets the maximum value of the variable, ‘for‘ is a loop, and anything within the loop executes repeatedly until the loop is valid for the given input value.

Special_Pattern Script
Special_Pattern Script

If you are a little aware of any programming language, learning the above script is not difficult, even if you are new to computation, programming, and Linux it is not going to be much more difficult.

Script 2: Creating Colorful Script

The “” script is a Bash script created to demonstrate the use of ANSI escape codes for adding color and formatting effects to text output in the terminal.

These escape codes provide a simple way to enhance the visual appeal of terminal-based scripts or programs, which display various text styles, colors, and background colors, allowing users to experiment with different combinations.

clear echo -e "e[1mHello World" # bold effect
echo -e "e[5mBlink" # blink effect
echo -e "e[0mHello World" # back to normal
echo -e "e[31mHello World" # Red color
echo -e "e[32mHello World" # Green color
echo -e "e[33mHello World" # Yellow color
echo -e "e[34mHello World" # Blue color
echo -e "e[35mHello World" # Magenta color
echo -e "e[36mHello World" # Cyan color
echo -e "e[0m" # back to normal
echo -e "e[41mHello World" # Red background
echo -e "e[42mHello World" # Green background
echo -e "e[43mHello World" # Yellow background
echo -e "e[44mHello World" # Blue background
echo -e "e[45mHello World" # Magenta background
echo -e "e[46mHello World" # Cyan background
echo -e "e[0mHello World" # back to normal
Creating Colorful Shell Script
Creating Colorful Shell Script

Script 3: Encrypt a File or Directory

The “” script is a Bash script designed to provide a simple interface for encrypting a file using the GnuPG (GPG) encryption tool.

The script welcomes the user and prompts them to enter the exact filename, including the extension, of the file or folder they want to encrypt. It then uses GPG to encrypt the specified file, displaying a success message afterward.

Additionally, the script removes the original unencrypted file for added security. Note that the script should be placed in the same directory as the file to be encrypted due to its current limitation.

echo "Welcome! I am ready to encrypt a file/folder for you."
echo "Currently, I have a limitation. Please place me in the same folder where the file to be encrypted is located."
echo "Enter the exact file name with the extension."
read file;
gpg -c $file
echo "I have successfully encrypted the file..."
echo "Now, I will be removing the original file."
rm -rf $file

The command "gpg -c filename" is used to encrypt a file using GnuPG (GNU Privacy Guard) with symmetric encryption and the command "gpg -d filename.gpg > filename" is used to decrypt a GPG-encrypted file and save the decrypted content into a new file.

Encrypt File Script
Encrypt File Script

Script 4: Linux Server Monitor Script

The “” script is a Bash script designed to provide a comprehensive overview of the health and performance of a server.

When executed, the script gathers various system-related information and presents it in a structured format that includes uptime, currently connected users, disk and memory usage, list open ports, network connections, running processes, and system statistics.

#!/bin/bash date; echo "uptime:" uptime echo "Currently connected:" w echo "--------------------" echo "Last logins:" last -a |head -3 echo "--------------------" echo "Disk and memory usage:" df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}' free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' echo "--------------------" start_log=`head -1 /var/log/messages |cut -c 1-12` oom=`grep -ci kill /var/log/messages` echo -n "OOM errors since $start_log :" $oom echo "" echo "--------------------" echo "Utilization and most expensive processes:" top -b |head -3 echo top -b |head -10 |tail -4 echo "--------------------" echo "Open TCP ports:" nmap -p- -T4 echo "--------------------" echo "Current connections:" ss -s echo "--------------------" echo "processes:" ps auxf --width=200 echo "--------------------" echo "vmstat:" vmstat 1 5
Linux Server Health Script
Linux Server Health Script

To redirect the output of the “” script to a file, you can use the following command:

./ > server_health_report.txt

Script 5: Linux Disk Space Monitor Script

The “” script is designed to monitor disk space usage on a specified partition of a server. Configurable parameters include the maximum allowed disk space usage percentage (`MAX`), the email address for receiving alerts (`EMAIL`), and the target partition (`PARTITION`).

The script utilizes the df command to gather disk usage information and sends email alerts if the current usage exceeds the defined threshold.

#!/bin/bash # Set the maximum allowed disk space usage percentage
MAX=90 # Set the email address to receive alerts
[email protected] # Set the partition to monitor (change accordingly, e.g., /dev/sda1)
PARTITION=/dev/sda1 # Get the current disk usage percentage and related information
USAGE_INFO=$(df -h "$PARTITION" | awk 'NR==2 {print $5, $1, $2, $3, $4}' | tr 'n' ' ')
USAGE=$(echo "$USAGE_INFO" | awk '{print int($1)}') # Remove the percentage sign if [ "$USAGE" -gt "$MAX" ]; then # Send an email alert with detailed disk usage information echo -e "Warning: Disk space usage on $PARTITION is $USAGE%.nnDisk Usage Information:n$USAGE_INFO" | mail -s "Disk Space Alert on $HOSTNAME" "$EMAIL"

Script writing and programming have no limits; you can implement anything as needed. That’s all for now. In my next article, I will introduce you to some different scripting flavors.

Similar Posts