Linux Shell (2)

function

System function

basename

  • basic grammar

    basename [string / pathname] [suffix] Function description: The basename command will delete all prefixes including the last ('/') character, and then display the string

    basename can be understood as the file name in the path file

    Options:

    suffix is ​​the suffix. If suffix is ​​specified, basename will remove suffix from pathname or string.

  • Case practice

    Intercept the file name of the /home/guozihan/banzhang.txt path

    [guozihan@hadoop100 program]$ basename /home/guozihan/banzhang.txt
    banzhang.txt
    [guozihan@hadoop100 program]$ basename /home/guozihan/banzhang.txt .txt
    banzhang
    

dirname

  • basic grammar

    dirname file absolute pathFunction description: Remove the file name (non-directory part) from the given file name containing an absolute path, and then return the remaining path (directory part)

    dirname can be understood as the absolute path name of the file path

  • Case practice

    Get the path of banzhang.txt file

    [guozihan@hadoop100 program]$ dirname /home/guozihan/banzhang.txt
    /home/guozihan
    

Custom function

  • basic grammar

    [ function ] funname[()]
    {
          
          
    	Action;
    	[return int;]
    }
    
  • experience skills

    • The function must be declared before calling the function, and the shell script is run line by line. It will not be compiled first like other languages.
    • The function return value can only be obtained through the $? system variable. You can add: return to display. If not, the result of the last command will be used as the return value. return followed by the value n(0-255)
  • Case practice

    Calculate the sum of two input parameters

    [guozihan@hadoop100 program]$ touch fun.sh
    [guozihan@hadoop100 program]$ vim fun.sh 
    
    #!/bin/bash
    function sum()
    {
          
          
    s=0
    s=$[$1+$2]
    echo "$s"
    }
    read -p "Please input the number1: " n1;
    read -p "Please input the number2: " n2;
    sum $n1 $n2;
    
    [guozihan@hadoop100 program]$ chmod 777 fun.sh
    [guozihan@hadoop100 program]$ ./fun.sh 
    Please input the number1:2
    Please input the number2:5
    7
    

Getting Started with Regular Expressions

Regular expressions use a single string to describe and match a series of strings that conform to a certain grammar rule.. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.In Linux, text processing tools such as grep, sed, and awk all support pattern matching through regular expressions.

regular match

A regular expression that does not contain special characters matches itself

[guozihan@hadoop100 program]$ cat /etc/passwd | grep guozihan
guozihan:x:1000:1000:Guozihan:/home/guozihan:/bin/bash

will match all lines containing guozihan

Commonly used special characters

  • Special characters: ^

    ^ matches the beginning of a line

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep ^a
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    

    will match all lines starting with a

  • Special characters: $

    $ matches the end of a line

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep t$
    halt:x:7:0:halt:/sbin:/sbin/halt
    

    will match all lines ending with t

  • Special characters:.

    . matches any character

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep r..t
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    

    Will match all lines containing rabt, rbbt, rxdt, root, etc.

  • Special characters:*

    * is not used alone. It is used together with the previous character to match the previous character 0 or more times.

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep ro*t
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    

    Will match all lines such as rt, rot, root, rooot, roooot etc.

  • Character interval (square brackets): [ ]

    [ ] means match a character within a certain range

    [6,8]------match 6 or 8

    [0-9]------match a number from 0-9

    [0-9]*------matches any length of numeric string

    [az]------matches a character between az

    [az]* ------match any length of alphabetic string

    [ac, ef] - matches any character between ac or ef

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep r[a,b,c]*t
    operator:x:11:0:operator:/root:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    

    Will match all lines such as rt, rat, rbt, rabt, rbact, rabccbaaacbt, etc.

  • Special characters:\

    \ represents escape and will not be used alone. Since all special characters have their own specific matching patterns, we will encounter difficulties when we want to match a special character itself (for example, I want to find all lines containing '$'). at this timeWe need to use escape characters and special characters together to represent the special characters themselves

    [guozihan@hadoop100 program]$ cat /etc/passwd | grep 'a\$b'
    

    will match all lines containing a$b. Note that you need to use single quotes to surround the expression

text processing tools

cut

The job of cut is to "cut", specificallyResponsible for cutting data in the file. cut commandCut bytes, characters and fields from each line of the file and output these bytes, characters and fields

  • Basic usage

    cut [option parameter] filename

    Note: The default delimiter is tab

  • Option parameter description

    -f Column number, which column to extract +

    -d Delimiter, split columns according to the specified delimiter, the default is the tab character "\t"

    -c After cutting by characters, add n to indicate which column to take, such as -c 1

  • Case practice

    data preparation

    [guozihan@hadoop100 program]$ touch cut.txt
    [guozihan@hadoop100 program]$ vim cut.txt
    
    dong shen
    guan zhen
    wo wo
    lai lai
    le le
    

    Cut the first column of cut.txt

    [guozihan@hadoop100 program]$ cut -d " " -f 1 cut.txt
    dong
    guan
    wo
    lai
    le
    

    Cut the second and third columns of cut.txt

    [guozihan@hadoop100 program]$ cut -d " " -f 2,3 cut.txt
    shen
    zhen
    wo
    lai
    le
    

    Cut out guan in cut.txt file

    [guozihan@hadoop100 program]$ cat cut.txt | grep guan | cut -d " " -f 1
    guan
    

    Select the system PATH variable value, all paths starting with the second ":"

    [guozihan@hadoop100 program]$ echo $PATH
    /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/guozihan/.local/bin:/home/guozihan/bin
    [guozihan@hadoop100 program]$ echo $PATH | cut -d ":" -f 3-
    /usr/local/sbin:/usr/sbin:/home/guozihan/.local/bin:/home/guozihan/bin
    

    IP address printed after cutting ifconfig

    [guozihan@hadoop100 program]$ ifconfig ens33 | grep netmask | cut -d " " -f 10 
    192.168.182.100
    

