编写shell脚本难免遇到需要交互式输入指令的步骤:

方法一:

# cat action.sh
#!/bin/sh
read -p "enter number:" no;
read -p "enter number:" name;
echo you have entered $no,$name;

# sh action.sh
enter number:chavin
enter number:king
you have entered chavin,king

# echo -e "chavin\nking\n" | sh action.sh
you have entered chavin,king

或者

# echo -e "chavin\nking\n" > input.data

# cat input.data
chavin
king

# sh action.sh < input.data
you have entered chavin,king

案例1:使用交互式自动化为用户改密码:

# useradd chavin0101

# echo -e "dbking588\ndbking588\n" | passwd chavin0101
Changing password for user chavin0101.
New password: Retype new password: passwd: all authentication tokens updated successfully.

方法二:

使用expect实现交互式任务自动化,expect工具在一般发行版中默认没有安装,需要手工安装,这里就不演示了。

shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化的更多相关文章

  1. shell中交互输入自动化

    shell中交互输入自动化 shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了.这个功能很有用的哟.好好学习. 1    利用重 ...

  2. shell脚本中的交互式输入自动化

    shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了. 1    利用重定向     重定向的方法应该是最简单的 例: 以下的te ...

  3. expect脚本自动获取root权限

    Linux脚本中有很多场景是进行远程操作的,例如远程登录ssh.远程复制scp.文件传输sftp等.这些命令中都会涉及到安全密码的输入,正常使用命令时是需要人工手动输入密码并接受安全验证的.为了实现自 ...

  4. 分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数

    7月19日任务 20.27 分发系统介绍20.28 expect脚本远程登录20.29 expect脚本远程执行命令20.30 expect脚本传递参数 20.27 分发系统介绍 公司业务逐渐扩大时, ...

  5. expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令

    7月20日任务 20.31 expect脚本同步文件20.32 expect脚本指定host和要同步的文件20.33 构建文件分发系统20.34 批量远程执行命令扩展:shell多线程 http:// ...

  6. 使用expect实现自动交互,shell命令行自动输入,脚本自动化,变量引用,expect spawn执行带引号命令,expect 变量为空,不生效,不能匹配通配符*,函数,数组

    背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...

  7. centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课

    centos shell编程5  LANMP一键安装脚本 lamp  sed  lnmp  变量和字符串比较不能用-eq  cat > /usr/local/apache2/htdocs/ind ...

  8. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  9. 利用Python+163邮箱授权码发送带附件的邮件

    背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...

随机推荐

  1. java 中的锁 -- 偏向锁、轻量级锁、自旋锁、重量级锁(转载)

    之前做过一个测试,详情见这篇文章<多线程 +1操作的几种实现方式,及效率对比>,当时对这个测试结果很疑惑,反复执行过多次,发现结果是一样的: 1. 单线程下synchronized效率最高 ...

  2. [svc]linux上vxlan实战

    linux vxlan实现2台机器的通往段ip互通 - 在n1上 ip l a vxlan0 type vxlan id 42 dstport 4789 remote 192.168.1.12 loc ...

  3. 跨控制器跳转view——RedirectToRoute和RedirectToAction

    已知控制器AccountController.cs和HomeController.cs,如果从页面Account/Login直接跳转到Home/Index,可以利用RedirectToRoute和Re ...

  4. Git 推送操作

    Jerry 修改了他的最后一次提交的修改操作,他已经准备好将更改.推操作的数据永久存储的 Git 仓库.推操作成功后,其他开发人员可以看到Jerry 的变化. 他执行的git日志命令来查看提交的细节. ...

  5. python初级 1 数据类型和变量

    一.整数(int) 例:  0 1 2 3 -1 -2 –3 In [31]: print(type(0)) <class 'int'> In [32]: print(type(1)) & ...

  6. 奇葩问题 eclipse中DDMS的LOGcat只有一列level

    http://stackoverflow.com/questions/25010393/eclipse-logcat-shows-only-the-first-letter-from-each-mes ...

  7. c#扩展函数

    分页 public static class IEnumerableExt { public static (IEnumerable<T> dataAfterPaging, Pageinf ...

  8. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  9. make INSTALL_MOD_PATH=path_dir modules_install

    The INSTALL_MOD_PATH variable is needed to install the modules in the target root filesystem instead ...

  10. 【03月04日】A股滚动市盈率PE历史新低排名

    2010年01月01日 到 2018年12月05日 之间,滚动市盈率历史新低排名. 上市三年以上的公司,2018年12月05日市盈率在300以下的公司. 1 - 青松股份(SZ300132) - 历史 ...