Linux--shell Script

Source: Internet
Author: User

Here is a summary of some of the recent learning Shell script knowledge points

Blog Park-Bang Bang sauce Good * * *

1. Introduction
The shell is a text interface that lets us communicate with the system. The shell script is the one written for the shell.
It's like a batch file in the early Dos era. bat, so shell script can be seen as a program or a batch script that leverages the shell and related tool directives, does not need to be compiled to execute, can help the system administrator to quickly manage the host.

Shell script is a good tool for system management, but it is not good enough to handle a large number of numerical operations, because the shell scripts slower and uses more CPU resources, resulting in poor allocation of host resources

2. Simple Syntax
test.sh
#! /bin/bash #宣告这个文档内的语法使用bash的语法, when the program is executed, load bash's related configuration file
# Beginning with # is an annotation
# then the script: 1. Content and features 2. Version Information 3. Author and contact 4. Date of filing 5. History, etc.
Echo-e "Hello world! \a \ n "
Exit 0 #中断程序 and return a value to the system

3. Talk script: Variable content is user-determined
#!/bin/bash
Read-p "Please enter your firstname:" FirstName
Read-p "Please enter your LastName:" LastName
Echo-e "\nyour full name is: $firstname $lastname"

4. How scripts are executed
(1) using Sh/bash to execute scripts: executing in subroutines
-When the subroutine is complete, the variables or actions within the subroutine will end without passing back to the parent program
(2) Use source/. To execute the script: Execute in the parent program
--The script executes in the parent program, and the variables or actions remain in the parent program
View file permissions: Ls-l test.sh
If you do not have permissions, you can give execution permissions like this: chmod a+x test.sh
(To get the root account: su root)

5. Test function with the tests command
(1) file type judgment
Test [-EFD] File
-E: Whether the file name exists
-F: Whether the file name exists and is file type
-D: Whether the file name exists and is of type diretory

(2) The judgment of the document permission
Test [-rwx] File
-R: Whether the file name exists and has a readable permission
-W: Whether the file name exists and has writable permissions
-X: Whether the file name exists and has executable permissions
--->test-e/dmtsal && echo "Exist" | | echo "not exist"

(3) Comparison of two documents
Test File1-nt File2
-nt: Judging whether file1 newer than file2
-ot: Judging whether file1 older than file2
-EF: Determine if two documents point to the same inode

(4) A comparison of two integers
Test N1-eq N2
-eq:equal
-ne:not equal
-gt:greater than
-lt:less than
-ge:greater than or equal
-le:less than or equal

(5) The judgment of the string
Test-z string:string is empty
Test-n string:string is not a null
Test str1 = str2:str1 is equal to STR2
Test STR1! = Str2:str1 is not equal to STR2

(6) Multiple conditional judgments
Test-r filename-a-x filename
-a:and, two cases are set up at the same time
-o:or, one of two cases was established
!: Inverting status, such as test! -x file, which returns True when file does not have X

6. Use judgment in accordance with []
(1) such as [-Z "$HOME"];echo $?
Note: Use [] to determine that each component inside the brackets is delimited by a space character, and the variable or constant is enclosed in double quotation marks:
["$HOME" = = "$MAIL"]
(2) brackets are commonly used in conditional judgments.
["$yn" = = "Y"-o "$yn" = = "Y"] && echo "ok,continue" && exit 0
Can also be written as: ["$yn" = = "Y"] | | ["$yn" = = "Y"]

default variable for 7.shell script ($0,$1)
FileName opt1 opt2 opt3 opt4
Executed script filename for this variable, OP1 corresponds to $1,opt2 corresponding to the $ ...
In addition, there are the following special variables:
$#: Represents the number of arguments to be followed
[Email protected]: representative ["$" "$" "$" "$" "$4"]
$*: Representative ["$ $ $4"]

8.shift: Offset of parameter variable number
VI test.sh S1 S2 S3 S4 S5 S6
echo "6 variables in front of shift"
Shift
echo "Shift after a total of 5 variables, namely S2 S3 S4 S5 S6"
Shift 3
echo "Shift after a total of 2 variables, namely S5 S6"

9. Conditional judgment (if there are spaces on both sides of the equal sign, you must [(space) condition (space)])
If [conditional judgment]; Then
Command content when conditions are set
Fi #结束if语句

10. Duplicate condition judgment
(1)
If [conditional judgment type one]; Then
Command content when conditions are set
Else
Command content when the condition is not established
Fi #结束if语句

(2)
If [conditional judgment type one]; Then
The content of the order when the condition is established
elif [conditional judgment type II]; Then
Order content when condition two is established
Else
Condition one or two no immediate command content
Fi #结束if语句

10. Using case...in ... Esac Judgment
Case $variablename in
"First-variable-content")
Operation
;;
"Second-variable-content")
Operation
;;
*)
Operation
;;
Esac

11 function function
Because shell script is executed from top to bottom, from left to right, the function must be set at the front of the program so that it can be found at execution time.
function fname () {
Program Segment
}
Such as:
function PrintIt () {
echo "Your choice is $"
}

PrintIt 2
---> Results: Your choice is 2
(In this case, the arguments followed by the function call are followed by the call script:./test.sh para1 is not the same)


12. Cycle
While [conditional-judging]
Do
Program Segment
Done
When the condition is established, it loops until the conditions are not established.

or
until [condition]
Do
Program Segment
Done
When the condition is established, it terminates the cycle, otherwise it continues to cycle

or
For Var in con1 con2 Con3 ...
Do
Program Segment
Done
Such as:
network= "192.168.1"
For Sitenu in $ (seq 1) #seq为sequence连续的缩写
Do
Ping-c 1-w 1 ${network}.${sitenu} &>/dev/null && result=0|result=1
if ["result" = = 0];then
echo "Server ${network}.${sitenu} is up"
Else
echo "Server ${network}.${sitenu} is down"
Fi
Done

or
Numerical processing of For...do...done
for (initial value; limit value; execution step))
Do
Program Segment
Done
Such as:
S=0
For ((i=1;i<100;i=i+1))
Do
s=$ (($s + $i))
Done

13.shell script tracking and debug
SH [-NVX] scripts.sh
-N: Do not execute scripts, only query syntax issues
-V: Output The contents of the script before executing the script
-X: Displays the contents of the script used to the screen

Linux--shell Script

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.