我想创建一个简单的bash脚本,该脚本在带有命令的文件上执行for循环并执行这些命令,并在发生错误时结束。我有这样的东西
#!/bin/bash
while IFS= read -r line; do
echo $line
output=$(eval $line)
if [ $? -eq 0 ]
then
echo ok
else
echo $output
break
fi
echo
done < summary.txt
问题是,我尝试执行的第一个命令是sudo命令,所以我必须输入密码。我试着把它放入命令中,如下所示
sudo -S <<< password <<< Y command
不走运。我已经检查过,如果我直接将其放入而不需要读取它(而不是将其放入字符串),那么它是有效的。问题是,如果没有循环,程序就会很长,没有太多意义。
谢谢
发布于 2020-03-18 02:51:30
echo <password> | sudo -S < your command>
来自man sudo
-S, --stdin
Write the prompt to the standard error and read the password from the standard input instead of using the terminal
device. The password must be followed by a newline character.
https://stackoverflow.com/questions/60732802
复制相似问题