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

CSS performance optimization - detailed explanation of will-change usage

will-change tells the browser what changes will h...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

jQuery realizes the sliding effect of drop-down menu

When we make a web page, sometimes we want to hav...

Javascript destructuring assignment details

Table of contents 1. Array deconstruction 2. Obje...

What you need to know about msyql transaction isolation

What is a transaction? A transaction is a logical...

Calling the search engine in the page takes Baidu as an example

Today, it suddenly occurred to me that it would be...

Detailed explanation of Angular component projection

Table of contents Overview 1. Simple Example 1. U...

Docker-compose quickly builds steps for Docker private warehouse

Create docker-compose.yml and fill in the followi...

Simple implementation of vue drag and drop

This article mainly introduces the simple impleme...

Detailed explanation of how to configure Nginx web server sample code

Overview Today we will mainly share how to config...

How to use video.js in vue to play m3u8 format videos

Table of contents 1. Installation 2. Introducing ...