How to write shell script? Try writing a simple script yourself

This is the back-end small class of the Xiuzhen Academy. Each shared article is from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[how to write shell script? Try to write a simple script yourself]

Hello, I'm the 2nd student of Shanghai Branch of it Academy. I'm an honest and kind java programmer.

Today, I'd like to share with you the relevant knowledge about scripts in the third Java task of the Academy.

background

Now most servers are Linux systems and need to be operated through the shell. Using shell script can greatly improve the efficiency of development and maintenance.

Knowledge analysis

What is a shell

Shell is a command-line interpreter. It provides users with an interface system level program that sends requests to the Linux kernel to run programs. Users can use the shell to start, suspend, stop and even write some programs.

Shell is also a powerful programming language, easy to write and debug. Shell is a script language for interpreting and executing. Linux commands can be called directly in the shell.

The two main syntax of the shell are Bourne and C, which are incompatible with each other. The Bourne family mainly includes sh, KSH, bash, PSH, Zsh, and the C family is mainly used in UNIX, including CSH and TSH The way to see the type of the current shell is: echo $shell

Concept discrimination, shell refers to an application that provides an interface through which users access the services of the operating system kernel.

Shell script is a script program written for shell. Shell in the industry usually refers to shell script.

The shell can be executed in two ways

Interactive: explain and execute the user's command. When the user enters a command, the shell executes one.

Batch processing: the user writes a shell script in advance, in which there are many commands. Let the shell execute these commands at one time without having to knock the commands one by one.

code

First, create a shell script file: VIM XXXX SH, suffix sh

There are two ways to execute scripts: 1 Grant execution permission, Chmod 755 XXXX sh 2. Execute the script bash Hello through bash call sh

Special symbols

$call variable value

`` backquotes, referencing system commands

$() reference system command

'' single quotation mark, in which the special symbol has no special meaning.

"" double quotation marks. The special symbols in them have no special meaning, except for three: $, ` `\

Pipe symbol

Command 1 | Command 2 the correct output of command 1 is the operation object of Command 2.

I / O redirection

Command > file to output the command to the specified file in an overwriting manner

Command > > file to output the command to the specified file by appending.

Output redirection

Command < file

Command < identifier

The following is a script for counting ngiinx response time in task 3:

#!/ bin/bash

#Nginx logs

H=100

cd /usr/local/Nginx/logs/

tail -n $H access. log|awk '{print $25,$26,$7 }'

Echo "counted $h data"

Echo "response time within 0.01 seconds"

tail -n $H access. log|awk 'BEGIN{sum=0}{if($26<0.01)sum++;} END{print sum}'

Echo "response time: 0.01 ~ 0.02 seconds"

tail -n $H access. log|awk 'BEGIN{sum=0}{if(($26>=0.01)&&($26<0.02))sum++;} END{print sum}'

Echo "response time: 0.02 ~ 0.03 seconds"

tail -n $H access. log|awk 'BEGIN{sum=0}{if(($26>=0.02)&&($26<0.03))sum++;} END{print sum}'

Echo "response time 0.03 ~ 0.04"

tail -n $H access. log|awk 'BEGIN{sum=0}{if(($26>=0.03)&&($26<0.04))sum++;} END{print sum}'

Echo "response time 0.04 and above"

tail -n $H access. log|awk 'BEGIN{sum=0}{if($26>=0.04)sum++;} END{print sum}'

Echo "traffic"

tail -n $H access. log|awk '{print $7}' | wc -l

tail -n $H access. log|awk '{print $11}' |sort | uniq -c |wc -l

Echo "########### statistics completed ####"

Author: Kelisi link: https://www.jianshu.com/p/413a266e0108 Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: nofollow "> https://www.jnshu.com , beginners switch to the gathering place of the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>