Exercises on shelll

This article is from http://blog.51cto.com/13572413/2063930, which can be used as a basic theoretical study

shell script exercises

1. Write the script / root/bin/systeminfo.sh to display the current host system information, including the host name, IPv4 address, operating system version, kernel version, cpu model, memory size and hard disk size.

            # Display computer hardware information
            echo "Host name:      $(hostname)"
            echo "IPv4 Address:    $(ifconfig | head -n2 |tail -n1 |tr -s " " ":"| cut -d":" -f3)"
            echo "Operating system version: $(cat /etc/redhat-release)"
            echo "Kernel version:    $(uname -r)"
            echo "CPU model:     $(cat /proc/cpuinfo|grep "model name"|uniq -c|cut -d":" -f2)"
            echo "Memory size:    $(free -m | head -n2 |tail -n1|tr -s " " ":"|cut -d":" -f2)"
            echo "Hard disk size:    $(fdisk -l | grep "GB"|cut -d":" -f2|cut -d"," -f1)"

2. Write the script / root/bin/backup.sh to back up the / etc / directory to / root / etxyyy MM DD every day

    cp -r /etc/ /root/etc"$(date  +%F)"

3. Write a script / root/bin/disk.h to display the maximum space utilization value in the current hard disk partition

        max=$(df |tr -s " " ":"|cut -d ":" -f5|egrep -o "[[:digit:]]*"|sort -nr|head -n1)
        echo "The maximum space utilization in the current hard disk partition is: $max"

4. Write the script / root/bin/links.sh to display the IPv4 address and the number of connections of each remote host that is connecting to the host, and sort by the number of connections from large to small

        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 "Number of connections and IP The addresses are:$links"
        exit 0

5. Write the script / root/bin/sumid.sh to calculate the sum of the ID s of the 10th user and the 20th user in the etc/passwd file

    usera=$(cat -n /etc/passwd | head |tail -n1|cut -d":" -f3)
    echo "id For 10 uid:$usera"
    userb=$(cat -n /etc/passwd | head -n20|tail -n1|cut -d":" -f3)
    echo "id For 20 uid:$userb"
    # userall=$usera+$userb
    let usera=$usera+$userb
    echo "id And are: $usera"
    exit 0

6. Write the script / root/bin/sumspace.sh, pass two file paths as parameters to the script, and calculate the sum of all blank lines in the two files.

    a=$(cat $1|grep "^[[:space:]]*$"|wc -l)
    b=$(cat $2|grep "^[[:space:]]*$"|wc -l)
    #let a=$a+$b
    let c=$a+$b
    echo "$1,$2The sum of the blank lines of the two documents is$c"
    exit 0

7. Write the script / root/bin/sumfile.sh, count the number of first level subdirectories and files in / etc,./var,/usr directory

    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 There are$efFirst level subdirectories,$edFiles"
    echo "/var There are$vfFirst level subdirectories,$vdFiles"
    echo "/usr There are$ufFirst level subdirectories,$udFiles"

8. Write a script to generate a script, / root/bin/createsh.sh when executing such as / root/bin/createsh.sh test. Sh, a new file named test.sh will be automatically created under / root/bin. The file will automatically give execution permission, and automatically add "#" and comment information to the file header. Note information is as follows:

        # ------------------------------------------
        # Filename: test.sh (automatically changed here according to the filename)
        # Revision:    1.0
        # Date: 2017-01-22 (the current date will be automatically changed here)
        # Author:      Nanyibo
        # Email:       nanyibo@gmail.com
        # ------------------------------------------
        # Copyright:   2017 nanyibo
        # License:     GPL

After that, it is automatically opened with vim, waiting for the user to start writing the script body

            touch  /root/bin/$1
            chmod +x /root/bin/$1
            echo "#!/bin/bash
                        # ------------------------------------------
                        # Filename:    $1(Automatically change according to the file name)
                        # Revision:    1.0
                        # Date:        $(date +%F)(This will automatically change to the current date)
                        # Author:      xiaoyu
                        # Email:       851812316@qq.com
                        # ------------------------------------------
                        # Copyright:   2018 xiaoyu
                        # License:     GPL" > /root/bin/$1
            vim /root/bin/$1

Tags: vim shell

Posted on Sat, 02 May 2020 05:01:52 -0700 by QuietWhistler