ubuntu运行sh脚本sudo自动输入密码

以前写的一个自动清理指定磁盘文件的脚本忽然挂了,查看log发现如下报错

1
sudo: no tty present and no askpass program specified

根据报错,大概推断是shell交互出现问题,google之,找到问题所在:sudo 版本升级,需要增加-S参数.

  • 使用Man查询sudo -S参数

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.

  • 可见加上-S参数sudo才会从标准输入中读取密码,不加-S参数以上命令将起不到作用
1
2
3
4
1 cd /opt/hlistenexp_data
2 sudo -S find . -atime +7 -exec rm -f {} \; <<EOF
3 PassWord
4 EOF