Mastering Bash: Programs for practice

Mastering Bash: Programs for practice


Hey there,

Just a casual Bash script programs that you can use to practice bash with me.

These are the programs that i practised for learning bash scripting along with my System administrator job.

1. File Existence Checker

Its a straightforward file existence checker. It’s a handy tool to determine if a file is present in the directory. Here’s the code:

#!/bin/bash

echo "Enter the filename to check: "
read filename

if [ -e "$filename" ]; then
    echo "File $filename exists."
else
    echo "File $filename does not exist."
fi

How It Works:

  • The script prompts the user for a filename.

  • It then checks if the file exists using the -e test in Bash.

  • Depending on the result, it prints out whether the file exists or not.

This is a simple yet powerful script to get familiar with basic conditional checks in Bash.

2. Palindrome Checker

Next up, we have a script that checks if a given string is a palindrome. A palindrome is a word or phrase that reads the same backward as forward, like "radar" or "level". Here’s the script:

#!/bin/bash

is_palindrome() {
  local str="$1"
  local reversed
  local length
  local i

  str=$(echo "$str" | tr -d '[:space:]' | tr -cd '[:alnum:]' | tr '[:upper:]' '[:lower:]')

  length=${#str}

  reversed=""
  for (( i=length-1; i>=0; i-- )); do
    reversed="${reversed}${str:i:1}"
  done

  if [ "$str" == "$reversed" ]; then
    echo "Yes, it's a palindrome."
  else
    echo "No, it's not a palindrome."
  fi
}

read -p "Enter a string to check if it's a palindrome: " input

is_palindrome "$input"

How It Works:

  • The is_palindrome function preprocesses the input string by removing spaces and non-alphanumeric characters and converting it to lowercase.

  • It then reverses the string and compares it to the original.

  • The script prints whether the input string is a palindrome.

This script is excellent for practicing string manipulation and working with functions in Bash.

3. Basic Calculator

Finally, we have a basic calculator script that performs simple arithmetic operations. Here’s how it looks:

#!/bin/bash

echo "Enter first number:"
read num1
echo "Enter operator (+, -, *, /):"
read op
echo "Enter second number:"
read num2

case $op in
    +)
        result=$(echo "$num1 + $num2" | bc)
        ;;
    -)
        result=$(echo "$num1 - $num2" | bc)
        ;;
    \*)
        result=$(echo "$num1 * $num2" | bc)
        ;;
    /)
        result=$(echo "scale=2; $num1 / $num2" | bc)
        ;;
    *)
        echo "Invalid operator"
        exit 1
        ;;
esac

echo "Result: $result"

How It Works:

  • The script prompts the user for two numbers and an operator.

  • It uses the bc command to perform arithmetic operations based on the input operator.

  • The result is displayed to the user.

This script is a great exercise in using conditionals and handling user input in Bash.

Wrapping Up

There you have it—three practical Bash scripts to enhance your scripting skills! From checking file existence to reversing strings and doing basic math, these examples cover fundamental concepts and are a fantastic way to get comfortable with Bash.