Share 9 common Linux Shell scripts used in practice and interviews

  • 2020-12-10 01:03:15
  • OfStack

Matters needing attention

1) Start with interpreter: #! /bin/bash

2) Syntax indenting, using 4 Spaces; Add more comments.

3) Suggested naming rules: variable name uppercase, local variable lowercase, function name lowercase, name reflects the actual function.

4) The default variable is global. In the function, the variable local is specified as a local variable to avoid contaminating other scopes.

5) There are two commands that can help me debug the script: ES14en-ES15en exits the script when execution is not 0, and ES16en-ES17en prints the execution process.

6) Script 1 must be tested before production.

Gets a random string or number

Get a random 8-bit string:

Obtain a random 8-digit number:

cksum: Prints CRC validation and statistics bytes

2 defines a color output string function

The function keyword defines 1 function, with or without addition.

3. Batch create users

Check whether the software package is installed

Check the service status

6. Check the host survival status

Method 1: Put the error IP into an array to see if ping failed 3 times

Method 2: Put the number of errors in the FAIL_COUNT variable to determine if ping failed three times

Method 3: Use the for loop to break out of the ping loop and continue. If you do not break out, you will go to print ping and fail

Monitor CPU, memory and hard disk utilization

1) CPU

Use the vmstat tool to analyze CPU statistics.

2) memory

3) the hard disk

Batch host disk utilization monitoring

The monitored side and monitored side SSH are free of interactive login or key login.

Write a configuration file to save the connection information of the monitored host SSH in the format of IP User Port

Check the usability of the site

1) Check URL availability

2) Judge the availability of URL for 3 times

The idea is the same as the above check the host survival state 1.

conclusion


Related articles: