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

Tutorial on installing mongodb under linux

MongoDB is cross-platform and can be installed on...

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

MySQL prepare principle detailed explanation

Benefits of Prepare The reason why Prepare SQL is...

Steps to customize icon in Vue

ant-design-vue customizes the use of Ali iconfont...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...

MySQL database index order by sorting detailed explanation

Table of contents The cause of the incident Anato...

Tomcat CentOS installation process diagram

Tomcat CentOS Installation This installation tuto...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

HTML meta usage examples

Example Usage Copy code The code is as follows: &l...

Summary of 7 pitfalls when using react

Table of contents 1. Component bloat 2. Change th...

Docker automated build Automated Build implementation process diagram

Automated build means using Docker Hub to connect...

How to customize more beautiful link prompt effect with CSS

Suggestion: Handwriting code as much as possible c...