Five, string processing1. Sub-string interception operations:
Path segmentation:
DirName command: Locate the folder where the target is located
basename command: The name of the target object, usually refers to the file name or the lowest-level directory name
Using the expr command
Format: Expr substr $Var 1 starting position intercept length
Using ${} expressions
Format: ${var1: Start position: intercept length}
Example:
1.
Var= "/etc/httpd/conf/httpd.conf"
DirName $Var
Etc/httpd/conf
BaseName $Var
httpd.conf
2.
Var=hsdzwmoon
Expr substr $Var 4 6 #编号从1开始
Zwmoon
3.
Var=hsdzwmoon
Echo ${var:4:6} #编号从0开始
Wmoon
Echo ${var::5} #从开头开始截取 can omit writing from the starting position
Hsdzw
2. String substitution
Using ${} expressions
Format 1:${var/old/new} #将变量里第一个old替换成new
Format 2:${var//old/new} #将变量里所有old替换成new
Example:
1.
Var=hsdzwmoon
Echo ${var/o/l}
Hsdzwmlon
Echo ${var//o/l}
Hsdzwmlln
3. Using Random strings
How do I get a random string?
There are no variables in Linux that can fetch random strings.
It can be obtained by the following ways:
/dev/urandom #设备文件, random characters garbled
|
/usr/bin/md5sum #校验和 Generate ASCII code
|
/bin/cut #格式处理, intercept the parts we need to keep.
Random character->ascii character
Head-1/dev/urandom |md5sum #head-1 first line random character
Cut a string with cut
echo $var |cut-b start position-end position #位置为首, footer can be omitted
Example:
1.
Such as:
Vi. Condition Testing1. Test Operation specification:
The nature of the test:
is an action command that determines whether a condition is true based on the return value of $?
Operating Specifications:
Format 1:test conditional expression
Format 2:[conditional expression]
Practice methods for testing actions:
Judge the result directly with &&echo YES
Usage: [conditional expression] && echo YES
Example:
1. For example:
2. Detection of File Status:
2.1 Existence and Identification:
-E: Whether the target exists
-D: Whether it is a directory
-F: Whether it is a file
Example:
1. For example:
2.2 Detection of permissions:
-r: Whether Read permission is available
-W: Write permission
-X: Whether there are executable permissions
Example:
1. For example:
[-X "/etc/shadow"] && echo YES
[-R "/etc/shadow"] && echo YES
YES
Special cases:
Root User under:
Ls-l/etc/shadow
-R--------1 root root 1386 04-16 18:58/etc/shdow
[-W "/etc/shadow"] && echo Yes #root下我们可以看到没有写权限, but the output is yes, note here: Owner W exception
YES
Integer value comparison, substring matching
Integer value comparison:
-eq: Equals
-ne: Not equal to
-GT: Greater Than
-LT: Less than
-ge: greater than or equal to
-le: Less than or equal to
Example:
W.H.O. | Wc-l #统计登陆用户数
String Matching:
=: Two strings are the same
! =: Two strings are not the same
Example:
1. For example:
Vii. using if to determine the structureProgram Flow control:
Sequential, automated execution of processes
The choice and processing of the hope Script intelligence
Make repetitive operations smarter
For... Multiple objects ... How do I deal with it one by one? For
In... The value is ... In the case of what? Case
If... The condition is ... What does the program need to do? If
1. Single Branch IF statement structure
If condition test
Then command sequence #只关注条件成立的情况
Fi
Example:
Single Branch If application example:
Check the backup directory/opt/mrepo, if it does not exist, create
Cat chkdir.sh
#!/bin/bash
Backup_dir= "/opt/mrepo"
if [!-D $BACKUP _dir]
Then
Mkdir-p $BACKUP _dir
Fi
2. Dual-Branch IF statement structure
If condition test
then command sequence 1
else Command sequence 2
Fi
Example:
Two-branch if application example:
Determine if the target host is alive and show the test results
Cat chkhost.sh
#!/bin/bash
Ping-c 3-i 0.2-w 3 $ &>/dev/null
if[$?-eq 0]
Then
echo "Host is up."
Else
echo "Host is down"
Fi
./chkhost.sh 192.168.4.11
Host 192.168.4.11 is up
./chkhost.sh 192.168.4.13
Host 192.168.4.13 is down
3. Multi-branch IF statement structure
If condition test 1
then command sequence 1
Elif Condition Test 2
then command sequence 2
Else
Command sequence N
Fi
Example:
Multi-Branch If application example
Judging machine test scores, distinguishing between excellent/qualified/unqualified
Cat gradediv.sh
#!/bin/bash
Read-p "Please enter your score (0-100):" GRADE
If [$GRADE-ge] && [$GRADE-le];then
echo "$GRADE points! Excellent
elif [$GRADE-ge]&&[$GRADE-le];then
echo "$GRADE points, qualified"
Else
echo "$GRADE points? Not qualified "
Fi
Results such as:
Viii. using A For loopFor variable name in value list
Do
Command sequence
Done
For IP address in host address List
Do
Check Status
Done
Example:
1. Output 1st. 2nd in turn. 3rd.
#!/bin/bash
For i in "1st." "2nd." "3rd."
Do
Echo $i
Done
Results such as:
2. Word Output/etc/host.conf file contents
#!/bin/bash
For I in $ (cat/etc/host.conf)
Do
Echo $i
Done
Results such as:
Actual cases:
Target: Batch Add user account:
User list file Users.txt, one per line
Set the initial password to 123456 and must be changed after the first login
Cat uad.sh
#!/bin/bash
For I in $ (cat/root/users.txt)
Do
Useradd $i
echo "123456" | passwd--stdin $i
chage-d 0 $i
Done
Results such as:
2. Detecting host status for an IP range
192.168.4.1-192.168.10
Judging by whether or not ping pass
Cat chkping.sh
#!/bin/bash
Ip_pre= "192.168.4."
For IP in $ (SEQ 1 5)
Do
Ping-c 3-i 0.2-w 3 ${ip_pre} $IP &>/dev/null
if[$?-eq 0];then
echo "${ip_pre} $IP is up."
Else
echo "${ip_pre} $IP is down"
Fi
Done
./chkping.sh
192.168.4.1 is down
192.168.4.2 is up
192.168.4.3 is down
192.168.4.4 is down
......
Linux shell Script (ii)