免交互:
交互:当我们使用程序时,需要进入程序发出对应的指令,然后程序做出对应的执行结果
免交互:I/O重定向,通过这种方式把指令提交给程序,程序运行指令
文本免交互:
Here Document 文本免交互
↓
- 格式:
命令 << 标记
。。。
。。。
。。。
。。。
内容
标记
例子:
wc -l << xy104
<1
<2
<3
<4
<xy104
-
1、标记位可以是任意合法字符(EOF作为标记)
-
2、结尾的标记一定要顶格写,前面不能有空格,也不能任何其他字符
-
3、开头的标记位,前后的空格都会被自动省略(可以有空格)
重定向输出:
变量配置:
file=‘test1.txt’
i=‘sun’
cat>> $file << EOF
today is "{$i}" day
EOF
- 在EOF外面定义的变量,可以直接传入使用
var="study make me happy"
myvar=$(cat<<EOF
today is monday
sun is good
$var
EOF
)
- EOF的输入内容可以直接赋值给变量
- 在开头的标记位使用单引号,外部的变量就不会被免交互内部使用
去掉每行之前的tab键
可以自动抑制tab键的空格
cat >>test4.sh<<-EOF
xy104
xy105
EOF
expect语句:
建立在tcl(类c语言)的一个工具,可以用来对程序进行自动化控制和测试 ---------- 解决shell脚本中交互的问题
###转义符:
- /n:换行
- /t:制表符(tab键)
- /r:表示回车键
- /b:表示退格键(backspace)
expect的语法:
1、#!/usr/bin/expect --------- 必须声明使用的解释器
2、使用expect的脚本,不可以使用bash 编译,必须是赋权执行,用点杠(./)的方式执行
作业:
#!/usr/bin/expect
set timeout 1
set disk "/dev/sdb"
#启动fdisk
spawn fdisk $disk
# 创建主分区
expect "命令 (m 获取帮助):"
send "n\r"
expect "选择 (默认 p):"
send "\r"
expect "分区号 (1-4, 默认 1):"
send "\r"
send "\r"
expect "最后扇区:"
send "+10G\r"
expect "命令 (m 获取帮助):"
send "w\r"
# 创建扩展分区
spawn fdisk $disk
expect "命令(m 获取帮助)::"
send "n\r"
expect "选择(默认 p)::"
send "e\r"
expect "分区号(1-4,默认 1):"
send "\r"
expect "起始扇区:"
send "\r"
expect "最后扇区:"
send "+5g\r"
expect "命令 (m 获取帮助):"
send "w\r"
# 创建逻辑分区
spawn fdisk $disk
expect "命令(m 获取帮助):"
send "n\r"
expect "选择(默认 p):"
send "l\r"
expect "起始扇区:"
send "\r"
expect "最后扇区:"
send "+2G\r"
expect "命令 (m 获取帮助):"
send "w\r"
expect eof
# 创建文件系统
spawn mkfs.ext4 "${disk}1"
expect eof
spawn mkfs.ext4 "${disk}5"
expect eof
# 创建挂载点
spawn mkdir -p /opt/test1 /opt/test2
expect eof
# 挂载分区
```bash
spawn mount "${disk}1" /opt/test1
expect eof
spawn mount "${disk}5" /opt/test2
expect eof
🐶
🐉
🐍
🐯
🐴
🐰
🐒
🐔
🐭
🐮
🐑
🐷