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

Native js to achieve accordion effect

In actual web page development, accordions also a...

Do you know how to use the flash wmode attribute in web pages?

When doing web development, you may encounter the...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

How to declare a cursor in mysql

How to declare a cursor in mysql: 1. Declare vari...

MYSQL unlock and lock table introduction

MySQL Lock Overview Compared with other databases...

Summary of MySQL basic common commands

Table of contents MySQL basic common commands 1. ...

How to optimize MySQL deduplication operation to the extreme

Table of contents 1. Clever use of indexes and va...

How to achieve 3D dynamic text effect with three.js

Preface Hello everyone, this is the CSS wizard - ...

Detailed explanation of Cgroup, the core principle of Docker

The powerful tool cgroup in the kernel can not on...

Optimized implementation of count() for large MySQL tables

The following is my judgment based on the data st...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

Hyperlink icon specifications: improve article readability

1. What is the hyperlink icon specification ?<...

Vue uses v-model to encapsulate the entire process of el-pagination components

Use v-model to bind the paging information object...

Example code for implementing bottom alignment in multiple ways with CSS

Due to the company's business requirements, t...