Linux study note _ 10_Shell programming _ 2_Shell programming syntax (2)

Source: Internet
Author: User

Variable test statement: used to test whether the variables are equal, empty, and file type.
Format:
Test conditions
Range: integer, string, and file
(1) integer test:
Test int1-eq int2 test whether the integer is equal
Test int1-ge int2 test whether int1> = int2
Test int1-gt int2 test whether int1> int2
Test int1-le int2 test int1 <= int2
Test int1-lt int2 test whether int1 is <int2
Test int1-ne int2 test whether the integer is not equal
(2) string test:
Test str1 = str2 test whether the string is equal
Test str1! = Str2 test whether the string is not equal
Test str1 test whether the string is not empty
Test-n str1 test string is not empty
Test-z str1 test whether the string is null
(3) file test:
Test-d file specifies whether the file is a directory
Test-f file specifies whether the file is a regular file
Test-x file specifies whether the file is executable
Test-r file specifies whether the file is readable.
Test-w file specifies whether the file is writable.
Test-a file specifies whether the file exists
Whether the size of the test-s file is not 0


Flow Control statement flow control statement: used to control the flow of shell programs
Exit statement: exit the program and return a return code. If the return code is 0, the program Exits normally. If the return code is not 0, the program exits abnormally.
Example: exit 0


1. if/else
Variable test statements are generally not used independently. They are generally used as test conditions for if statements, for example:
If test-d $1
Then
...
Fi
Variable test statements can be simplified using []. For example, test-d $1 is equivalent to [-d $1].
[Instance]
#! /Bin/sh
If [$ #-ne 2]; then
Echo "Not enough parameters"
Exit 0
Fi
If [$1-eq $2]; then
Echo "$1 equals $2"
Elif [$1-lt $2]; then
Echo "$1 littler than $2"
Elif [$1-gt $2]; then
Echo "$1 greater than $2"
Fi


More complex if statements:
If condition 1
Then
Command 1
Elif condition 2
Then
Command 2
Else
Command 3
Fi


Union of multiple conditions:
-A: logical and. The result is true only when both conditions are true.
-O: logical or. If either of the two conditions is true, the result is true.


[Example]
#! /Bin/bash
Echo "please input a file name :"
Read file_name
If [-d $ file_name]
Then
Echo "$ file_name is a directory"
Elif [-f $ file_name]
Then
Echo "$ file_name is a common file"
Elif [-c $ file_name-o-B $ file_name]
Then
Echo "$ file_name is a device file"
Else
Echo "$ file_name is an unknown file"
Fi


2. for/in
Format: for variable in name table
Do
Command list
Done
[Example]
#! /Bin/sh
For DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Do
Echo "The day is: $ DAY"
Done


[Awk command application: Segment extraction]
Awk-F domain separator 'COMMAND '[single quotation marks here]
[If you do not use-F to specify a delimiter, the default Delimiter is space]
1. Check users with UID 0 in the system.
Awk-F: '$3 = 0 {print $1}'/etc/passwd
-F: Specify the delimiter as follows:
$3 indicates the third digit of the separator
2. Check users with blank passwords in the system
Awk-F: 'length ($2) = 0 {print $1} '/etc/shadow

[Important instance]
#! /Bin/bash
# A test shell for and awk
Username = $1
# Extract the pid of all usernames
/Bin/ps aux |/bin/grep $ username |/bin/awk '{print $2}'>/home/xiaofang/test/tmp.txt
Killid = $ (cat/home/xiaofang/test/tmp.txt)
For killtmp in $ killid
Do
/Bin/kill-9 $ killtmp 2>/dev/null
Done

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.