In some cases, such as Oracle, you must switch to the specified user to restart Oracle.
Each time the manual operation is more troublesome, you can write a script.
The function of automatically entering password, need to use expect
Install first
Yum Install-y expect
Edit Script
VI aa.sh
#!/usr/bin/expect
Spawn su oracle-c "/opt/1.sh"
Expect "Password:"
Send "123456\r"
Interact
Exit
Note:
1. The first line is #!/usr/bin/expect, not #!/bin/bash, which requires special attention.
2.-C is the command that you need to execute after you specify the switch user. There are multiple commands that can be written in a script. Without the-c option, after switching users, subsequent commands are executed at the current user, not after the user is switched.
3.123456\r indicates that the Oracle password is 123456
4. After the script executes, the input password appears, this time, do not enter the password, wait 12 seconds, will automatically enter the password, and execute the command specified by-C, to ensure that the shell has execute permissions.
This article is from the "Falling Star" blog, make sure to keep this source http://xiao987334176.blog.51cto.com/2202382/1673981
Shell, switch user, execute specified, script