shell lessons learned in Operation and Maintenance

Xu Liang Linux from the public No.

write

  1. Beginning of the script should be part of the script function description, parameters description, author name, creation / modification date, version information, in the format:

  2. When writing the script, note aligned format, such as loops or all statements before and after the sentence is determined alignment, and selecting the complete case

  3. The beginning of the implementation of the script, execute the following command, if encountered during execution using undefined variables or command returns a non-zero value, it will direct error Exit:

  4. Recommended that each parameter of the command line in single quotes, double quotes, especially rm, mv, etc. may result in the modification of existing operations on the production data, it is recommended to use the trash Policy: rm operating turn means mv operation, setting documents save directory to prevent rollback, and regular cleaning:

  5. You need to use the command line parameter ' ' '? 'Wildcard, should be based on the most accurate matching principle, can be determined as a file, directory name prefixes, suffixes, and other recognizable extension keywords, the information shall be contained in a parameter, such as to determine the file, directory length should use'? 'Wildcard may not be used' 'Recommended use:

  6. After the assignment to the numerical variables, means to ensure the required values ​​of the numerical variables, avoiding abnormal occurrence in a subsequent process
#!/bin/bash
count=`cat test.log|wc -l`
isNumber $count
isNumber()
{
if [ "$1" -eq "$count" ];then 
echo " $1 is number"
else
echo "$1 isn't a number"
exit 1
fi
}
  1. Variables used in determination conditions, must be enclosed in double quotes, such as:
if [ "$i" -lt "$max" ]
then
    break
fi
if [ -f "$conf" ]
then
    break
fi

  1. When a backup file is packaged, it must be packaged using a relative path, such as:

  2. Files needed for compressing the package, pipeline processing is recommended, such as:

  3. When you use the ps command screening process, such as to determine the process belongs to the user, the user name must be specified in the parameters, such as its output as the kill command input, you must specify the process belongs to the user, such as:
    kill -9ps -fu $ USERNAME| grep java |grep AdminServer

Write error-prone class shell

  1. Update files>, NA cp
    use> modify files and rollback file, to retain ownership and permission of the source file, to avoid the rights belong to the group to be modified when using cp, but also can be use cp -pr original rights reserved and is a group

  2. Confirm before using the kill command.
    -W use an exact match keyword.
    before and after the kill are kept on-site, twice ps -ef | grep -w keyword | grep -v grep >> / tmp / kill_ process name .backup
    before removing the need to confirm the check, get the process ID is unique, to avoid manslaughter

  3. Before using rm confirm
    backup before removal delete an object information, avoid using variables directly using the file and directory names;
    if must be used, before deletion, it is recommended to check to avoid accidental deletion, delete the directory and file information retention:

    try not to use find to traverse the root directory to find, it is necessary to confirm former colleague delete, delete avoid multiple accidental deletion.

  4. for loop in conditions press the space bar to distinguish, avoid entering incorrect or infinite loop

  5. while loop taboo, if you want to use a loop variable, do not use while combined pipeline.

  6. Caution cp, this statement is basically correct, but again there are problems of space segmentation. So it should be enclosed in double quotes:
    cp "$ File" "$ target"

  7. Cd caution
    to avoid using cd to the directory and then manipulate the operation, may go in the directory failed, accidentally deleted, such as:

  8. use ... to replace[ ]

  9. Do not operate the pipeline at the same time to read and write files
  10. cd fallibility problem

Application category



Guess you like

Origin www.cnblogs.com/0916m/p/11617269.html