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
Back up website content
Copy codeThe Code is as follows :#! /Bin/bash
# Specify the running script shell
# Run the script to grant the user the execution permission
Bakdir =/backup
Month = 'date + % m'
Day = 'date + % d'
Year = 'date + % y'
Back up database shell script 1
Copy codeThe Code is as follows :#! /Bin/bash
# Specify the running script shell
# Run the script to grant the user the execution permission
Bakdir =/backup
Month = 'date + % m'
Day = 'date + % d'
Year = 'date + %
3.1 search for textGrep: use the basic regular expression (BRE) defined by POSIX ).Egrep: use an extended regular expression (ERE ).Fgrep: Quick grep. Use an optimized algorithm to match a fixed string instead of a regular expression.
1992 POSIX
1. define the shell function (define function)
Syntax:[Function] funname [()]{Action;[Return int;]}Note:1. It can be defined with function fun () or fun () without any parameters.2. If the parameter is returned, the value "+: return" is displayed.
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];
Copy the files in the bill folder to the bill1 and bill2 folders according to certain rules (file name parity.Update the progress tag of bill1 and bill2 (Update file. max)File3 is the label of the latest file under the bill directory. It is updated
Copy codeThe Code is as follows :#! /Bin/bash
#********************************#
# Run fun in the concurrent background #
# For wanggy 2012-01-25 #
# Note :#
# Fun_num number of times the fun function runs in the background #
# SQL _num SQL
Copy codeThe Code is as follows :#! /Bin/bash
# Telno is used to query the number list file
# Sendsms. log is the text message sending result File
#1 indicates that the message is successfully sent.
Query_sms ()
{
> Submit_telno
>
1. Character truncation:
For normal path truncation, you can use the basename and dirname tools:Basename can intercept a file name from a file path.
For example:
Copy codeThe Code is as follows: $ basename/home/file.tar
File.tar
Dirname can be
*-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.
Binary comparison operator, which compares variables or numbers. Note the difference between numbers and strings.
Integer comparison
-Eq equals to, for example, if ["$ a"-eq "$ B"]-Ne is not equal to, for example, if ["$ a"-ne "$ B"]-Gt is greater
For example, to require scripts to only access a certain resource in sequence, such as disk files, you can refer to the following implementation.
Copy codeThe Code is as follows :#! /Bin/bash
#
# File locking using bash.
# Ver 0.1.6
#
# Author:
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
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 #
1. Example of an error method
A)
Var = 1 + 1
Echo $ var
The output result is 1 + 1. Tragedy.
B)Var = 1Var = $ var + 1Echo $ varThe output result is 1 + 1, which is still tragic.
2. Correct Method1) Use letVar = 1Let "var + = 1"Echo $ varThe output
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
The scripts for this lesson are also shared in share.
Day 4: cyclic structure
Content outline:
Application Instance Analysis
For Loop
While Loop
Principle of for Loop
Achieve results
According to yesterday's menu modification. After selecting 1, 2,
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
The following are Shell Study Notes for 1-2 days:
--------- I am a separator ---------
Hardware is executed, and operations are performed between the kernel and hardware.
Command parser.
Shell script Components
System commands
Text processing
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.