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

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

How to use bind to set up DNS server

DNS (Domain Name Server) is a server that convert...

How to introduce pictures more elegantly in Vue pages

Table of contents Error demonstration By computed...

Summary of naming conventions for HTML and CSS

CSS naming rules header: header Content: content/c...

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

VScode Remote SSH remote editing and debugging code

The latest Insider version of Visual Studio Code ...

JavaScript determines whether the browser is IE

As a front-end developer, I can’t avoid IE’s pitf...