shell study-12day--shell实例

1 , shell instance

( 1) Print the 99 multiplication table

[root@test shell]# vi for-1.sh  
#!/bin/bash 
for i in `seq 9` #variable   
i takes an integer between 1-9 
do 
        for j in `seq $i` #variable 
j respectively Take an integer between 1-1,1-2,1-3...1-9 
        do 
        echo -n "$i*$j = `echo $(($i*$j))` " 
#do not wrap Output the result of multiplying variable i and variable j 
        done 
        echo "" 
done

( 2) Ping the entire network segment address, and output the result to the specified file

[root@test shell]# vi ping.sh 
#!/bin/bash
for i in `seq 254`
do
        ping -c 3 192.168.0.$i &>/dev/null
  if [ $? = 0 ]; then
        echo "192.168.0.$i up ">>ping.txt
  else
        echo "192.168.0.$i down" >> ping.txt
  fi
done

( 3) Grab real-time traffic of designated network ports

[root@test shell]# vi liuliang.sh 
#!/bin/bash 
ethn=$1 
while true #Each 
time while true is executed, the true command will be called. 
#true is used to perform logical operations with other commands. The return status is always success; the return value is 0. 
do 
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed's/:/ /g' | awk'{print $2}') 
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed's/:/ /g' | awk'(print $10}') 
 sleep 1 
 RX_next=$(cat /proc/net/dev | grep $ethn | sed's/:/ /g' | awk'{ print $2}') 
 TX_next=$(cat /proc/net/dev | grep $ethn | sed's/:/ /g' | awk'{print $10}') 
 
 clear 
 echo -e "\t RX `date + %k:%M:%S` TX" 
 
 RX=$((${RX_next}-${RX_pre})) 
 TX=$((${TX_next}-${TX_pre})) 
 
 if [[ $RX -lt 1024 ]];
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi
 
 if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi
 echo -e "$ethn \t $RX $TX "
done
[root@test shell]# sh liuliang.sh eth0


Personal public number:

image.png

Guess you like

Origin blog.51cto.com/13440764/2575385