Mark blog

知行合一 划水归档

Linux Shell 脚本编程进阶(四)

高级变量用法-有类型变量

Shell变量一般是无类型的,但是bash Shell提供了declare和typeset两个命令用于指定变量的类型,两个命令是等价的
declare [选项] 变量名
​ -r 声明或显示只读变量
​ -i 将变量定义为整型数
​ -a 将变量定义为数组
​ -A 将变量定义为关联数组
​ -f 显示已定义的所有函数名及其内容
​ -F 仅显示已定义的所有函数名
​ -x 声明或显示环境变量和函数
​ -l 声明变量为小写字母 declare –l var=UPPER
​ -u 声明变量为大写字母 declare –u var=lower

eval命令

eval命令将会首先扫描命令行进行所有的置换,然后再执行该命令.该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两次扫描
示例:

1
2
3
4
5
6
7
8
9
10
[root@server ~]# CMD=whoami
[root@server ~]# echo $CMD
whoami
[root@server ~]# eval $CMD
root
[root@server ~]# n=10
[root@server ~]# echo {0..$n}
{0..10}
[root@server ~]# eval echo {0..$n}
0 1 2 3 4 5 6 7 8 9 10

间接变量引用

如果第一个变量的值是第二个变量的名字,从第一个变量引用第二个变量的值就称为间接变量引用
variable1的值是variable2,而variable2又是变量名,variable2的值为value,间接变量引用是指通过variable1获得变量值value的行为
​ variable1=variable2
​ variable2=value

bash Shell提供了两种格式实现间接变量引用
​ eval tempvar=$$variable1
​ tempvar=${!variable1}
示例:

1
2
3
4
5
6
7
8
[root@server ~]# N=NAME
[root@server ~]# NAME=mark
[root@server ~]# N1=${!N}
[root@server ~]# echo $N1
mark
[root@server ~]# eval N2=$$N
[root@server ~]# echo $N2
mark

创建临时文件

mktemp命令:创建并显示临时文件,可避免冲突
mktemp [OPTION]… [TEMPLATE]
​ TEMPLATE: filenameXXX
​ X至少要出现三个
OPTION:
​ -d: 创建临时目录
​ -p DIR或–tmpdir=DIR:指明临时文件所存放目录位置
示例:

1
2
3
4
5
mktemp /tmp/testXXX

tmpdir=`mktemp –d /tmp/testdirXXX`

mktemp --tmpdir=/testdir testXXXXXX

安装复制文件

install命令:
​ install [OPTION]… [-T] SOURCE DEST 单文件

1
2
3
4
5
install [OPTION]... SOURCE... DIRECTORY

install [OPTION]... -t DIRECTORY SOURCE...

install [OPTION]... -d DIRECTORY...创建空目录

选项:
​ -m MODE,默认755

1
2
3
-o OWNER

-g GROUP

示例:
​ install -m 700 -o mark -g admins srcfile desfile

1
install –m 770 –d /testdir/installdir

expect介绍

expect 是由Don Libes基于Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect命令

expect 语法:
expect [选项][ -c cmds ] [ [ -[f|b] ] cmdfile ][ args ]
选项
​ -c:从命令行执行expect脚本,默认expect是交互地执行的
​ 示例:expect -c ‘expect “\n” {send “pressed enter\n”}
​ -d:可以输出输出调试信息
​ 示例:expect -d ssh.exp
expect中相关命令
​ spawn:启动新的进程
​ send:用于向进程发送字符串
​ expect:从进程接收字符串
​ interact:允许用户交互
​ exp_continue 匹配多个字符串在执行动作后加此命令

expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
​ expect “hi” {send “You said hi\n”}
​ 匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:

1
2
3
expect "hi" { send "You said hi\n" } \
"hehe" { send “Hehe yourself\n" } \
"bye" { send “Good bye\n" }

匹配hi,hello,bye任意字符串时,执行相应输出.等同如下:

1
2
3
4
5
expect {
"hi" { send "You said hi\n"}
"hehe" { send "Hehe yourself\n"}
"bye" { send “Good bye\n"}
}

expect例程

1
2
3
4
5
6
7
#!/usr/bin/expect
spawn scp /etc/fstab 192.168.8.100:/app
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send “mark\n" }
}
expect eof

例程:变量

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/expect
set ip 192.168.8.100
set user root
set password mark
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact

例程:位置参数

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
#./ssh3.exp 192.168.8.100 root mark

例程:执行多个命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "useradd haha\n" }
expect "]#" { send "echo mark |passwd --stdin haha\n" }
send "exit\n"
expect eof
#./ssh4.exp 192.168.8.100 root mark

例程:shell脚本调用expect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "useradd hehe\n" }
expect "]#" { send "echo mark |passwd --stdin hehe\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
#./ssh5.sh 192.168.8.100 root mark