Sudo automatic password input for Linux

Burn slowly 2022-06-24 07:00:58 阅读数:385

sudoautomaticpasswordinputlinux

1. Mode one :

#!/bin/bash
echo password | sudo -S ls
Or
echo password | sudo -s ls
notes :
-S, --stdin : Read the password from standard input
-s, --shell : Run as target user shell; One command can be specified at the same time

echo: Use a text block to enter redirection .

2. Mode two :

#!/bin/bash
sudo -S ls << EOF
password
EOF
Or
sudo -s ls << EOF
password
EOF
notes :
-S, --stdin : Read the password from standard input
-s, --shell : Run as target user shell; One command can be specified at the same time 

EOF: take EOF And << Use a combination of , Indicates that the subsequent input is the input of the subcommand , Until I met EOF until .

copyright:author[Burn slowly],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240047117654.html