Linux script commonly used commands

Need to use Linux scripts, refer to some blogs and e-books, and organize some commands for reference only!

yum: Fedora/RedHat/SUSE is
based on PRM package management, which can automatically download and install PRM packages from the specified server, automatically handle dependencies, and install all dependent packages at once, without the need for tedious downloads and installations. Provides commands to find, install, delete a certain, a group, or even all packages.
yum[options][command][package…]
options: optional, options include -h (help), -y (when the installation process prompts to select all "yes"), -q (do not display the installation process), etc. .
command: The operation to be performed.
package: The name of the installed package.

Tips:
1) The Linux chmod (full English: change mode) command is a command to control the user's file permissions.
2) Linux/Unix file calling permissions are divided into three levels: file owner (Owner), user group (Group) , Other Users (Other Users).
3) #! is an agreed mark, which tells the system which interpreter the script needs to execute, that is: which kind of Shell is used
4) Add "$" before the output variable.
5) "{}" helps the interpreter identify the boundaries of variables.
6) Command parameters can only be combined between short formats.
7) Add an ampersand at the end when executing the command, the command will enter the system background for execution.
8) Files beginning with a dot (.) in Linux represent hidden files, and most of them are system service files.
9) "echo $?" displays the return value after the last command is executed.
10) read is used to read user input information, and can assign the received user information to the following specified variables. -p parameter
11) is used to send the user Show certain prompt information to users.
"Read -p "please inout a number:" grade"

12) There is a device file of /dev/zero in Linux, which does not occupy system storage space, but can provide endless data.
13) $RANDOM can be adjusted to take out a random value (0~32767), use expr Get its result

Linux variables:
read-only variables
Use the readonly command to define variables as read-only variables, and the value of read-only variables cannot be changed.
The following example tries to change the read-only variable, and the result is an error:

#!/bin/bash
myUrl="https://www.google.com"
readonly myUrl
myUrl="https://www.runoob.com"
Run the script and the result is as follows:
/bin/sh: NAME: This variable is read only.

Deleting variables
Use the unset command to delete variables. Syntax:
unset variable_name
variable cannot be used again after being deleted. The unset command cannot delete read-only variables.
Example
#!/bin/sh
myUrl="https://www.runoob.com"
unset myUrl
echo $myUrl

There will be no output when the above example is executed.
Note: Only valid once, you can continue to assign values ​​to the variable

Variable types When
running the shell, there are three types of variables:

  1. Local variables Local variables are defined in scripts or commands and are only valid in the current shell instance.
    Programs started by other shells cannot access local variables.
  2. Environment Variables All programs, including those started by the shell, can access environment variables.
    Some programs need environment variables to ensure their normal operation. Shell scripts can also define environment variables when necessary.
  3. Shell variables Shell variables are special variables set by the shell program. Some of the shell variables are environment variables,
    and some are local variables. These variables ensure the normal operation of the shell

Single quotation mark:
Limitation of single quotation mark string:
Any character in single quotation mark will be output as-is, variables in single quotation mark string are invalid;
single quotation mark cannot appear in single quotation mark string (use escape for single quotation mark) After the character), but it can appear in pairs and used as a string concatenation.

双引号
your_name=‘runoob’
str=“Hello, I know you are “$your_name”! \n”
echo -e $str

The output result is:
Hello, I know you are “runoob”!

Advantages of double quotation marks:
1. Variables can be in
double quotation marks 2. Escape characters can appear in double quotation marks

