Share 9 Linux Shell Scripting Tips for Practice and Interviews

Share 9 Linux Shell Scripting Tips for Practice and Interviews

Precautions

1) Add interpreter at the beginning: #!/bin/bash

2) Use four spaces for syntax indentation and add more comments.

3) Naming suggestion rules: variable names are capitalized, local variables are lowercase, function names are lowercase, and the names should reflect their actual functions.

4) By default, variables are global. In a function, the variable local is specified as a local variable to avoid polluting other scopes.

5) There are two commands that can help me debug the script: set -e exits the script when a non-zero result is encountered, and set -x prints the execution process.

6) When writing a script, be sure to test it first before putting it into production.

1 Get a random string or number

Get a random 8-bit string:

Get a random 8-digit number:

cksum: print CRC checksum statistics bytes

2 Define a color output string function

The function keyword defines a function and can be added or not.

3. Create users in batches

4 Check if the package is installed

5 Check service status

6 Check host survival status

Method 1: Put the wrong IP into an array to determine whether the ping fails three times

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

Method 3: Use the for loop to exit the loop if the ping succeeds. If it does not exit, it will print "ping failed"

7. Monitor CPU, memory and disk utilization

1) CPU

Use the vmstat tool to analyze CPU statistics.

2) Memory

3) Hard disk

8 Batch host disk utilization monitoring

Prerequisite: SSH non-interactive login or key login is used for the monitoring and monitored ends.

Write a configuration file to save the SSH connection information of the monitored host. The file content format is: IP User Port

9. Check website availability

1) Check URL availability

2) Determine the availability of the URL three times

The idea is the same as checking the host survival status above.

Summarize

The above is what I introduced to you about sharing 9 practical and commonly used Shell scripts for interviews. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • 25 Linux Shell Script Interview Questions Collection
  • Summary of several classic interview questions of C language under Linux (sharing)
  • Detailed discussion of Linux netstat command (essential for senior interviews)
  • C language fork function process operation in Linux and related interview questions explanation
  • Compilation of must-read books for linux c++ server-side development interviews
  • Summary of the 10 most frequently asked questions in Linux interviews

<<:  WeChat Mini Program implements the likes service

>>:  Detailed explanation of several ways to write private variables of ES6 implementation class

Recommend

CSS uses calc() to obtain the current visible screen height

First, let's take a look at the relative leng...

Solution to Chinese garbled characters when operating MySQL database in CMD

I searched on Baidu. . Some people say to use the...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...

An article teaches you how to implement a recipe system with React

Table of contents 1. Recipe Collection 1.1 Projec...

In html table, set different colors and widths for each cell

It is recommended that you do not set the width, h...

Detailed explanation of MySQL Truncate usage

Table of contents MySQL Truncate usage 1. Truncat...

A brief discussion on how to write beautiful conditional expressions in JS

Table of contents Multiple conditional statements...

How to modify Flash SWF files in web pages

I think this is a problem that many people have en...

Tips to prevent others from saving as my web page and copying my site

Nowadays, copying websites is very common on the I...

Website design should pay attention to the sense of color hierarchy

Recently I have been saying that design needs to h...

Nginx configuration and compatibility with HTTP implementation code analysis

Generate SSL Key and CSR file using OpenSSL To co...

How to operate MySQL database with ORM model framework

What is ORM? ORM stands for Object Relational Map...

Detailed explanation of computed properties in Vue

Table of contents Interpolation Expressions metho...

Introduction to the properties of B-Tree

B-tree is a common data structure. Along with him...

Web Design Tutorial (4): About Materials and Expressions

<br />Previous Web Design Tutorial: Web Desi...