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

Embed player in web page embed element autostart false invalid

Recently, I encountered the need to embed a player...

How to configure Java environment variables in Linux system

Configure Java environment variables Here, the en...

How to Easily Remove Source Installed Packages in Linux

Step 1: Install Stow In this example, we are usin...

MySQL index coverage example analysis

This article describes MySQL index coverage with ...

Detailed tutorial on installing ElasticSearch 6.x in docker

First, pull the image (or just create a container...

MySQL quick recovery solution based on time point

The reason for writing such an article is that on...

How a select statement is executed in MySQL

Table of contents 1. Analyzing MySQL from a macro...

Solve the problem of case sensitivity of Linux+Apache server URL

I encountered a problem today. When entering the ...

JavaScript custom calendar effect

This article shares the specific code of JavaScri...

Installation steps of mysql under linux

1. Download the mysql tar file: https://dev.mysql...

JavaScript implements page scrolling animation

Table of contents Create a layout Add CSS styles ...

Teach you how to deploy Vue project with Docker

1.Write in front: As a lightweight virtualization...