Interaction issues are always used in shell scripts. Many beginners do not use expect and are not familiar with expect. I will use expect in my first shell script.
This is an ordinary user who needs root privileges to find bad blocks. The code part is the expected language part of the shell script.
Where mypassword is the password of the root user, please write according to your own password
spawn is the command that comes with expect, mainly to add a shell to the connection
Expect is to interact when the content in quotation marks appears,
send is the password or command that needs to be entered, remember to enter "\n" after typing
Variables in shell scripts can be used in expect, such as $FILE in the code
/usr/bin/expect <<-EOF
spawn su -root
expect "Password: "
send "mypassword\n"
send "badblocks -s -v /dev/vda1>$FILE\n"
set timeout 15
send "exit\n"
expect eof
EOF
Note : I wrote the code in the text, so please type the above code in the script by yourself.