awk

A powerful text analysis tool,Read the file line by line, slice each line using spaces as the default delimiter, and then analyze and process the cut parts.

  • Basic usage

    awk [option parameters] '/pattern1/{action1} /pattern2/{action2}…' filename

    Pattern: Indicates what awk is looking for in the data, which is the matching pattern.

    action: a series of commands executed when matching content is found

  • Option parameter description

    -F Specify input file delimiter

    -v Assign a value to a user-defined variable

  • Notice

    Only rows matching pattern will execute the action.

    [atguigu@hadoop101 shells]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
    user, shell
    root,/bin/bash
    bin,/sbin/nologin 。。。
    atguigu,/bin/bash
    dahaige,/bin/zuishuai
    

    BEGIN is executed before all rows of data are read; END is executed after all data is executed.

  • awk's built-in variables

    FILENAME file name

    NR Number of records read (Line number

    NF The number of domains in the browsing record (After cutting, the number of columns

  • Practical examples

    Query the line number of the blank line in the ifconfig command output result

    [guozihan@hadoop100 program]$ ifconfig | awk '/^$/{print NR}'
    9
    18
    25
    

    Cut IP

    [guozihan@hadoop100 program]$ ifconfig ens33 | awk '/netmask/ {print $2}'
    192.168.182.100
    

Comprehensive application cases

archive file

In actual production applications, important data often need to be archived and backed up.

Requirements: Implement a script that archives backups of a specified directory every day, enter a directory name (without / at the end), archive and save all files in the directory by day, append the archive date to the archive file name, and place it in /root/ archive.

The archive command is used here: tar

You can add the -c option afterwards to indicate archiving, and the -z option to indicate simultaneous compression. The resulting file suffix is ​​.tar.gz.

#!/bin/bash
# 首先判断输入参数个数是否为 1
if [ $# -ne 1 ]
then
echo "参数个数错误!应该输入一个参数,作为归档目录名"
exit
fi
# 从参数中获取目录名称
# 判断目录是否存在
if [ -d $1 ]
then
echo
else
echo
echo "目录不存在!"
echo
exit
fi
DIR_NAME=$(basename $1)
DIR_PATH=$(cd $(dirname $1); pwd)
# 获取当前日期
DATE=$(date +%y%m%d)
# 定义生成的归档文件名称
FILE=archive_${DIR_NAME}_$DATE.tar.gz
DEST=/root/archive/$FILE
# 开始归档目录文件
echo "开始归档..."
echo
tar -czf $DEST $DIR_PATH/$DIR_NAME
if [ $? -eq 0 ]
then
echo
echo "归档成功!"
echo "归档文件为:$DEST"
echo
else
echo "归档出现问题!"
echo
fi
exit

Send a message

We can use the mesg and write tools that come with Linux to send messages to other users

Requirement: Implement a script to quickly send a message to a user. Enter the user name as the first parameter, followed directly by the message to be sent. The script needs to detect whether the user is logged in to the system, whether the messaging function is turned on, and whether the currently sent message is empty.

#!/bin/bash
login_user=$(who | grep -i -m 1 $1 | awk '{print $1}')
# [ -z $变量] :判断变量是否为空
if [ -z $login_user ]
then
echo "$1 不在线!"
echo "脚本退出.."
exit
fi
is_allowed=$(who -T | grep -i -m 1 $1 | awk '{print $2}')
# 查看用户是否开启消息功能
# 判is_allowed是否为+
if [ $is_allowed != "+" ]
then
echo "$1 没有开启消息功能"
echo "脚本退出.."
exit
fi
# 确认有消息发送
if [ -z $2 ]
then
echo "没有消息发出"
echo "脚本退出.."
exit
fi
# 从参数中获取要发送的消息
whole_msg=$(echo $* | cut -d " " -f 2- )
# 获取用户登录的终端
user_terminal=$(who | grep -i -m 1 $1 | awk '{print $2}')
# 写入要发送的数据
echo $whole_msg | write $login_user $user_terminal
# 确认发送结果
if [ $? != 0 ]
then
echo "发送失败!"
else
echo "发送成功!"
fi
exit

Guess you like

Origin blog.csdn.net/pipihan21/article/details/132592944