shell编程练习题库2

一、基础练习题

习题1、在/userdata下建立50个目录,格式为userdata1~50,给每个目录赋予下面的权限:文件所有者读写执行、文件用户组读执行、其他读
cat create.sh

#!/bin/bash

[ -d /userdata ] &&  mkdir -p  /userdata/user{1..50}  || mkdir -p  /userdata/user{1..50}
chmod -R 754 /userdata/user{1..50}

习题2、编写一个脚本,显示日期和事件,列出所有用户,写出系统的更新事件并将这些内容输出到文件logfile中

#!/bin/bash

>logfile
a=$(date +%Y-%m-%d" "%H:%M:%S)
echo $a >> logfile
w |  awk '{print $1}' > a.txt
sed -i '1,2d' a.txt
cat a.txt | uniq >> logfile
uptime >> logfile
sed -i '3s/^[ \t]//g' logfile
cat logfile

习题三、编写一个脚本,名为/root/bin/createscripts.sh ,当执行该脚本时,输入 createscripts.sh /path/newsh.sh 则会在指定路径生成脚本文件,并涵盖注释信息
cat createscripts.sh

#!/bin/bash

mkdir -p /path

[ -z "$1" ] && { echo "Usage:$(basename $0) arg1";exit -1; }
>$1
cat <<EOF>> $1
#!/bin/bash
# -------------------------
# Filename: hello.sh
# Revisoon: 1.0
# Date:     20190603
# Author:   ty
# Email:    ty@gmail.com
# Website:  www.ty.com
# --------------------------
EOF

习题三、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
自己写的脚本
cat osinfo.sh

#!/bin/bash

echo "This host information:"
echo "host is $(hostname)"
a=$(ls -l /etc/sysconfig/network-scripts | grep ifcf* | grep -v lo | awk -F ' ' '{print $9}')
b=${a/ifcfg-/}
ip=$(ip addr | grep $b | grep inet | awk -F ' ' '{print $2}' | awk -F '/' '{print $1}')
echo "iP address is ${ip}"
echo "OS version is `cat /etc/centos-release`"
echo "kernek is  $(uname -r)"
echo "Cpu type is $(lscpu | grep "Model name" | awk -F ':' '{print $2$3}')"
echo "Mem total is $(free -m | grep Mem | awk -F ' ' '{print $2}') MB"
echo "Disk infomation is 
$(fdisk -l /dev/sda | tail -3)"

别人写的脚本
cat osinfo1.sh

#!/bin/bash
                echo "当前主机系统如下:"
                echo "主机名: $(hostname)"
                echo "IPV4地址: "  "$(ifconfig|grep "inet "|grep -o "\([0-9]\{1,3\}\.\)\{3\}[1-9]\{1,3\}")"
                echo "操作系统版本:" "$(cat /etc/redhat-release)"
                echo "内核版本:" "$(uname -r)"
                echo "CPU型号:" "$(lscpu|grep -i "model name")"
                echo "内存大小" $(free -hm|head -2|tr -s " "  |tail -1|cut -d" " -f2)
                echo "磁盘容量" $(fdisk -l /dev/sda|head -2|tail -1|cut -d " " -f3,4)

习题四、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
cat etcbak.sh

#!/bin/bash

cp -r /etc /root/etc$(date +%Y-%m-%d)

习题五、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
cat diskmax.sh

#!/bin/bash

a=$(df -h | awk -F ' ' '{print $5}' | sed '1d' | head -1)
b=$(df -h | grep $a | tr -s " " | cut -d " " -f1,5)
echo "the max usage disk is $b"

习题五、编写脚本显示连接的主机的远程ip地址和连接数,并按照连接数来排序
自己写的脚本
cat display_ip.sh

#!/bin/bash

netstat -an|grep ESTAB | awk '{print $5}' | awk -F ':' '{print $1}' | sort | uniq -c

别人的脚本
cat display_ip2.sh

cat /var/log/nginx/access.log | cut -d" " -f1  | wort -r  

习题六、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第5个用户和第10用户的ID之和
cat sumid.sh

#!/bin/bash

a=$(cat /etc/passwd | head -5 | tail -1 | cut -d":" -f 3)
b=$(cat /etc/passwd | head -10 | tail -1 | cut -d":" -f 3)
c=$(($a + $b))

习题七、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
cat sumspace.sh

#!/bin/bash

if [ $# -lt 2 ];then
    echo "usage: $(basename $0) file1 file2"
    exit -1
fi
a1=$(cat $1| wc -l)
a2=$(egrep -v '#|^$' $1 | wc -l)
a3=$((${a1} - ${a2}))

b1=$(cat $2 | wc -l)
b2=$(egrep -v '#|^$' $2 | wc -l)
b3=$((${b1} - ${b2}))

c=$((a3 + b3))
echo $c

习题八、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
cat sumfile.sh

#!//bin/bash

j=0
k=0
for m in /etc /var /usr
do
    for i in $(ls ${m[@]})
    do  
        if [ -d ${m[@]}/$i ];then
            ((j++))
        else
           ((k++))
        fi
    done
done
echo "There are $j dir and $k file"

习题九:从下面字符串找出所有域名,排序

http://www.baidu.com/map/http://www.baidu.com/guding/more.htmlhttp://www.baidu.com/events/20180105/photomore.htmlhttp://hi.baidu.com/browse/http://www.sina.com.cn/head/www20021123am.shtmlhttp://www.sina.com.cn/head/www20041223am.shtml/http://www.bluevision.com/index.htmlhttp://www.domob.com/langding/a.jpg

cat yy.sh

#!/bin/bash

str="http://www.baidu.com/map/http://www.baidu.com/guding/more.htmlhttp://www.baidu.com/events/20180105/photomore.htmlhttp://hi.baidu.com/browse/http
://www.sina.com.cn/head/www20021123am.shtmlhttp://www.sina.com.cn/head/www20041223am.shtml/http://www.bluevision.com/index.htmlhttp://www.domob.com/l
angding/a.jpg"
a=$(echo $str)
b=${a//http:\/\// /}

for i in ${b[@]}}
do
    echo $i  | awk -F '/' '{print $2}' >> yy.txt
done

cat yy.txt | uniq -c | sort -u -r