Operations 5 min read

Master Bash Loops: Sum, Even Numbers, Multiplication Table, and System Checks

This tutorial presents a collection of Bash scripts demonstrating how to calculate sums with for, while, and until loops, compute the sum of even numbers, generate a 9×9 multiplication table, monitor user login status, and retrieve system information via a case menu.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Bash Loops: Sum, Even Numbers, Multiplication Table, and System Checks

Calculate the sum from 1 to 100

Using a for loop:

#!/bin/bash
declare -i sum=0
for ((i=1;i<=100;i++)); do
  let sum+=$i
done
echo "sum=$sum"

Using an until loop:

#!/bin/bash
i=1
sum=0
until [ $i -gt 100 ]; do
  let sum+=$i
  let i++
done
echo "sum is:$sum"

Using a while loop:

#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]; do
  let sum+=$i
  let i++
done
echo "sum is:$sum"

Sum of even numbers within 100

Using a for loop:

#!/bin/bash
i=0
sum=0
for i in `seq 100`; do
  if [ $[$i%2] -eq 1 ]; then
    continue
  fi
  let sum+=$i
done
echo "sum is:$sum"

Using a while loop:

#!/bin/bash
i=0
sum=0
while [ $i -le 100 ]; do
  let i++
  if [ $[$i%2] -eq 1 ]; then
    continue
  fi
  let sum+=$i
done
echo "sum is: $sum"

Using an until loop:

#!/bin/bash
i=0
sum=0
until [ $i -gt 100 ]; do
  let i++
  if [ $[ $i%2 ] -eq 1 ]; then
    continue
  fi
  let sum+=$i
done
echo "sum is: $sum"

Generate a 9×9 multiplication table

Using a for loop:

#!/bin/bash
for ((j=1;j<=9;j++)); do
  for ((i=1;i<=j;i++)); do
    echo -ne "$i*$j=$(($i*$j))\t"
  done
  echo
 done

Using a while loop:

#!/bin/bash
i=1
j=1
while [ $j -le 9 ]; do
  while [ $i -le $j ]; do
    echo -ne "$i*$j=$(($i*$j))\t"
    let i++
  done
  echo
  let i=1
  let j++
 done

Using an until loop:

#!/bin/bash
i=1
j=1
until [ $j -gt 9 ]; do
  until [ $i -gt $j ]; do
    echo -ne "$i*$j=$(($i*$j))\t"
    let i++
  done
  echo
  let i=1
  let j++
 done

Check if a user is logged in and log the event

Using a while loop to poll every 10 seconds:

#!/bin/bash
read -p "pls input a username: " username
while ! `who | grep "^$username" > /dev/null`; do
  sleep 10
done
echo "$(date +%F-%H:%M:%S) $username logged on" >> /tmp/user.log

Retrieve system information via a case menu

#!/bin/bash
cat <<EOF
1) show cpu information;
2) show memory information;
3) show disk information;
4) quit
EOF

cpu_info(){
  lscpu
}

mem_info(){
  cat /proc/meminfo
}

disk_info(){
  fdisk -l
}

quit(){
  echo "quit"
  exit 0
}

read -p "pls input a num: " num
if [ $num -ne 1 -a $num -ne 2 -a $num -ne 3 -a $num -ne 4 ]; then
  read -p "pls input a num again: " num
fi

case "$num" in
  1) cpu_info ;;
  2) mem_info ;;
  3) disk_info ;;
  4) quit ;;
esac
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BashShell scriptingLoops
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.