1. 转义符:
    /r:回车
    /n: 换行
    /t:水平制表符 (Tab键)
    /v: 垂直制表符

  2. if 语句
    包括:
    1, if [ 表达式 ] then 语句 fi

  3. if [ 表达式 ] then 语句 else 语句 fi

  4. if 表达式] then 语句 elif[ 表达式 ] then 语句 elif[ 表达式 ] then 语句 …… fi

3 . for循环:
(1)for loop in 1 2 3 4 5
do
echo “The value is: $loop”
done

(2)for i in $ ( cat host.txc )
do
scp host.txc [email protected]$i:/root
done

4 . while循环:
while ((i <= 100))
do
((sum += i))
((i++))
done (在循环中使用 break 与continue 跳出循环。 另外,break 命令后面还可以跟一个整数,表示跳出第几层循环)

5 . util循环:unti 循环和 while 循环恰好相反,当判断条件不成立时才进行循环,一旦判断条件成立,就终止循环
until ((i > 100))
do
((sum += i))
((i++))
done

跳出循环参数:
exit:直接结束脚本的执行
break:用于完全结束一个循环,跳出循环体执行循环后面的语句
continue:结束当前循环,开始执行下一次循环

6 . 函数:
函数名 () {
命令列表
[ return value ]
}
像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项(unset .f function_name)

7 . 逻辑连接:
(1) expression1 && expression2  #expression1为真才执行expression2
(2) expression1 || expression2   #expression1为假才执行expression2
(3) expression1 ;expression2   #不管expression1执行是否正确,expression2都会执行

8 . 使用脚本执行交互命令工具: expect
expect 涉及用法说明:
shell个人笔记

(1)#!/bin/bash
expect <<eof
spawn “passwd”
expect “密码:” {send “234 \r”}
eof

(2)#!/usr/bin/expect -f
#最多不超过10分钟,命令执行持续的时间要比这个时间段,否则会提前退出
set timeout 600 #设置超时 , -1 可以设置为永远等待
set RUN_PATH “/root/pack_data”
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]

spawn ssh $ [email protected]$ hostname
expect “password:” {send “$password\r”;}
expect接收参数的方式和bash脚本的方式不太一样,bash是通过$0 … $n 这种方式,而expect是通过set <变量名称> [lindex $argv ],
例如 set username [lindex $argv 0]

定义变量:bash中定义变量 password=123456 ,而expect定义变量 set password “123456”

(3) #!/bin/bash
expect <<eof
spawn ssh [email protected]
expect {
-re “*(yes/no)” {send “yes\r”} # -re 表示正则匹配
-re “Password:” {send “Txc27624138…\r”}
}
expect eof
eof

(4) 注 :在shell中插入expect需要加上" expect <<eof eof "
也可以用 “ expect -c ”,例:
#!/bin/bash
hosts=“hostname1 hostname2”
for host in $ hosts
do
expect <<eof
spawn ssh [email protected]
expect {
-re “*(yes/no)” {send “yes\r”} # -re 表示正则匹配
-re “Password:” {send “Txc27624138…\r”}
}
expect eof
eof
done

(5) exp_continue的作用:执行下一个匹配,如果没有该参数,则是多个expect分支中匹配一个成功即执行完成

#!/usr/bin/expect -f

set timeout -1
//永远等待,不会超时
spawn ssh [email protected]
//spawn 后面跟命令名称和参数

//如果匹配到*assword,那么发送密码,并进入下面的expect语句(uname -a语句)。
//如果匹配到yes/no,那么发送yes,并重新执行这个expect语句。

expect {
assword" {send “123456\r”;}
“yes/no” {send “yes\r”;exp_continue}
}
expect "
]#” {send “uname -a\r”}
send “exit\r” //退出远程登录
//匹配到 *]# ,那么运行uname -a命令
expect eof //结束spawn

//开始下一个命令
spawn ssh [email protected]
expect {
assword" {send “123456\r”;}
“yes/no” {send “yes\r”;exp_continue}
}
expect "
]#” {send “uname -a\r”}
send “exit\r” //退出远程登录
expect eof //结束spawn

exit //退出expect脚本

(6)“ interact ”和 “ expect eof ”的区别:

使用interact会保持在终端而不会退回到原终端,
比如切换到root用户,会一直在root用户状态下;
比如ssh到另一服务器,会一直在目标服务器终端下,而不会切回的原服务器。

" expect eof 表示交互结束,退回到原用户;
interact 会停留在目标用户。"

(7) ssh实现自动登录,并停在登录服务器上:
#!/usr/bin/expect -f
set ip [lindex $argv 0 ] //接收第一个参数,并设置IP
set password [lindex $ argv 1 ] //接收第二个参数,并设置密码
set timeout 10 //设置超时时间
spawn ssh [email protected]$ ip //发送ssh请求
expect { //返回信息匹配
“*yes/no” { send “yes\r”; exp_continue} //第一次ssh连接会提示yes/no,继续
*password:" { send “$password\r” } //出现密码提示,发送密码
}
interact //交互模式,用户会停留在远程服务器上面.

(8)在shell中调用expect脚本
#!/usr/bin/expect -f
spawn passwd txc
expect “Password” { send “123456”}

#!/bin/bash
expect /root/expect.sh 执行expect脚本

相关文章:

  • 2021-12-23
  • 2021-04-22
  • 2021-05-18
  • 2021-05-19
  • 2021-11-03
  • 2021-09-26
猜你喜欢
  • 2021-05-28
  • 2021-10-14
  • 2021-05-21
相关资源
相似解决方案