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

What are the considerations for Linux Shell script programming

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the matters needing attention in Linux Shell script programming". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Common skills

The code is as follows:

Ssh user@server bash < / path/to/local/script.sh

# ssh executes a script to the remote server. This command avoids uploading the script to the remote server.

Ssh user@host cat / path/to/remotefile | diff / path/to/localfile-

# compare the differences between remote files and local files

Vim scp://username@host//path/to/somefile

# vi a remote file

Curl ifconfig.me

# View public network ip under private network

Echo ${# a}

# take the number of characters in the variable

FUNCNAME

# function name variable, printing this variable will output the current function name

two。 Variable naming

1. Uniform rules before and after variable naming

The code is as follows:

COUNT=

Sum=

two。 Avoid meaningless characters or numbers, such as the following 18, who knows what it means

The code is as follows:

#-incorrect spelling-

If [[$count-gt 18]]

Then

Commmand

Fi

#-

#-correct writing-

Process_Limit=18

If [[$count-gt ${Process_Limit}]]

Then

Commmand

Fi

#-

3. Global variables are defined during script initialization, usually some configuration parameters, and use global variables as little as possible.

4. Local variables within a function are declared using local

The code is as follows:

Func_test_1 ()

{

Local test=1

Echo $test

}

5. Variable merging

If some variables need to be combined to make sense, such as the file path, assign the combined value to a variable so that it can be easily modified later.

The code is as follows:

Log_dir=/opt/log

Log_name=website.log

#-incorrect spelling-

If [[!-f ${log_dir} / mam/$ {log_name}]]

Then

Touch ${log_dir} / mam/$ {log_name}

Fi

#-

#-correct writing-

Log_file=$ {log_dir} / ${log_name}

If [[!-f ${log_file}]]

Then

Touch ${log_file}

Fi

#-

6. Abnormal judgment, judging whether the variable name exists or not, and for the input variable, it is also necessary to judge the legitimacy of the variable name.

The code is as follows:

#-incorrect spelling-

Rm-rf ${path} / *

#-

#-correct writing-

If [- d "${path}"]

Then

Rm-rf ${path} / *

Fi

#-

7. The use of double parentheses [[]]

The double square brackets of shell have many functions.

The code is as follows:

#-

Regular matching

If [[Yes = ~ Y | y]]

Then

Echo matched

Fi

#-

#-

To prevent null variables, the following if determines that an error will be reported if you use []

If [[$aaa = = 1]]

Then

Echo matched

Else

Echo "no such variable"

Fi

#-

8. Use ctrl+n to complete the variable name

If the naming of the variable name is long, manual input is easy to make mistakes, and copying is too troublesome, you can use ctrl+n to complete the variable name.

Operation can avoid variable name errors caused by manual input

three。 Temporary file

Avoid using temporary files as much as possible

If you need to use temporary files, add the script pid to the file name and clear the temporary files before the script exits

The mktemp command can be used to generate a temporary file

Tmp_file_name=$ (mktemp TMP.XXXXXX)

This command generates a file named TMP.XXXXXX (where XXXXXX is a 6-digit random code)

This avoids temporary file name conflicts when scripts are in parallel

four。 Code style

1. The first line declares the parser

The code is as follows:

#! / bin/bash

two。 The second line imports the environment variable

The code is as follows:

. ~ / .bash_profile (this is especially important in cron scripts)

3. Find the path of the script itself

Many scripts do not define a starting directory, which will cause the script to be executed only in the current directory

4. The style of conditional statement and loop statement is unified.

Use the shell uniform format instead of

The code is as follows:

#-incorrect spelling-

If [[]]; then

Command1

Fi

While [[]]; do

Command2

Done

#-

#-correct writing-

If [[]]

Then

Command1

Fi

While [[]]

Do

Command2

Done

#-

5. Indent, use TAB or spaces, do not mix the two

To check whether a technician is experienced, take a look at his electronic resume and see if TAB is mixed with spaces.

6. Add comment

Scripts without comments are destined to be difficult to maintain. Of course, we should also avoid some meaningless comments.

The code is as follows:

#-incorrect spelling-

# assign 3 to variable load_limit

Load_limit=3

#-

Brother, are you kidding me?

#-correct writing-

# define system load threshold

Load_limit=3

#-

five。 Interface programming

When multiple scripts need to coordinate their work, the corresponding interface should be defined according to the function of each module.

For example, if multiple scripts need to read the configuration of the same configuration file, then the requirement is formed into a separate script, and each script invokes the interface script.

So when you change the configuration file format, all you have to do is modify the interface.

This is the end of the content of "what are the considerations for Linux Shell script programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report