Shell Scripting Exercises

Source: Internet
Author: User
Tags egrep

Shell script exercises 1. Script/root/bin/, which displays the current host system information, including hostname, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size.
            #!/bin/bash            # 显示电脑硬件信息            echo "主机名:      $(hostname)"            echo "IPv4地址:    $(ifconfig | head -n2 |tail -n1 |tr -s " " ":"| cut -d":" -f3)"            echo "操作系统版本:$(cat /etc/redhat-release)"            echo "内核版本:    $(uname -r)"            echo "CPU型号:     $(cat /proc/cpuinfo|grep "model name"|uniq -c|cut -d":" -f2)"            echo "内存大小:    $(free -m | head -n2 |tail -n1|tr -s " " ":"|cut -d":" -f2)"            echo "硬盘大小:    $(fdisk -l | grep "GB"|cut -d":" -f2|cut -d"," -f1)"
2. Scripting/root/bin/ for daily/etc/directory backup to/root/etcyyyy-mm-dd
    #!/bin/bash    cp -r /etc/ /root/etc"$(date  +%F)"
3. Write script/root/bin/disk.h, showing the maximum space utilization in the current hard disk partition
        #!/bin/bash        max=$(df |tr -s " " ":"|cut -d ":" -f5|egrep -o "[[:digit:]]*"|sort -nr|head -n1)        echo "当前硬盘分区中空间利用率最大的值为: $max"
4. Script/root/bin/, showing the number of IPV4 addresses and connections for each remote host connecting to this host, sorted by number of connections from large to small
        #!/bin/bash        links=$(netstat -tan| grep "^[t]\|[u]"|tr -s " " ":"|cut -d":" -f6|uniq -c|sort -nr|egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"|tail -n1)        echo "连接数和IP地址分别为:$links"        exit 0
5. Write script/root/bin/, calculate the sum of the tenth user 20th User ID in the etc/passwd file
    #!/bin/bash    usera=$(cat -n /etc/passwd | head |tail -n1|cut -d":" -f3)    echo "id为10的uid:$usera"    userb=$(cat -n /etc/passwd | head -n20|tail -n1|cut -d":" -f3)    echo "id为20的uid:$userb"    # userall=$usera+$userb    let usera=$usera+$userb    echo "id和为: $usera"    exit 0
6. Write the script/root/bin/, pass two file paths as parameters to the script, calculate the sum of all the blank lines in the two files.
    #!/bin/bash    a=$(cat $1|grep "^[[:space:]]*$"|wc -l)    b=$(cat $2|grep "^[[:space:]]*$"|wc -l)    #let a=$a+$b    let c=$a+$b    echo "$1,$2两个文件的空白行之和为$c"    exit 0
7. Scripting/root/bin/, count the number of sub-directories and files in the/ETC,./VAR,/USR directory
    #!/bin/bash    ef=$(ls /etc/* | grep  ".*[:]$"|wc -l)    ed=$(ls /etc/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)    vf=$(ls /var/* | grep  ".*[:]$"|wc -l)    vd=$(ls /var/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)    uf=$(ls /usr/* | grep  ".*[:]$"|wc -l)    ud=$(ls /usr/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)    echo "/etc目录中一共有$ef个一级子目录,$ed个文件"    echo "/var目录中一共有$vf个一级子目录,$vd个文件"    echo "/usr目录中一共有$uf个一级子目录,$ud个文件"
8. Write a script that generates a script that/root/bin/ when executed such as/root/bin/ test. SH, automatically in/root/ A new file named is created under Bin, which automatically gives execute permission and automatically adds #! and annotation information to the header of the file. Note the following information:
        #!/bin/bash        # ------------------------------------------        # Filename:此处会根据文件名自动更换)        # Revision:    1.0        # Date:        2017-01-22(此处会自动变换为当前日期)        # Author:      Nanyibo        # Email:       [email protected]        # ------------------------------------------        # Copyright:   2017 nanyibo        # License:     GPL
After that, automatically opens with Vim, waiting for the user to start writing the script body
            #!/bin/bash            touch  /root/bin/$1            chmod +x /root/bin/$1            echo "#!/bin/bash                        # ------------------------------------------                        # Filename:    $1(此处会根据文件名自动更换)                        # Revision:    1.0                        # Date:        date +%F(此处会自动变换为当前日期)                        # Author:      xiaoyu                        # Email:       [email protected]                        # ------------------------------------------                        # Copyright:   2018 xiaoyu                        # License:     GPL" > /root/bin/$1            vim /root/bin/$1

For the first time after the homework was finished the night before, I hereby record it. Cheer Up ~

Shell Scripting Exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.