Shell script syntax

Source: Internet
Author: User

Shell script:
#! /Bin/Bash must pass the first line #! To specify the Current Shell

I. variables:
"": Allows you to reference values of other variables using the $ symbol.
'': Other variables cannot be referenced. $ is considered as a normal character.
'': Assigns the command execution result to the variable.

1. input variables:
Read-P "input prompt:"
$ A # Call and DISPLAY variable

2. Custom Variables
A = "Hello World"
Export a # Set A as a global variable, suitable for all shells supported by the current environment
Echo $ A # display Variables
Unset A # Clear Variables
A = "Haha: $ A" # append a string to
Expr $ A + $ B # Calculate integer variables using expr

3. System numeric variables:
#$0 ~ $9 is a digital variable defined by the system. $0 indicates the current process name.

4. predefined variables:
$: # Current process ID
$ #:# Number of location parameters in the command line
$? : # Status returned after the previous command is executed: 0 indicates normal, non-0 indicates exception
$ *: # Parameters of all locations
$! : # The last process number running in the background

 

Application:
A. # Number of users logged on to the query system. If there are too many users, the system is overloaded.
['Who | WC-l'-le 10] & Echo "yes" the WC parameter is short for word count.

B. # An alarm is triggered when the space usage of the Search/boot partition exceeds the rated size.
# Awk is used to extract information data from files or strings according to certain rules,
# The complete awk script is used to format text file information
A = 'df-HT | grep "/Boot" | awk '{print $6}' | cut-d "%"-F 1'
[$ A-GT 95] & Echo "/the disk capacity occupied by the boot directory is too large"

# Determine whether the input information on the keyboard is equal to the information in the system
Read-P "Location:" mylocation
[$ Mylocation = "mylocation"] & Echo "yes"

# Check whether MySQL is running. If a prompt is displayed, start MySQL.
Service mysqld Status &>/dev/null
If [$? -EQ 0]
Then "MySQL server is running"
Else/etc/init. d/mysqld restart
Fi

# Add 20 system users in batches with the following accounts: "user1", "user2",... "user20"
I = 1
While [$ I-le 20]
Do
Useradd user $ I
Echo "123456" | password -- stdin user $ I &>/dev/null
I = 'expr $ I + 1'
Done

# Calculate the sum of several numbers
Result = 0
While [$ #-GT 0]
Do
Result expr $ reesult + $1
Shift
Done
Echo "result is $ result"

Logical statement:

Test command:
1. test condition expression
2. [conditional expressions]:
[Operator file or directory]:
Operators include:
-D: whether it is a directory
-E: whether it exists
-F: whether the file is used
-R: whether the current user has the read permission
Exercise: [-D/etc/vsftpd]
Echo $?
Or
[-D/etc/vosftpd] & Echo "yes"

If the disk space used exceeds 80%
Then alarm
Fi

If the disk space used exceeds 80%
Then alarm
Else...
Fi

For TM in "moring" "Noon" "Evening"
Do
Echo "the $ TM Of The Day ."
Done

While available memory <100 MB
Do
Available memory
Done

Function
# Define a function and call it
Adder (){
Echo 'expr $1 + $2'
}
Adder12 34
Adder 56 78

 

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.