Conditional judgment (if... Then) and netstat (Introduction) and function

Single layer, simple conditional judgment

if [ Conditional judgment ]; then
 When the conditional judgment is true,Work contents of instructions that can be carried out;
fi<==take if Reverse write,Become fi la!end if Meaning of!
  • &&Represents AND;
  • ||Representative or;
[ "${yn}" == "Y" -o "${yn}" == "y" ]
The above formula can be replaced by
[ "${yn}" == "Y" ] || [ "${yn}" == "y" ]
#!/bin/bash
# Program:
#		This program shows the user's choice
# History:
# 		2015/07/16	VBird	First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh, interrupt!"
exit 0
fi
echo "I don't know what your choice is" && exit 0

Multiple and complex conditional judgment

# A conditional judgment, divided into success and failure (else)
if [ Conditional judgment ]; then
 When the conditional judgment is true,Work contents of instructions that can be carried out;
else
 When the conditional judgment formula is not tenable,Work contents of instructions that can be carried out;
fi

If you consider more complex situations, you can use this syntax:

# Multiple conditional judgments (if... Elif... Elif... Else) are executed in different situations
if [ Conditional judgment formula I ]; then
 When conditional judgment formula 1 is true,Work contents of instructions that can be carried out;
elif [ Conditional judgment formula II ]; then
 When conditional judgment equation 2 is true,Work contents of instructions that can be carried out;
else
 When both conditional judgment equations 1 and 2 are not true,Work contents of instructions that can be carried out;
fi

his program shows the user's choice example

#!/bin/bash
# Program:
#	This program shows the user's choice
# History:
# 	2015/07/16	VBird	First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK, continue"
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what your choice is"
fi

In general, if you don't want users to enter additional information from the keyboard
You can use the parameter function ($1) mentioned in the previous section when using data! Let the user bring in the parameters when giving instructions!

Check $1 is equal to "hello" example

  1. Judge whether $1 is hello. If so, it will display "Hello, how are you?";
  2. If no parameters are added, the user will be prompted for the parameter release method that must be used;
  3. If the added parameter is not hello, remind the user that only hello can be used as the parameter.
#!/bin/bash
# Program:
#
Check $1 is equal to "hello"
# History:
# 2015/07/16
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
if [ "${1}" == "hello" ]; then
echo "Hello, how are you ?"
elif [ "${1}" == "" ]; then
echo "You MUST input parameters, ex> {${0} someword}"
else
echo "The only parameter is 'hello', ex> {${0} hello}"
fi

netstat

[dmtsai@study ~]$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 	0 	0 		0.0.0.0:22 		0.0.0.0:* 	LISTEN
tcp 	0	0 		127.0.0.1:25 	0.0.0.0:* 	LISTEN
tcp6	0 	0 		:::22 			:::* 		LISTEN
tcp6 	0 	0 		::1:25 			:::* 		LISTEN
udp 	0 	0 		0.0.0.0:123 	0.0.0.0:* 
udp 	0 	0 		0.0.0.0:5353 	0.0.0.0:* 
udp 	0 	0 		0.0.0.0:44326 	0.0.0.0:* 
udp 	0 	0 		127.0.0.1:323 	0.0.0.0:* 
udp6 	0 	0 		:::123 			:::* 
udp6 	0 	0 		::1:323 		:::*
#packet format  			 Local IP: Port 	 Remote IP: does the port listen

The key point above is the field "Local Address", which represents the network service started by the local machine! The IP part indicates that the service is located on that interface. If it is 127.0.0.1, it is only open to the local machine. If it is 0.0.0.0 or::: it means it is open to the whole Internet (for more information, please refer to the introduction of server setup). Each port has its own specific network service. Several common relationships between ports and related network services are:

  • 80: WWW
  • 22: ssh
  • 21: ftp
  • 25: mail
  • 111: RPC (remote procedure call)
  • 631: cups (print service function)

netstat example

If my host is interested in detecting the common ports 21, 22, 25 and 80, how can I detect whether my host has opened these four main network service ports through netstat? Since the keywords of each service are followed by the colon, they can be detected by retrieving similar words like: 80! Then I can simply write this program like this:

