Chapter 9 functions

9.1 System Functions

1. Basename Basic syntax

Basename [string/pathname] [suffix] (Basename deletes all prefixes including the last (‘/’) character, and displays the string.

Options:

Suffix is the suffix, and if suffix is specified, Basename drops suffix from pathName or String.

2. A case in field

(1) Intercept the file name of the /home/aowin/banzhang. TXT path

touch:wq

  1. Dirname basic syntax

Dirname Specifies the absolute path of a file. Dirname specifies the absolute path of a file. Dirname specifies the absolute path of a file.

4. A case in field

(1) Obtain the path of the banzhang. TXT file

9.2 User-defined Functions

1. The basic grammar

[ function ] funname[()]

{

Action;

[return int;]

}

funname

2. skills

(1) You must declare functions before calling them. Shell scripts are run line by line. Does not compile first like other languages.

(2) The function returns a value only via $? If not, the result of the last command run will be used as the return value. Return followed by the number n(0-255)

3. A case in field

(1) Calculate the sum of the two input parameters

$ touch fun.sh

$ vim fun.sh

#! /bin/bash
function sum()

{

  s=0

  s=$[ The $1 + $2 ]

  echo "$s"

}

read -p "Please input the number1: " n1;

read -p "Please input the number2: " n2;

sum $n1 $n2;
Copy the code

$ chmod 777 fun.sh

$ ./fun.sh

Chapter 10 Shell Tools (Emphasis)

10.1 the cut

The job of cut is to “cut”, specifically to cut data in a file. The cut command cuts bytes, characters, and fields from each line of the file and outputs these bytes, characters, and fields.

1. Basic usage

Cut [option parameter] filename

Note: The default delimiter is a TAB character

2. Parameter Description

Table 1-55

Option parameters function
-f Column number, which column to extract
-d Delimiter that splits the column by the specified delimiter
-c Specifies a specific character

3. Case practice

(0) Data preparation

$ touch cut.txt

$ vim cut.txt

dong shen

guan zhen

wo wo

lai lai

le le
Copy the code

(1) Cut the first column cut. TXT

$ cut -d ” ” -f 1 cut.txt

(2) Cut. TXT the second and third columns

$cut -d “” -f 2,3 cut. TXT

(3) Cut guan in the cut-.txt file

$ cat cut.txt | grep “guan” | cut -d ” ” -f 1

(4) Select the value of the system PATH variable. All paths after the second “:” start:

$ echo $PATH

$ echo $PATH | cut -d : -f 2-

(5) IP address printed after cutting ifConfig

$ ifconfig eth0 | grep “inet addr” | cut -d: -f 2 | cut -d” ” -f1

10.2 the sed

Sed is a stream editor that processes content one line at a time. When processing, the current line is stored in a temporary buffer, called “pattern space”. The contents of the buffer are then processed with the sed command, and the contents of the buffer are sent to the screen after processing. Proceed to the next line and repeat until you reach the end of the file. The contents of the file do not change unless you store the output using redirection.

  1. Basic usage

Sed [option parameter] ‘command’ filename

  1. Options Parameter Description

Table 1-56

Option parameters function
-e Sed action editing is performed directly in command column mode.
-i Edit file directly
  1. Function Description

Table 1-57

The command Functional description
a New, a can be followed by a string, appear on the next line
d delete
s Find and replace
  1. A case in field

(1) Data preparation

$ touch sed.txt

$ vim sed.txt

dong shen

guan zhen

wo wo

lai lai

le le
Copy the code

(2) Insert “mei nv” into the second line of sed. TXT and print.

$ sed ‘2a mei nv’ sed.txt

$ cat sed.txt

Note: The file has not changed

(3) delete all lines in sed. TXT containing wo

$ sed ‘/wo/d’ sed.txt

(4) Replace wo with ni in sed. TXT

$ sed ‘s/wo/ni/g’ sed.txt

Note: ‘g’ stands for global, replace all

(5) Delete the second line in sed. TXT and replace wo with ni

$ sed -e ‘2d’ -e ‘s/wo/ni/g’ sed.txt

10.3 the awk

A powerful text analysis tool that reads a file line by line, slicing each line with a space as the default separator, and dissecting the sections.

  1. Basic usage

Awk [options parameter] ‘Pattern1 {action1} pattern2{action2}… ‘the filename

Pattern: Indicates the content that AWK looks for in the data

Action: Series of commands to execute when a match is found

  1. Options Parameter Description

Table 1-55

Option parameters function
-F Specifies the input file fold delimiter
-v Assigns a user-defined variable
  1. A case in field

(1) Data preparation

$ sudo cp /etc/passwd ./

(2) Search for all lines in the passwd file that start with the root keyword and print the seventh column of the line.

$ awk -F: ‘/^root/{print $7}’ passwd

(3) Search for all lines starting with root in the passwd file and output the first and seventh columns of the line separated by commas (,).

$ awk -F: ‘/^root/{print
1 , 1″,”
7}’ passwd

Note: Only rows that match pattern will be executed

(4) Display only the first and seventh columns of /etc/passwd, separated by commas, and add the column name user in front of all lines. Shell add “dahaige, /bin/zuishuai” in the last line.

$ awk -F : ‘BEGIN{print “user, shell”} {print
1 , 1″,”
7} END{print “dahaige,/bin/zuishuai”}’ passwd

Note: BEGIN is executed before all rows of data are read; END is executed after all data has been executed.

(5) Add the user ID in the passwd file by 1 and output the output

$ awk -v i=1 -F: ‘{print $3+i}’ passwd

  1. Built-in variables of AWK

Table 1-56

variable instructions
FILENAME The file name
NR Number of read records
NF Number of fields viewed (after cutting, number of columns)
  1. A case in field

(1) Collect the file name, line number, and column number of the passwd line

$ awk -F: ‘{print “filename:” FILENAME “, linenumber:” NR “,columns:” NF}’ passwd

(2) Cut IP addresses

$ ifconfig enp0s3 | grep “inet ” | awk -F ” ” ‘{print $2}’

(3) Query the number of the hollow line in sed. TXT

$ awk ‘/^$/{print NR}’ sed.txt

10.4 the sort

The sort command is very useful in Linux. It sorts files and sorts the results to standard output.

  1. The basic grammar

Sort (option)(parameter)

Table 1-57

options instructions
-n Sort by numerical size
-r Sort in reverse order
-t Sets the separator character to use when sorting
-k Specify the columns to be sorted

Parameter: Specifies the list of files to sort

  1. A case in field

(1) Data preparation

$ touch sort.sh

$ vim sort.sh

Bb: 40:5. 4 bd: pass. 2 xz: perfection. 3 CLS: now. The 5 ss: spake. 6Copy the code

(2) Sort in reverse order according to the third column after “:” segmentation.

$ sort -t : -nrk 3 sort.sh