shell中交互输入自动化
来源:互联网 发布:sql清空表数据 编辑:程序博客网 时间:2024/06/07 07:09
shell中交互输入自动化
shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了。这个功能很有用的哟。好好学习。
1 利用重定向
重定向的方法应该是最简单的
例:
以下的test.sh是要求我们从stdin中分别输入no,name然后将输入的no,name打印出来
[root@localhost test]# cat test.sh
#! /bin/bash
read -p "enter number:" no
read -p "enter name:" name
echo you have entered $no, $name
以下是作为输入的文件内容:
[root@localhost test]# cat input.data
1
lufubo
然后我们利用重定向来完成交互的自动化:
[root@localhost test]# ./test.sh < input.data
you have entered 1, lufubo
看吧!效果不错吧!哈哈
2 利用管道完成交互的自动化
这个就是利用管道特点,让前个命令的输出作为后个命令的输入完成的
也用上面例子举例:
[root@localhost test]# echo -e "1\nlufbo\n" | ./test.sh
you have entered 1, lufbo
上面中的 "1\nlufbo\n" 中的“\n”是换行符的意思,这个比较简单的。
3 利用expect
expect是专门用来交互自动化的工具,但它有可能不是随系统就安装好的,有时需要自己手工安装该命令
查看是否已经安装:rpm -qa | grep expect
以下脚本完成跟上述相同的功能
[root@localhost test]# cat expect_test.sh
#! /usr/bin/expect
spawn ./test.sh
expect "enter number:"
send "1\n"
expect "enter name:"
send "lufubo\n"
expect off
注意:第一行是/usr/bin/expect,这个是选用解释器的意思,我们shell一般选的是 /bin/bash,这里不是
spawn: 指定需要将哪个命令自动化
expect:需要等待的消息
send:是要发送的命令
expect off:指明命令交互结束
- shell中交互输入自动化
- shell中交互输入自动化
- shell中自动化交互实现--一般用户脚本自动切换
- Shell 交互输入
- Shell脚本交互之:自动输入密码
- shell脚本之输入交互read
- Shell脚本交互之:自动输入密码
- Shell while read命令完成用户的交互输入
- Linux-----Shell脚本交互实现自动输入密码
- Shell脚本交互之:自动输入密码【转】
- shell中Expect的程序交互
- Linux中shell自动交互的使用
- 基于linux expect处理linux自动化中交互问题
- 【收集】解决自动化测试中那些难以实现的交互
- 交互shell
- shell 交互
- 执行shell脚本时,遇到需要输入yes/no的时候,怎么自动化完成输入呢?
- linux shell 脚本需要交互,比如输入回车,输入YES/NO Y/N之类进行选择
- 软件开发的哲学思考
- mysql源代码相关资料
- 什么是DQL、DML、DDL、DCL
- shell中常用的% %% # ##
- 涨工资(改)
- shell中交互输入自动化
- linux中生成任意大小的文件
- JSON中数据特殊字符的处理
- linux 文本文件的交集,差集 comm命令
- 用汇编语言申请栈空间
- linux shell 查找文件差异并进行修补
- 菜鸟学汇编005 栈(Stack) 和 段寄存器SS、SP
- linux shell cut按列切分文件
- linux awk学习