Shell array
bash supports one-dimensional arrays (multi-dimensional arrays are not supported), and there is no limit to the size of the array.
In Shell, parentheses are used to denote arrays, and array elements are separated by "space" symbols.
Use the @ symbol to get all the elements in the array
echo array [@] Get the length of the array len = {array[@]} Get the length of the array len=A R & lt R & lt A Y [ @ ] is eligible to take the number of sets of length of L E n-={#array[@]}

Receive user parameters
shell to pass parameters, the format of the script to get the parameters is: $n.
n represents a number, 1 is the first parameter of the execution script, 2 is the second parameter of the execution script, and so on...

#!/bin/bash
echo "Shell passes parameter examples!";
echo "File name executed: $0";
echo "The first parameter is: $1";
echo "The second parameter is: $2";
echo "No. The three parameters are: $3";

$ sh test.sh 1 2 3
Example of Shell passing parameters!
File name executed: test.sh The
first parameter is: 1 The
second parameter is: 2 The
third parameter is: 3

$# The number of parameters passed to the script * Display all the parameters passed to the script
as a single string. For example, "* displays all the parameters passed to the script as a single string. For example,"* In a th single- word character string significantly shown that there is the foot of this transfer delivery of the parameter number . If " *" is enclosed in """, all parameters are output in the form of "$1 $2… $n".
$$ The ID number of the current process running the script
$! TheID numberof the last process running in the background
@ and @ and@ And * the same, but the use of quotation marks, and returns each parameter in quotation marks. Such as "$@" enclosed in """, "$1" "2"… "2"…"2"All parameters are output in the form of " n".
$- Display the current options used by the Shell, which has the same function as the set command.
$? Display the exit status of the last command. 0 means no error, any other value means there is an error.

Judge the user's parameters
Test statement: [Conditional expression]
File test statement: Use specified conditions to determine whether the file exists or whether the permissions are satisfied, etc.
-d/-e/-f/-r/-w/-x

Logical test statement: Perform a logical analysis of the test results, and different effects can be achieved according to the test results.
AND (&&): indicates that the following commands will be executed after the current command is executed successfully
or (||): indicate that the current command execution fails Will execute his subsequent commands
Non (!): Invert the judgment result of the condition test

Integer comparison operator: (-eq/-ne/-gt/-lt/-le/-ge)
String comparison: (=/!=/-z (judge whether the string is empty))

if conditional statement

if conditional test statement
then command sequence 1
else command sequence 2
fi

for conditional loop statement

for variable name in value list
do
command sequence
done

while conditional loop

while conditional test operation
do
command operation
done

case variable value in
mode 1)
command sequence 1
;;
mode 2)
command sequence 2
;;

#Other command mode

*) The
default command sequence
esac

Common commands

The ps command
PS is used to view the process status in the system:
-a displays all processes (including processes of other users)
-u users and other detailed information
-x displays processes that do not control the terminal

There are five process states
R (running) in Linux : the process is running or waiting in the run queue.
S (interrupt): the process is in sleep, and it leaves this state when a certain condition is formed or when a signal is received.
D (Uninterruptible): The process does not respond to system asynchronous signals, and it cannot be interrupted even with the kill command.
Z (Zombie): The process has been terminated, but the process descriptor still exists, until the parent process calls the wait4() system function to release the process.
T (stop): the process stops running after receiving a stop signal.

The top command
top is used to dynamically monitor information such as process activity and system load, which is equivalent to a Windows task manager

The pidof/kill/killall command
pidof is used to query the PID value of a specified service process
kill is used to terminate the service process of a specified PID
killall is used to terminate all processes corresponding to a service of a specified name

The ifconfig command is used to obtain information such as network card configuration and network status. The
uname command is used to view the system kernel and system version information.
Uptime is used to view system load information. The
free command is used to display the memory usage information in the current system.
Who is used to view the current login host the user terminal information
last for viewing all system login recorded
history command history executed for display (default display 1000 record command, you can customize the / etc variable values HISTSIZE / profile file to modify)
sosreport command with To collect system configuration and architecture information and output diagnostic documents

The cat command is used to view plain text files (less content)
more is used to view plain text files (more content)
head command is used to view the first N lines of a plain text document
tail command is used to view the last N lines of a plain text document The
most powerful function of the tail command is that it can continuously refresh the content of a file. It is especially useful when you want to view the latest log file in real time. The command format is "tail -f file name"

The tr command is used to replace characters in a text file, the format is "tr [original character] [target character]

The wc command is used to count the number of lines, words, and bytes of the specified text (-l/-w/-c) The
stat command is used to view the specific storage information and time of the file. The
cut command is used to extract text characters by column" cut [parameter] The text "
Search by column, not only use the -f parameter to set the number of columns to see, but also use the -d parameter to set the interval symbol
eg "cut -d: -f1 /etc/passwd"

