This is not possible and should not be done. Consider that if privileges can be escalated in this way, the root privileges of the system will be accessible to anyone.
Generally, if a script requires root permissions, we will execute the script directly as root at the beginning, instead of running it as our own ID first, and then switch identities through su or sudo during an operation that requires permissions. .
In addition, if you need to execute it as root and it can be executed and called by a user with ordinary permissions, you can consider setting setuid for the execution file
Can use expect script
echo "password" | sudo -S try
This is not possible and should not be done. Consider that if privileges can be escalated in this way, the root privileges of the system will be accessible to anyone.
Generally, if a script requires root permissions, we will execute the script directly as root at the beginning, instead of running it as our own ID first, and then switch identities through su or sudo during an operation that requires permissions. .
In addition, if you need to execute it as root and it can be executed and called by a user with ordinary permissions, you can consider setting setuid for the execution file