NASutils Knowledge BaseUnofficialPolski

Bash · lesson 6 of 30 · First steps · bonusDecide with if

if, elif, else and fi. [[ ]] tests text, (( )) tests numbers.

Programming on the NAS › Bash in 30 lessons

06-if.sh Download 06-if.sh

#!/usr/app/TurboShell/bin/bash
read -rp "Disk temperature in C: " temp

if [[ ! $temp =~ ^(0|[1-9][0-9]{0,3})$ ]]; then
  echo "Type a whole number"
elif ((temp > 100)); then
  echo "Type a number from 0 to 100"
elif ((temp >= 55)); then
  echo "Too hot: check the fans"
elif ((temp >= 45)); then
  echo "Warm: look again in an hour"
else
  echo "Fine"
fi

Terminal

$ bash 06-if.sh
Disk temperature in C: 38
Fine
$ bash 06-if.sh
Disk temperature in C: 58
Too hot: check the fans
$ bash 06-if.sh
Disk temperature in C: hot
Type a whole number

Exercise

Ask for the free space in percent and answer fine, low or full.