Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Bash Conditional Expressions and Operator Patterns

Notes Aug 4 1

Extended Pattern Matching

Using double brackets to validate input falls outside a specific range:

[[ ! $score =~ ^[1-3]$ ]] && {
  echo "Value must be between 1 and 3"
  exit 1
}

Example 1: Single Character Validation

Accept a single digit input and output the corresponding value, displaying an error for invalid entries.

Script: /opt/tools/checker.sh

#!/bin/bash
echo -n "Enter a digit: "
read input
[ "$input" = "7" ] && {
  echo "Seven"
  exit 0
}
[ "$input" -eq 9 ] && {
  echo "Nine"
  exit 0
}
[ "$input" != "7" -a "$input" -ne 9 ] && {
  echo "Invalid input"
  exit 0
}

Execution results:

$ bash /opt/tools/checker.sh
Enter a digit: 7
Seven

$ bash /opt/tools/checker.sh
Enter a digit: 9
Nine

$ bash /opt/tools/checker.sh
Enter a digit: 4
Invalid input

Example 2: Comparing Two Integer Values

Read two integers from the user and display their relationship without using if statements. The script validates numeric input and checks parameter count.

Script: /opt/tools/compare.sh

#!/bin/bash
read -t 15 -p "Enter two integers: " x y
[ -z "$x" ] || [ -z "$y" ] && {
  echo "Error: provide both numbers"
  exit 1
}
[[ ! $x =~ ^[0-9]+$ ]] && {
  echo "Error: first value not numeric"
  exit 2
}
[[ ! $y =~ ^[0-9]+$ ]] && {
  echo "Error: second value not numeric"
  exit 3
}
[ $x -eq $y ] && {
  echo "x equals y"
  exit 0
}
[ $x -gt $y ] && echo "x is greater than y" || echo "x is less than y"

Example 3: Web Stack Installation Menu

Display a selection menu for various web service installations and execute the corresponding setup script.

Script: /opt/tools/install.sh

#!/bin/bash
base=/opt/tools
[ ! -d "$base" ] && mkdir -p "$base"

cat <<MENU
1. Deploy Apache + MySQL + PHP
2. Deploy Nginx + MySQL + PHP
3. Quit
Select an option: 
MENU

read choice
expr "$choice" + 0 >/dev/null 2>&1
[ $? -ne 0 ] && {
  echo "Invalid selection: $choice"
  exit 1
}

case $choice in
  1)
    echo "Installing LAMP stack..."
    sleep 2
    [ -f "$base/lamp_install.sh" ] && {
      [ -x "$base/lamp_install.sh" ] || {
        echo "LAMP script lacks execute permission"
        exit 2
      }
      "$base/lamp_install.sh"
    }
    ;;
  2)
    echo "Installing LNMP stack..."
    sleep 2
    [ -f "$base/lnmp_install.sh" ] && source "$base/lnmp_install.sh"
    ;;
  3)
    echo "Exiting installation"
    exit 0
    ;;
  *)
    echo "Valid options: 1, 2, or 3"
    exit 5
    ;;
esac

The script creates the target directory if absant, presents a menu interface, validates numeric input, and handles three scenarios: Apache stack enstallation, Nginx stack installation via sourcing, and graceful exit.

Tags: bash

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.