Linux Miscellaneous Notes (Part 1)

personal work record

1. Use the linux command to get the ip address of the computer:

#!/bin/sh
   ifconfig eth0 | awk -F "[ ]+" '/ inet /{print $3}'

Note:
AWK is a language for manipulating text files and is a powerful text analysis tool.
awk -F specifies the input file fold separator
[ ]+ this is a regular expression, + means one or more, here means one or more spaces or colons

2. Find the process number of the specified program and kill the process

#!/bin/sh
echo "==============重启推流服务包server/streamServer/frpsServer============"
ps aux|grep streamServer |awk -F "[ ]+" '/root /{print "kill -9 "$2}'|sh

Note:
Find the execution process PID and execute sh

Another way:

#!/bin/sh
ps aux|grep frps |awk -F "[ ]+" '/root /{print $2}'|xargs kill -9 

Note:
pass the obtained process ID as a parameter to kill -9
3. Get the keyboard input and pass the obtained value as a parameter

echo "请输入你的名字:\n"
read name
printf "=========================获取到的名字为:%s==========================\n" $name
echo "输入一个数字:"
read number1
printf "再输入一个数:"
read number2
echo $number1 $number2
if [ $number1 -eq $number2 ];
then
        printf "================================\n"
fi
printf "两个数的计算结果为:%d\n" $(($number1+$number2))

Comments:
read information from an input device

4. Usage of if...elif...else...

#!/bin/sh
SYSTEM=`uname -s` #获取操作系统类型,进行赋值操作
if [ $SYSTEM = "Linux" ] ; then #条件必须使用[]包含起来 前后一定要有空格,并且后面必须有分号;
        echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then #条件前后一定要有空格
        echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
        echo "Solaris" #字符串的赋值一定不能有空格
else
        echo "What?"
fi

Note: Different from other languages, the comparison does not use greater than or less than signs, but uses characters with different meanings for different types to represent
1 String judgment
[ str1 = str2 ] When two strings have the same content and length, it is true
[ str1 != str2 ] True if the strings str1 and str2 are not equal
[ -n str1 ] True if the length of the string is greater than 0 (the string is not empty)
[ -z str1 ] True if the length of the string is 0 (the empty string )
[ str1 ] True when string str1 is not empty
2 Judgment of numbers
[ int1 -eq int2 ] True if two numbers are equal
[ int1 -ne int2 ] True if two numbers are not equal
[ int1 -gt int2 ] True if int1 is greater than int2 true
[ int1 -ge int2 ] true if int1 is greater than or equal to int2
[ int1 -lt int2 ] true if int1 is less than int2
[ int1 -le int2 ] true if int1 is less than or equal to int2

Guess you like

Origin blog.csdn.net/qq_40267002/article/details/111167272