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

Some considerations for shell scripts

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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

Check the public network ip under the 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

one。 Variable naming

1. Uniform rules before and after variable naming

COUNT=

Sum=

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

-misspelled-

If [[$count-gt 18]]

Then

Commmand

Fi

-

-write it correctly-

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

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.

Log_dir=/opt/log

Log_name=website.log

-misspelled-

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

Then

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

Fi

-

-write it correctly-

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.

-misspelled-

Rm-rf ${path} / *

-

-write it correctly-

If [- d "${path}"]

Then

Rm-rf ${path} / *

Fi

-

7. The use of double parentheses ``

The double square brackets of shell have many functions.

-

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

two。 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

three。 Code style

1. The first line declares the parser

#! / bin/bash

two。 The second line imports the environment variable

. ~ / .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

-misspelled-

If ``; then

Command1

Fi

While ``; do

Command2

Done

-

-write it correctly-

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.

-misspelled-

# assign 3 to variable load_limit

Load_limit=3

-

Brother, are you kidding me?

-write it correctly-

# define system load threshold

Load_limit=3

-

four。 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.

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

Internet Technology

Wechat

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

12
Report