Linux user script creation/guessing game/network card traffic monitoring introduction

Linux user script creation/guessing game/network card traffic monitoring introduction

1. User created script

(1) Design file judgment module

insert image description here

(2) Design user establishment

insert image description here

(3) File content

insert image description here

Script content

#!/bin/bash
USERADD(){ #User created function RN=$(sed -n '$=' $1)
        for ((I=1;I<=$RN;I++))
        do
                user=`sed -n "$I"p $1`
                password=`sed -n "$I"p $2`

                id $user &>/dev/null # Check if the user exists [ $? -eq 0 ] && {
                        echo "$user already exists"
                }||{
                        useradd $user #Create a new user in $user echo "$password" | passwd --stdin $user &> /dev/null
                        [ $? -eq 0 ] && {
                                echo "$user user created successfully"
                        }
                }
        done
}
# Judgment module [ $# -lt 2 ] && { #Judge whether the file is less than two echo "ERROR: Insufficient number of files!!"
}||{ [ -e $1 -a -e $2 ]&& { #Judge whether the file exists #Judge whether the number of lines of two files are equal [ `sed -n '$=' $1` -eq `sed -n '$=' $2` ] &&{
                        USERADD $1 $2
                        #Call user created function}||{
                        echo "ERROR: The number of lines in the file is inconsistent!!"
                }
        }||{
                echo "ERROR: The file does not exist!!"
        }
}

2. Word Guessing Game

insert image description here

#!/bin/bash

Number=7
while true
do
       read -p "Please enter a number: " NUM
       if [ $NUM = $Number ]
       then
               echo "Congratulations, you guessed it right!!!"
               exit
       elif [ $NUM -lt $Number ]
       then
               echo -n "The number is too small!"
       elif [ $NUM -gt $Number ]
       then
               echo -n "The number is too large!"
       else
               echo -n "error"
       fi
done

3. Network card traffic monitoring

(1) Judgment module

insert image description here

(2) Display module

Use the clear command to clear the screen

insert image description here

The same line of output shows

insert image description here

(3) Script content

#!/bin/bash

[ -z $* ] && {
        echo "ERROR: Not input net device!"
}||{
        Device=`ifconfig $* 2> /dev/null`&&{
                while true
                do
                        RXpre=$(cat /proc/net/dev | grep $1 | tr : " " | awk '{print $2}')
                        TXpre=$(cat /proc/net/dev | grep $1 | tr : " " | awk '{print $10}')
                        # Get the traffic received and sent by the network port after 1 second sleep 1
                        RXnext=$(cat /proc/net/dev | grep $1 | tr : " " | awk '{print $2}')
                        TXnext=$(cat /proc/net/dev | grep $1 | tr : " " | awk '{print $10}')
                        # Get the actual inbound and outbound traffic in this 1 second RX=$((${RXnext}-${RXpre}))
                        TX=$((${TXnext}-${TXpre}))

                        TX=$(echo $TX | awk '{print $1/1024 " KB/s "}')
                        RX=$(echo $RX | awk '{print $1/1024 " KB/s "}')
					
					#Refresh the monitoring display on the same line #echo -ne "input: $RX \t\toutput: $TX "
                        #echo -ne "\r"
					clear # Use clear to clear the screen and then display echo -ne "input: $RX \noutput: $TX "					   
                done
        }||{
                echo "$1 is not exist"
        }
}

This is the end of this article about Linux user script creation/word guessing game/network card traffic monitoring. For more relevant Linux script guessing game monitoring content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  Vue encapsulation component tool $attrs, $listeners usage

>>:  Detailed basic operations on data tables in MySQL database

Recommend

Detailed introduction of Chrome developer tools-timeline

1. Overview Users expect the web applications the...

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

Advantages and disadvantages of Table layout and why it is not recommended

Disadvantages of Tables 1. Table takes up more byt...

XHTML three document type declarations

XHTML defines three document type declarations. T...

An article teaches you to write clean JavaScript code

Table of contents 1. Variables Use meaningful nam...

Summary of several replication methods for MySQL master-slave replication

Asynchronous replication MySQL replication is asy...

Simple use of Vue bus

Simple use of Vue bus Scenario description: Compo...

Simple implementation of html hiding scroll bar

1. HTML tags with attributes XML/HTML CodeCopy co...

JS array deduplication details

Table of contents 1 Test Cases 2 JS array dedupli...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

Discussion on the problem of garbled characters in iframe page parameters

I encountered a very unusual parameter garbled pro...

CSS3 mouse hover transition zoom effect

The following is a picture mouse hover zoom effec...

Linux kernel device driver character device driver notes

/******************** * Character device driver**...