Need help with the Script to pass stdin in run time

I have put a script inside bash_profile of user "root". That script executes when we do "sudo su -" and prompts with a question : "Why are you logginf as root?" and users have to pass the reason then they get prompt. Inside script we have used "read -p input" to take input from user.

I am a writing a script to check if this functionality works in all servers. Something like below-

_check_whyroot()

{

_check_why=`sudo su - 2>&1`

[[ $_check_why == *"Why are you logging in as root"* ]] && [[ $? == 0 ]] && echo "Checking Whyroot Script is working...Okay"  || echo "Checking Whyroot Script is working...Not Okay"

}

_check_whyroot

The issue with this function is that it waits for the input from user when they execute it because of "read -p command" inside bash_profile of root user. And I am also not able to pass the stdin to it forcefully in the script because "sudo su -" command spawns a new shell for root user.

Is there a way I can pass an input to this so I do not need to press "enter" or pass anything manually.

Your script shouldn't be doing sudo su - in the first place. Trying to automate an interactive login is the root of all that trouble. Just do sudo -u root /path/to/whateveryouwanttorun

Hi Corona,

I believe I could not put my question right way.

This is already a functionality in the bash_profile of root user to make sure if ever someone do "sudo su -" he/she should be asked question that why he is becoming root. That is achived by putting a script in bash_profile of root user that uses "read -p" to take input.

Now, the purpose of the function I wrote is not automating root login, but to check when authorized users do a "sudo su -" , they get a question prompt. So my function that i wrote is just to make sure if this functionality works.

if i do sudo -u root <Command> then I will not be able to capture the question that pops up from bash_profile of root user. I need to capture that string in a variable or in a file in an automated way.

Your problem is beyond me. Pls rephrase and post supporting data / scripts.