Linux-简-脚本集合

编写脚本,求100以内所有正奇数之和

while加 if 判断 

#!/bin/bash
# 
#
#
sum=0
i=1
while (($i<=100));do

sur=$[i%2]
        if [ $sur -eq 0 ];then
                let i++
        else
                let sum+=i
                let i++
        fi

done
echo $sum

 

 

 

 

编写脚本,打印九九乘法表

for循环

#!/bin/bash
# 
#
for ((i=1;i<=9;i++));do
        for ((j=1;j<=i;j++));do
                printf "${i}x${j}=$[i*j]\t"
        done
                printf "\n"
done

 while循环

#!/bin/bash
# 
#
i=1
j=1
while ((i<=9));do
        j=1
    while ((j<=i));do
        printf "${i}x${j}=$[i*j]\t"
        let j++
    done
        let i++
        printf "\n"
done

 

 

编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值

#!/bin/bash
# 
#
min=$RANDOM
max=$min
i=1

while ((i<=10));do
      let i++
      RD=$RANDOM
      echo $RD
   if [ $RD -ge $max ];then
      max=$RD
   else if [ $RD -lt $min ];then
      min=$RD
        fi
   fi
done
echo "maximum is $max"
echo "minimum is $min"

 编写脚本 systeminfo.sh,显示当前主机系统信息

#!/bin/bash
#systeminfo
#20220415
#


echo
echo "**************************HOST systeminfo*****************************"
echo "HOSTNAME:               `hostname`"
echo "IPADDR:             `ifconfig |head -2|tail -1|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1`"
echo "OSVERSION:    `cat /etc/redhat-release`"
echo "KERNEL:            `uname -r`"
echo "CPU:    `lscpu | grep "Model name"|tr -s ' '|cut -d":" -f2|head -1`"
echo "MEMORY:               `free -h | tr -s ' '|grep Mem|cut -d' ' -f2`"
echo "DISK               `lsblk | grep "^sda"|tr -s ' ' | cut -d" " -f4`"
echo "***********************************************************************"

编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
#
#Maximum disk space
#USAGE 使用率 
#DISKINFO=`df -h | grep "$USAGE"`
USAGE=`df -h |grep -v "Filesystem" |tr -s ' ' |cut -d' ' -f5 |sort -nr|head -1`
DISK=`df -h | grep "$USAGE" | tr -s ' ' | cut -d' ' -f1`
SIZE=`df -h | grep "$USAGE" | tr -s ' ' | cut -d' ' -f2`
USED=`df -h | grep "$USAGE" | tr -s ' ' | cut -d' ' -f3`
AVAIL=`df -h | grep "$USAGE" | tr -s ' ' | cut -d' ' -f4`
FDISK=`df -h | grep "$USAGE" | tr -s ' ' | cut -d' ' -f6`
echo "diskinfo:"

#echo "$DISKINFO"

echo "$DISK"
echo
echo  -e "size:$SIZE; userd:\E[1;31m$USED\E[0m; avail:\E[1;32m$AVAIL\e[0m; use%:\E[1;35m$USAGE\e[0m "
echo
echo -e  "\e[1;36mdisk-partition: $DISK\e[0m"
echo "File-directory: $FDISK"

posted @ 2022-04-24 20:31  goodbay说拜拜  阅读(39)  评论(0编辑  收藏  举报