linux - 如何通过 shell 脚本向提示符提供输入值?

标签 linux bash shell

我正在编写一个包装器 shell 脚本 wrapper.sh 来运行其他人拥有的一堆其他已经可用的脚本,但我无法触及这些脚本。

问题是,有一个脚本运行一些特定于数据库的事件 - db_perf_clean.sh。该脚本通常手动执行,并在运行时提示输入密码。我无法将密码作为参数提供给它,我也无法修改该脚本。因此,我知道数据库密码,我可以在 wrapper.sh 中提供它。

请告诉我如何在 wrapper.sh 中运行 db_perf_clean.sh 脚本,就像在静默模式下一样。

最佳答案

有时脚本会坚持从 tty 读取密码。通常,它会从标准输入读取。如果是这样,请尝试:

echo password | db_perf_clean.sh

上面的缺点是密码会出现在ps中。为避免这种情况,将密码隐藏在文件中并将该文件用于标准输入:

db_perf_clean.sh <file_with_password

如果你想让命令安静下来,你可以丢弃它的输出:

db_perf_clean.sh <file_with_password >/dev/null 2>&1

在 bash 下,与通用 shell 不同,可以稍微简化一下:

db_perf_clean.sh <file_with_password &>/dev/null

关于linux - 如何通过 shell 脚本向提示符提供输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578165/

相关文章:

linux - 通过gdb手动唤醒epoll_wait?

linux - 序列中丢失文件

Bash:在 "Vim: Warning: Output not to a terminal"时杀死 Vim

perl - 如何从 cron 运行的 Perl 脚本中运行 shell 脚本?

vhost 的 Php 版本,apache2

php - $_FILES 空 - 撕扯我的头发

linux - 如何为我的存储库删除所有 Git 痕迹?

linux - Bash 脚本执行所需的帮助

xml - 使用 incron 和 bash 脚本调用 REST API?

linux - Linux 中的 shell 命令之间什么时候应该暂停?