Considerations for Linux shell script programming _linux Shell

Source: Internet
Author: User
Tags ssh

First, the common skills

Copy Code code as follows:
SSH User@server Bash </path/to/local/script.sh
#ssh到远程服务器执行一个脚本, this command avoids uploading scripts to a remote server

SSH User@host Cat/path/to/remotefile | Diff/path/to/localfile-
#比较远程文件和本地文件的差异

Vim Scp://username@host//path/to/somefile
#vi一个远程文件

Curl Ifconfig.me
#在内网下查看公网ip

echo ${#a}
#取变量字符个数

FUNCNAME
#函数名变量, printing the variable outputs the current function name

Two. Variable naming

1. Variable naming before and after rule unification

Copy Code code as follows:
Count=
sum=

2. Avoid meaningless characters or numbers, such as the following 18, who knows what it means?
Copy Code code as follows:

#-------------------the wrong wording-------------------
if [[$count-GT 18]]
Then
Commmand
Fi
#----------------------------------------------

#-------------------the right wording-------------------
Process_limit=18
if [[$count-GT ${process_limit}]]
Then
Commmand
Fi
#----------------------------------------------

3. Global variables are defined when the script is initialized, typically with some configuration parameters, with minimal use of global variables
4. Local variables within a function use the locals declaration
Copy Code code 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 file path, please assign a combination of values to a variable so that it is easier to modify later
Copy Code code as follows:

Log_dir=/opt/log
Log_name=website.log
#-------------------the wrong wording-------------------
if [[!-f ${log_dir}/mam/${log_name}]]
Then
Touch ${log_dir}/mam/${log_name}
Fi
#----------------------------------------------

#-------------------the right wording-------------------
Log_file=${log_dir}/${log_name}
if [[!-f ${log_file}]]
Then
Touch ${log_file}
Fi
#----------------------------------------------

6. Abnormal judgment, judge the existence of variable names, for input variables, but also to determine the legality of variable names
Copy Code code as follows:

#-------------------the wrong wording-------------------
RM-RF ${path}/*
#----------------------------------------------


#-------------------the right wording-------------------
If [-D "${path}"]
Then
RM-RF ${path}/*
Fi
#----------------------------------------------

7. Use of double brackets [[]]
The double middle bracket of the shell has many functions
Copy Code code as follows:

#----------------------------------------------
Regular match
if [[Yes =~ Y|y]]
Then
Echo matched
Fi
#----------------------------------------------

#----------------------------------------------
To prevent null variables, if you use [], the following if is an error
if [[$AAA = 1]]
Then
Echo matched
Else
echo "No such variable"
Fi
#----------------------------------------------

8. Use CTRL + N to complement variable names
If the name of the variable is relatively long, hand input error, duplication is too cumbersome, you can use CTRL + N to complement the variable name
Operation can avoid the wrong variable name caused by manual input

Three. Temporary documents

Try to avoid using temporary files
If you need to use temporary files, add the script PID to the file name and clear the temporary file 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 with a file name of Tmp.xxxxxx (where XXXXXX is a 6-bit random code)
This avoids the conflict of the temporary file name when the script is parallel

Four. Code style

1. The first line affirms the parser

Copy Code code as follows:

#!/bin/bash

2. Import environment variables in the second row
Copy Code code as follows:

. ~/.bash_profile (This is particularly important in cron scripts)

3. Find the script's own path
Many scripts do not define the start directory, which causes the script to execute only in the current directory

4. Conditional statement and circular statement style unification
Use the shell uniform format, not;

Copy Code code as follows:

#-------------------the wrong wording-------------------
if [[]]; then
Command1
Fi
while [[]];d o
Command2
Done
#----------------------------------------------

#-------------------the right wording-------------------
if [[]]
Then
Command1
Fi

while [[]]
Todo
Command2
Done
#----------------------------------------------

5. Indent, use tab or space, do not mix
Want to check if a technician is experienced, then look at his electronic resume bar, whether the tab and the space mixed

6. Add comments
Script without annotations, destined to be difficult to maintain, of course, we should also avoid some meaningless comments

Copy Code code as follows:

#-------------------the wrong wording-------------------
#将3赋值给变量load_limit
Load_limit=3
#----------------------------------------------
Brother, are you kidding me?

#-------------------the right wording-------------------
#定义系统负载阀值
Load_limit=3
#----------------------------------------------

Five. Interface programming

When multiple scripts need to be coordinated, the corresponding interfaces should be defined according to the capabilities of their respective modules
For example, multiple scripts need to read the configuration of the same configuration file, then the requirement is formed into a separate script, and each script calls this interface script
So when you modify the configuration file format, just modify the interface.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.