Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Knowledge of linux shell script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

In a recent project, there was a task of writing linux shell scripts. As I was not very familiar with it before, I encountered a lot of difficulties in this process, found a lot of information, and gained a lot. Next, Xiao Yun Network will share a summary of the knowledge commonly used in linux shell scripts.

1 basic grammar

1.1 variable

To use a defined variable, simply precede the variable name with a dollar sign

Your_name= "qinjx"

Echo $your_name

Echo ${your_name}

1.2 string

Strings can be in single quotation marks, double quotation marks or without quotation marks.

Single quotation mark

Any character in the single quote will be output as is, and the variable in the single quote string is invalid.

Single quotation marks cannot appear in a single quote string (not even after using escape characters for single quotation marks).

Double quotation marks

There can be variables in double quotation marks

Escape characters can appear in double quotation marks

1.3 transfer parameters

$# indicates the number of parameters passed in to execute the script

$* indicates that all parameters passed to the script are displayed as a single string.

In the case of "$*" enclosed by "$", with "$1 $2". Outputs all parameters in the form of $n ".

$indicates the current process ID number under which the script is running

$@ is the same as $*, but use it in quotation marks and return each parameter in quotation marks.

In the case of "$@" surrounded by ", output all parameters in the form of" $1 "" $2 "…" $n ".

$0 indicates the name of the execution script

$1 indicates the first parameter

$? Displays the exit status of the last command. 0 indicates that there is no error, and any other value indicates an error.

1.4 basic operator

Native bash does not support simple mathematical operations, but it can be done through other commands, such as awk and expr,expr.

Expr is an expression evaluation tool that can be used to evaluate expressions.

Val=expr 2 + 2

The conditional expression should be placed between square brackets and with a space, for example: [$axixifolds roomb] is wrong and must be written as [$axiomatic brackets].

A backslash () must be added before the multiplication sign (*) to realize the multiplication operation.

If...then...fi is a conditional statement, which will be explained later.

The expr syntax for shell in MAC is $((expression)), where the "*" in the expression does not need to escape the symbol "".

2 commonly used commands

2.1printf command

The syntax of the printf command:

Printf format-string [arguments...]

Printf "%-10s%-8s%-4s\ n" name, gender, weight kg

S, c, d, f are all format substitutes

%-10s means a width of 10 characters (- for left alignment, none for right alignment), any character will be displayed in a 10 character width character, if it is insufficient, it will be automatically filled with spaces, and the excess will also display all the content.

%-4.2f refers to formatted as a decimal, where .2 means to retain 2 decimal places.

2.2test command

The test command is used to check whether a condition is true, and it can be tested in terms of values, characters, and files.

File expression

-e filename true if filename exists

-d filename true if filename is a directory

-f filename true if filename is a regular file

-L filename true if filename is a symbolic link

-r filename true if filename is readable

-w filename true if filename is writable

-x filename true if filename is executable

-s filename true if the file length is not 0

-h filename true if the file is a soft link

Filename1-nt filename2 true if filename1 is newer than filename2.

Filename1-ot filename2 true if filename1 is older than filename2.

String variable expression

If [$a = $b] true if string1 is equal to string2 (the string allows you to use the assignment sign as the equal sign)

If [$string1! = $string2] true if string1 is not equal to string2

If [- n $string] if string is not empty (non-0), return 0 (true)

If [- z $string] True if string is empty

If [$sting] returns 0 if string is not empty (similar to-n)

2.3read command

-n (no line wrapping)-p (prompt statement)-n (number of characters)-t (waiting time)-s (not echoed)

Such as:

Read-p "Enter your name:" name

3 compression and decompression

Decompress: tar-zxvf FileName.tar

Compression: tar-czvf FileName.tar DirName

3.1 five independent commands

Compression and decompression all need to use one of them, can be used with other commands, but only one of them.

-c: create compressed files

-x: decompression

-t: view content

-r: appends files to the end of the compressed archive file

-u: update the files in the original package

3.2 five optional parameters

Optional when compressing or decompressing files as needed.

-z: with gzip attribute

-j: with bz2 attribute

-Z: with compress attribute

-v: show all processes

-O: unlock files to standard output

3.3 one required parameter

-f: use the file name. Remember that this parameter is the last parameter and can only be followed by the file name.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report