有一个示例脚本,并且一次只想执行一个,那么如何向 shell 脚本提供命令行输入来执行响应?另外,我无法修改脚本,下面的代码只想执行“ls”命令。
have tried below command to execute, but it took 'yes' for both
yes yes no | bash test.sh
e.g:
while true; do
read -p "Would you like to run ls command? (y/n)" response
case $response in
[Yy]* ) ls; break;;
[Nn]* ) break;;
* ) echo; echo "Please answer yes or no.";;
esac
done
while true; do
read -p "Would you like to run PWD command? (y/n)" response
case $response in
[Yy]* ) pwd; break;;
[Nn]* ) break;;
* ) echo; echo "Please answer yes or no.";;
esac
done
有人能建议解决方案吗?
答案1
从描述yes
:
重复输出包含所有指定字符串或“y”的行
所以,yes yes no
输出:
yes no
yes no
yes no
...
但你想要
yes
no
您可以尝试在字符串中添加换行符:
yes 'yes
no' | bash test.sh
或者,由于您只需要一次而不是重复,因此可以手动将其提供给您的脚本:
printf '%s\n%s\n' yes no | bash test.sh