The diff command is used to compare the differences between multiple text files, the format is "diff[parameter] file"
diff --brief determines whether the two files are the same
diff -c compares the differences between multiple files in detail

The touch command is used to create a blank file or set the time of the file "touch [option] [file]" (-a read time -m modify time -d both)
First use the ls command to view the modification time of a file, and then modify this File, and finally set the modified file time to the time before modification through the touch command

The mkdir command is used to create a blank directory (combined with the -p parameter to recursively create a file directory with nested cascading relationships)

The cp command is used to copy files or directories "cp [options] source file target file"
mv command is used to cut or rename files

The dd command is used to copy files or convert files according to the specified size and number of
data blocks. It allows the user to copy the content of the file according to the specified size and number of data blocks, and can also convert the data during the copy process .

if input file name
of output file name
bs set the size of each "block"
count set the number of "blocks" to be copied
eg: "dd if=/dev/zero of=560_file count=1 bs=400M"

The tar command is used to pack, compress or decompress
files -c create a compressed file
-x unzip the compressed file
-t view which files are in the compressed package
-z compress or decompress with Gzip
-j compress or decompress with bzip2
-v show compression or The process of decompression
-f target file name
-p retain the original permissions and attributes
-P use absolute path to compress
-C specify the decompressed directory
eg "tar -czvf etc.tar.gz /etc" "tar xzvf etc.tar. gz -C /root/etc"

The grep command is used to perform a keyword search in the text and display the matching results

The find command is used to find files according to specified conditions. It can use different file characteristics as search conditions (file name, size, modification time, permissions, etc.)

Input redirection is to import the file into the command, and output redirection is to write the data information originally to be output to the screen into the specified file.
The command <file uses the file as the standard input of the
command. The command << delimiter reads from the standard input and stops the
command until the delimiter is encountered. <file 1> file 2 uses file 1 as the standard input of the command and outputs the standard to the file 2

Command> File redirects standard output to a file (clear the original file data)
Command 2> File redirects error output to a file (clear original file data)
Command >> File redirects standard output to In a file (append to the end of the original content)
Command 2>> File redirect the error output to a file (append to the end of the original content)
Command >> File 2>&1/Command&>> File standard Redirect output and error output to a file (append to the original content)

Pipeline command symbol : Regard the standard normal data originally output to the screen by the previous command as the standard input of the next command.
Wildcard: a general symbol for matching information: * represents matching zero or more characters,? Represents matching a single character.
Escape character :
backslash: makes a variable after the backslash a simple string.
Single quote: escapes all variables in it as a simple string.
Double quotes: retains the variable attributes, and does not Perform escape processing
Backquote: return the result after executing the command

The awk command
awk to read the file line by line, cut each line with a space as the default separator, and perform various analysis and processing on the cut part
awk'{pattern + action }'{filename}
pattern means AWK is in the data The search content, action is a series of commands executed when matching content is found.
Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a specific pattern. The pattern is the regular expression to be expressed, enclosed in slashes.
The most basic function of the awk language is to browse and extract information based on specified rules in files or strings. Only after awk extracts the information can other text operations be performed. A complete awk script is usually used to format the information in a text file.
Usually, awk is a processing unit of a file. Awk receives a line of the file, and then executes the corresponding command to process the text.
The awk workflow is like this: read in a record separated by a line break with'\n', and then divide the record into fields according to the specified field separator, fill the fields, $0 means all fields, 1 means the first field, 1 Represents the first domain,1 Table shows first a th field , n represents the n-th field. The default field separator is "blank key" or "[tab] key"

Syntax format:
Format 1: Pre-command | awk [Option]'Condition {Edit command}'
Format 2: awk [Option]'Condition {Edit command}' File...
If the editing command contains multiple sentences, you can use a semicolon Separate. When processing text, if no separator is specified, spaces, tabs, etc. will be used as separators by default. print is the most common command.
Commonly used command options
-F: Specify the delimiter, which can be omitted (default space or Tab bit)
-V: Call external Shell variable variable

