[grep profile : ]grep is used to filter lines that contain specific charactersusage: grep ' pattern ' fileFor example: grep ' BBB ' aaa.txt--color # Search for the keyword BBB from the file Aaa.txt and highlight it.[Regular meta-character :
I. define functions
CopyCodeThe Code is as follows: function fname ()
{
Statements;
}
OrCopy codeThe Code is as follows: fname (){Statements;}
Ii. Call Functions
You can call a function by using the function name:$ Fname; # execute a
I am also a newbie. I am a little old-fashioned. I am a good trainer. It is a reference for beginners.
Copy codeThe Code is as follows :#! /Bin/bash
If [$ succ_ping-gt $ (ping_count-1)]; then
If [-f./alarm_file/alarm _ $ host];
*-B returns true if the file exists and is a block file
*-C returns true if the file exists and is a character file
*-D returns true if pathname exists and is a directory
*-E returns true if the file or directory specified by pathname exists.
The color of the text terminal can be generated using an ANSI unconventional character sequence. Example:
Echo-e "\ 033 [44; 37; 5 m ME \ 033 [0 m COOL"
The above command sets the background to blue, foreground white, blinking cursor, output
Ctrl + a switch to the command line
Ctrl + e switch to the end of the command line
Ctrl + l clear screen content
Ctrl + u clear the content before cutting the cursor
Ctrl + k cut the content after clearing the cursor
Ctrl + y copy the content
When writing a Shell filter process, most people use grep to search for the specified process in the output result of ps aux. However, the grep process is also displayed, for example, the pptpd process, two matching items are displayed:
Copy
1. Use pgrep to match the name
Copy codeThe Code is as follows: if test $ (pgrep-f $1 | wc-l)-eq 0
Then
Echo "the process does not exist"
Else
Echo "process"
Fi
The following is a supplement:Processes in the current system:
Apple @ ubuntu :~ $
First script file
Copy codeThe Code is as follows :#! /Bin/bash
Echo "This is my first bash code! "
Exit 0
Redirection symbols and mathematical computationCopy codeThe Code is as follows :#! /Bin/bashEcho-n "The time and date are :"DateValue1 = 100 #
Two very detailed shell instances
General programming steps
Now let's discuss the general steps for writing a script. Any excellent script should have help and input parameters. And write a pseudo script (framework. sh) that contains the framework
Variable
Get local host name# Hostname
Obtain the System Version Number# Uname-r
Obtain CPU type
Obtain the cpu operation frequencyCat/proc/cpuinfo
Obtain memory usage# Free-m
Obtain hard disk usage# Df
Variable types and variable operations
File Type:
-F: determines whether the file is a common file.-D: determines whether the directory is used.
-B: determines whether a device file is a block.-C: determines whether a device file is a dual-character device file.-S: Determine whether the
I. Requirements
It is often difficult to enter a password for different linux machines, such as ssh Login or scp file copying. You can generate a public key through ssh-keygen, establish a trusted channel between the two machines.II. Environment
I. Requirements
It is often difficult to enter a password for different linux machines, such as ssh Login or scp file copying. You can generate a public key through ssh-keygen, establish a trusted channel between the two machines.II. Environment
Location parameters can be usedShift command shifts left. For exampleShift 3 indicates the original$4 now becomes$1, the original$5 now becomes$2, etc. The original$1,$2,$3 discard,$0 is not moved. Without ParametersThe shift command is
8.1 count disk usage
1. Disk Space is a limited resource.
2 df and du are important commands used in Linux to measure disk usage. df is short for disk free and du is short for disk usage.
3. Find the disk space occupied by a file (or multiple files):
#! /Bin/bashNAME = javaNOP =/bin/trueDATE = $ (/bin/date + "% Y-% m-% d % H: % M: % S ") ps-ef | awk-F "" '{print $8, $2}' | grep ^ java>/dev/null 2> & 1 case "$? "In0) # It is running in this case so we do nothing. echo "$ NAME is running OK. relax.
Assume that the same Command needs to be executed on multiple nodes (considering the uncertainty of the command). Generally, the for loop is used and the command can be directly called through ssh. For example, you need to view the root directory
In some scenarios with high security requirements, the ssh timeout period is preset by the Administrator. After a period of time, the ssh connection is automatically disconnected. In this case, if you execute the script through ssh and the script
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service