Day04 string intercept & String initial value & array & string substitution

Source: Internet
Author: User

One, string interception

":" represents interception

1.1 ${var: Start position: Length}

Starting position starting at 0 (0 can be omitted)
x=13918581996
Echo ${x::5} #只取前5位数字
13918

1.2 Expr substr "$var" Start position length

Starting position starting from 1
x=13918581996
Expr substr "$x" 1 5
13918

1.3 Cut-b Start position-end position

Intercept in sequence, starting from 1
echo $x |cut-b 1-5
13918

1.3.1 Cut-b position 1, Position 2, location 3

echo $x |cut-b 1,2,3,4,5
13918
echo $x |cut-b 5,4,3,2,1
13918

Second, Path segmentation 2.1 dirname "$var"

Intercepting Directory Locations
Y=/etc/httpd/conf/httpd.conf
DirName $y
/etc/httpd/conf

2.2 basename "$var"

Intercept File Locations
Y=/etc/httpd/conf/httpd.conf
BaseName $y
httpd.conf

Third, string substitution

"/" represents the replacement

3.1 Echo ${var/old/new}

Replaces only the first matching result. That is, only the nearest old one is replaced with new, and the "/" symbol represents a replacement. Wildcard characters are supported inside curly braces
echo ${x/1/}
3918581996

3.2 Echo ${var//old/new}

Replace all matching results. That is, replace all old with new and the "//" symbol to replace all
echo ${x//1/}
996858

Four, String Qiatouquwei

"#" represents a pinch head; "%" represents a go-tail

4.1 String pinch head 4.1.1 from left to right (longest match delete)

#代表从左往右删, # #表示最长匹配
I want to know which interpreter is used by root user?
n=head -1 /etc/passwd
echo ${n##:} # "" matches All ":" as well as delete
/bin/bash

4.1.2 from left to right (shortest match delete)

#代表从左往右删, shortest match
I want to know if the root user is configured with a password?
n=head -1 /etc/passwd
echo ${n#*:}
X:0:0:root:/root:/bin/bash

4.2 String 4.2.1 Delete from right to left (longest match deleted)

% represents the longest match from right to left
I want to know who is the first line of passwd?
n=head -1 /etc/passwd
echo ${n%%:*}
Root

4.2.2 from right to left (shortest match delete)

% represents from right to left, shortest match
I want to delete the root user's interpreter.
n=head -1 /etc/passwd
echo ${n%:*}
Root:x:0:0:root:/root

Five, the string initial value judgment 5.1 echo ${var:-value}

Returns the value of the $var if the variable var already exists and is not NULL, otherwise returns the string "value"
That is, attempts to display the value of $var, if the assignment was previously assigned, or if the variable does not exist or is not assigned:-The value after
Echo ${pass:-123456}
123456 #此时显示的是value
pass=8888
Echo ${pass:-123456}
8888# now shows the value assigned to $pass.

VI, array 6.1 definition

A variable can have more than one value, and the value defaults to text, which is automatically converted to an integer when used in a mathematical operation. The biggest benefit of arrays is that if you have a large number of values to store, you can define them in one line
6.2 Array Assignment format

6.2.1 Array name = (value 1 value 2 value 3 ...)

Overall assignment, subscript starting from 0
svrs= (www ftp mail game)
echo ${svrs[]}
www ftp mail game
You can also modify the value
Svrs[3]=bbs
echo ${svrs[
]}
www ftp mail BBS

6.2.2 array name [subscript]= Value

Assign a value to a single element, starting with the subscript 0
fqdn[0]=www.tmooc.cn
Fqdn[1]=www.baidu.com

6.3 Output array element 6.3.1 ${array name [subscript]}

Output a single array element

6.3.2 ${array name [@]}

Get all array elements

6.3.3 ${#数组名 [@]}

Gets the number of all array elements

6.3.4 ${array name [@]: Start subscript: Number of elements}

Getting multiple array elements in succession

6.3.5 ${#数组名 [subscript]}

Gets the length of an element

6.3.6 ${array name [subscript]: Start subscript: number of characters}

Intercepts a portion of a single array element (containing the starting subscript character itself)

Vii. expect expected interaction 7.1 introduction

Automated interactive programs written in the TCL language can be used in shell scripts to automatically deliver pre-prepared text or instructions for interactive processes without human intervention.
Trigger conditions are expected to appear in the feature prompt text, as long as it appears to the command, as long as the text appears, etc.

7.2 Format

#!/usr/bin/expect #指定脚本解释器
Set Host #定义变量
Set timeout #定义超时时间 in seconds
Spawn #创建交互式进程壳
Expect {
"Feature text" {Send "command"}
} #根据特征文本执行相应命令
Exp_continue #前面的exepct条件满足后, continue execution of the following statement
Interact #expect脚本执行完毕后不退出, control over to expect console

7.3 Script Examples

Automatic telnet Server execution after command exit
Bash script:
#!/bin/bash
host=192.168.4.7
User=root
Pass= "123456"
Expect << EOF
Spawn ssh [email protected] $host
Expect "password" {set timeout 180;send "$pass \ r"}
Expect "#" {Send "cat/etc/redhat-release\r"}
Expect "#" {send "ls"}
Eof
Interact
Expect script:
#!/usr/bin/expect
Set Host 192.168.4.7
Set User root
Set Pass "123456"
Spawn ssh [email protected] $host
Expect {
"Yes" {send "yes\r"; Exp_continue}
"Password" {set timeout 180;send "$pass \ r"; Exp_continue}
"[$user \@" {send "cat/etc/redhat-release\r"}
"#" {send "ls"}
}
Interact

7.4 There is a problem 7.4.1 yes/no situation is not necessarily

Because some computers have previously saved keys, some have not been saved

7.4.2 SSH connection Slow causes expect script timeout execution to fail

One way is to modify the sshd configuration file
Another option is to modify the timeout timer time for expect

7.4.3 the last command in the EOF section does not execute.

Day04 string intercept & String initial value & array & string substitution

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.