awk provides both print and printf functions for printing output.
The parameters of the print function can be variables, numbers or strings. Strings must be quoted in double quotes, and parameters are separated by commas. If there is no comma, the parameters are concatenated and cannot be distinguished. Here, the role of the comma is the same as that of the separator of the output file, except that the latter is a space.
The printf function, its usage is basically similar to printf in the C language, it can format strings, and when the output is complex, printf is easier to use and the code is easier to understand.

Array
Because the subscript of an array in awk can be numbers and letters, the subscript of an array is usually called a key. Values ​​and keywords are stored in an internal table that applies hash to key/value. Since hashes are not stored sequentially, you will find that they are not displayed in the order you expected when displaying the contents of the array. Arrays, like variables, are automatically created when they are used, and awk will also automatically determine whether it stores numbers or strings. Generally speaking, arrays in awk are used to collect information from records, and can be used to calculate sums, count words, and track the number of times a template is matched, etc.

awk programming

显示/etc/passwd的账户
awk -F ‘:’ ‘BEGIN {count=0;} {name[count] = $1;count++;}; END{for (i = 0; i < NR; i++) print i, name[i]}’ /etc/passwd
0 root
1 daemon
2 bin
3 sys
4 sync
5 games

Variables and assignments
In addition to awk's built-in variables, awk can also customize variables.

The following counts the number of accounts in /etc/passwd
awk'{count++;print $0;} END{print "user count is ", count}' /etc/passwd
root❌0:0:root:/root:/bin/bash

user count is 40
count is a custom variable. There was only one print in the previous action{}. In fact, print is just one statement, and action{} can have multiple statements, separated by ;.

The count is not initialized here. Although the default is 0, it is appropriate to initialize it to 0:
awk'BEGIN {count=0;print "[start]user count is ", count} {count=count+1;print $0;} END{print "[end]user count is ", count}' /etc/passwd
[start]user count is 0
root❌0:0:root:/root:/bin/bash

[end]user count is 40

Count the number of bytes occupied by files in a folder
ls -l |awk'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ", size}'
[end] size is 8657198

If it is displayed in M ​​units:
ls -l |awk'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ", size/1024/1024,"M"}'
[ end]size is 8.25889 M
Note that the statistics do not include the subdirectories of the folder.

Awk built-in variable
ARGC number of command line parameters
ARGV command line parameter arrangement
ENVIRON supports system environment variables in the queue using
FILENAME awk to browse the file name
FNR browse the number of file records
FS set the input field separator, equivalent to the command line -F option
NF
Number of domains for browsing records NR Number of records read
OFS Output domain separator
ORS Output record separator
RS Control record separator

The chmod command controls user permissions on files.
Only file owners and super users can modify file or directory permissions. Absolute mode/symbol mode can be used to specify file permissions.
Syntax: chmod [-cfvR] [–help] [–version] mode file...
mode: permission setting characters, the format is: [ugoa...][[±=][rwxX]...][,...]
where:

u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。
+ 表示增加权限、- 表示取消权限、= 表示唯一设定权限,设置指定用户权限的设置,即将用户类型的所有权限重新设置 。
r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该文件是个子目录或者该文件已经被设定过为可执行。

Other parameter description:

-c : 若该文件权限确实已经更改,才显示其更改动作
-f : 若该文件权限无法被更改也不要显示错误讯息
-v : 显示权限变更的详细资料
-R : 对目前目录下的所有文件与子目录进行相同的权限变更(即以递归的方式逐个变更)
--help : 显示辅助说明
--version : 显示版本

Use symbol mode to set multiple items: who (user type), operator (operator) and permission (permission), the settings of each item can be separated by commas.
The command chmod will modify the file access authority of the user type specified by who. The user type is indicated by one or more letters in the position of who, as shown in the symbol pattern table of who:

dos2unix is a practical command to convert Windows format files to Unix and Linux formats. The newline character for Windows format files is \r\n, and the newline character for Unix&Linux files is \n. The dos2unix command actually converts \r\n in the file to \n.
unix2dos is a command that is a twin of dos2unix. It is a command to convert Linux&Unix format files to Windows format files.

Guess you like

Origin blog.csdn.net/liulanba/article/details/112194441