简体   繁体   中英

Executing bash commands automatically under sudo is not working

I need a script to execute sudo commands without requesting a password. In the following scripts I have a strange bash stdin behaviour:

username@username-laptop:~$ sudo -Si | echo "password" 
password
root@username-laptop:~# exit
username@username-laptop:~$ sudo -Si | echo "password"
password
[sudo] password for rootname:

(password is a root password on my machine)

Why the password has not been requested in the first time, and has been in the second (and always after that)? Also I don't understand how stdin works in bash, where is an error in my code?

I know this is a very bad idea to put a root password to the command directly, but I need it to execute correctly.

Maybe you shoud try:

echo "Password" | sudo -Si

instead of:

sudo -Si | echo "Password"

The output of the first command (before the "|") is used as input to the second command.

You should be aware that the first command cannot do any screen output while the second command cannot receive any keyboard input unless you do some programming tricks...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM