Shell 初学练习题

1.显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

#!/bin/bash
echo "System information List:"
echo " -----------------------"
h=`hostname`
echo  "Host Name is                : $h"
ens33=`ifconfig | head -n 2 | grep "inet" | cut -d' ' -f10`
lo=`ifconfig | head -n 11 | tail -n 1 | grep "inet" | cut -f10 -d' '`
echo "IPV4 Address is : ens33     : $ens33"
echo "                  lo        : $lo "    
v=`cat /etc/redhat-release`
echo "operating system version is : $v"
u=`uname -a | cut -f1,3 -d' '`
echo "kernel version is           : $u"
m=`grep "model name" /proc/cpuinfo | cut -f2 -d:`
echo "CPU model is                :$m"
t=`grep MemTotal /proc/meminfo`
echo "Memory Size is              : $t"
d=`fdisk -l | grep "Disk /dev/sda" | cut -f1 -d,`
echo "Hard Disk Size is           : $d"

在这里插入图片描述
2. 将/etc/目录备份到/tmp下,并以此格式保存bak_etc_yyyy-mm-dd,保存为脚本bak_etc.sh

#!/bin/bash
tar -czvf /tmp/bak_etc_`date +%F`.tar.gz /etc

在这里插入图片描述

3.显示当前硬盘分区中空间利用率最大的值,保存脚本为disk_used.sh

#!/bin/bash
u=`df -h | grep "^/dev/sd." | tr -s " " ":" | cut -d: -f5 |tr -d % | sort -nr | head -n 1`
echo "The maximum value of space utilization in the hard disk partition : $u"

在这里插入图片描述
4.批量生成随机字符文件名;在/tmp目录下批量创建10个html文件,其中每个文件需要包10个随机小写字母加固定字符串tmp

#!/bin/bash
dir=/tmp/
#[ ! -d $dir ] && mkdir -p $dir
for ((i=0;i<10;i++))
do
    touch $dir`echo $RANDOM | md5sum | cut -c 1-10 | tr -s "0-9" "a-z"`_tmp.html
done

在这里插入图片描述
5.批量创建10个系统帐号user01-user10并设置密码(密码为随机数,要求字符和数字等合)

#!/bin/bash
for a in user{01..10}
do 
  useradd $a
echo "mktemp | awk -F. '{print $2}'"| passwd $a --stdin
done

6.判断自己的网络里,当前在线的IP有多少?

#!/bin/bash
for ip in `seq 1 255`
do 
ping -c 1 192.168.247.$ip (ifconfig看自己ip段)
if [ $? -eq 0 ]
then 
echo is up >> ipsum
else 
echo is down >> ipsum
fi
done
cat /home/scripts/ipsum | grep "is up" | wc -l  (注意ipsum文件的生成位置编写脚本的位置就是它所在位置)

7.计算1-100的和.

#!/bin/bash
sum=0
for ((i=1;i<=100;i++))
# for i in {1..100}
do
sum=$[ $sum + $i ]
done
echo "sum=$sum"

8.编写乘法口诀

#!/bin/bash
for (( a=1;a<10;a++ ))
do
 echo "$a"
 for (( b=1;b<10;b++ ))
  do
   echo "$b"
   c=$[ $a*$b ]
   echo "$a*$b=$c"
  done 
done

9.提示用户登录,登录后显示欢迎该用户登录,5秒内未登录结束。

#!/bin/bash
if read -t 5 -p "Please enter your name:" name
 then 
  echo "hellow $name, welcome to login"
 else 
 echo "Sorry, too slow"
fi

10…把/root/目录下的所有目录 拷贝到/tmp/目录下

#!/bin/bash
for i in `ls /root/`
do
    if [ -d $i ]
    then
        cp -r $i /tmp/
    fi
done

11…生成1-50内取随机数

#!/bin/bash
echo $(($RANDOM%50+1))

12.生成一个文件,格式为2018-05-25-diskinfo.log, 并且把磁盘的使用情况写到到这个文中

#!/bin/bash
df -Th >  `date +%F`-diskinfo.log

13…统计/var/log目录下的文件数

#!/bin/bash
ls -l /var/log/ | grep "^-" | wc -l

14.shell下生成32位随机密码

#!/bin/bash
date +%s | sha256sum | base64 | head -c 32

15.在脚本中生成50-70的偶数

#!/bin/bash
n=70
while [ $n -gt 50 ]
do 
  if [ $(($n%2)) -eq 0  ]
  then
     echo "偶数:$n"
  fi
  n=$(($n-1))
done

扩展:shell练习(https://blog.csdn.net/weixin_43168314/article/details/88954059)(https://blog.csdn.net/weixin_43168314/article/details/86660011)(https://blog.csdn.net/weixin_43168314/article/details/86896719)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值