#!/bin/bash
# Program:
#		Using netstat and grep to detect WWW,SSH,FTP and Mail services.
# History:
# 2015/07/16
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. Just do some informing actions first~
echo "Now, I will detect your Linux server's services!"
echo -e "The www, ftp, ssh, and mail(smtp) will be detect! \n"
# 2. Start some tests and output some information!
testfile=/dev/shm/netstat_checking.txt
netstat -tuln > ${testfile} # First transfer the data to memory! You don't have to keep executing netstat
testing=$(grep ":80 " ${testfile}) # Detect to see if port 80 is there?
if [ "${testing}" != "" ]; then
echo "WWW is running in your system."
fi
testing=$(grep ":22 " ${testfile})
# Check to see if port 22 is there?
if [ "${testing}" != "" ]; then
echo "SSH is running in your system."
fi
testing=$(grep ":21 " ${testfile})
# Detect to see if port 21 is there?
if [ "${testing}" != "" ]; then
echo "FTP is running in your system."
fi
testing=$(grep ":25 " ${testfile})
# Check to see if port 25 is there?
if [ "${testing}" != "" ]; then
echo "Mail is running in your system."
fi

Birthday calculation example

case ... esac

case $Variable name in <==Key words are case ,And the money in front of the variable
 "First variable content") <==It is recommended to enclose the contents of each variable in double quotation marks,Keywords are parentheses )
 		Program segment
		;;		<==The end of each category is treated with two consecutive semicolons!
 "Second variable content")
		Program segment
		;;
	*)			<==The contents of the last variable will be used * To represent all other values
		Other program execution segments that do not contain the content of the first variable and the content of the second variable
		exit 1
		;;
 esac			<==Final case ending!『Reverse write』Think about it!

Direct release: hello example

#!/bin/bash
# Program:
#
Show "Hello" from $1.... by using case .... esac
# History:
# 2015/07/16
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case ${1} in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> {${0} someword}"
;;
*)
# In fact, it is equivalent to a wildcard, 0~ an infinite number of arbitrary characters!
echo "Usage ${0} {hello}"
;;
esac

Generally speaking, in the syntax of "case $variable in", there are roughly two ways to obtain the "$variable":

  • Direct release: for example, as mentioned above, use "script.sh variable" to directly give the content of the variable $1, which is also in / etc / init D directory, the design of most programs.
  • Interactive: let users input the contents of variables through the read command.

Interactive: show123 example

#!/bin/bash
# Program:
#
This script only accepts the flowing parameter: one, two or three.
# History:
# 2015/07/17
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
# read -p "Input your choice: " choice # Temporarily cancelled, can be replaced!
# case ${choice} in # Temporarily cancelled, can be replaced!
case ${1} in # Now use, you can replace with the above two lines!
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;"three")
echo "Your choice is THREE"
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac

function

function fname() {
Program segment
}

Because the execution mode of shell script is from top to bottom and from left to right, the function setting in shell script must be at the front of the program

#!/bin/bash
# Program:
#
Use function to repeat information.
# History:
# 2015/07/17
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo -n "Your choice is " # Add - n to continue to display on the same line
}
echo "This program will print your selection !"
case ${1} in
"one")
printit; echo ${1} | tr 'a-z' 'A-Z'
# Case conversion of parameters!
;;
"two")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"three")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac

Function also has built-in variables ~ its built-in variables are very similar to shell script. The function name represents $0, and the subsequent variables are also replaced by $1, $2... Which is easy to make mistakes here ~ because $0, $1... In "function fname() {program segment}" is different from $0 in shell script. Show 123-2 above For SH, if I say, "sh show123-2.sh one", it means that $1 in the shell script is the string "one". But $1 in print () has nothing to do with this one.

#!/bin/bash
# Program:
#
Use function to repeat information.
# History:
# 2015/07/17
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo "Your choice is ${1}"
# This $1 must refer to the following instructions
}
echo "This program will print your selection !"
case ${1} in
"one")
printit 1
# Note that the print instruction is followed by a parameter!
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac

In the above example, if you enter "sh show123-3.sh one", the word "Your choice is 1" will appear. Why is it 1? Because we wrote "print 1" in the program paragraph, that 1 will become $1 in the function. Oh ~ do you understand this? Function itself is actually a little more difficult, if you want to write other things. However, we just want to know more about shell script, so just take a look here

Added by yoost on Mon, 10 Jan 2022 22:22:45 +0200