1-100 the sum of the natural numbers: Law one: For Loop #!/bin/bash#declare-i sum=0for ((i=0;i<=100;i++));d o let sum+= $idoneecho "sum: $sum" Law two: While loop #!/bin/bash#declare-i sum=0declare-i i=0while [$i-le];d o let sum+= $i let I++doneecho $iecho "Summary: $sum. " Script for Statistics TCP connection Status: #!/bin/bash# This script was Count TCP statusdeclare-i estab=0declare-i listen=0declare-i other=0for State In $ (Netstat-tan | grep "^tcp\>" | awk "{print $NF}");d o if ["$state" = = ' established '];then let estab++ elif [ "$state" = = ' LISTEN '];then let listen++ else let other++ Fidoneecho "established: $estab" echo "LISTEN: $listen" Ech O "Unknow: $other" Add 10 users in bulk: #!/bin/bashif [! $UID-eq 0];then echo "Only the root can use this script." Exit 1fifor i in {1..10};d o if ID user$i &>/dev/null;then echo "user$i exist" else useradd user$i if [ $? -eq 0];then echo "user$i" | passwd--stdin user$i &>/dev/null fi fidone test connectivity scripts for hosts within a network segment: #!/bin/bash#ping net= ' 172.16.1 ' uphosts=0downhosts=0 For I in {1..254};d o ping-c 1-w 1 ${net}.${i} &>/dev/null if [$?-eq 0];then echo "${net}.${i} is up." Let uphosts++ else echo "${net}.${i} was down." Let downhosts++ Fidoneecho ' up hosts: $uphosts ' echo ' Down hosts: $downhosts ' factorial of the Count: #!/bin/bash#fact () {if [$1-eq 0-o $ -eq 1];then Echo 1 else echo $[$1*$ (fact $[$1-1]) Fi}fact 5
Shell Script Small Example