Reputation: 720
I'm calling bash A via different user within bash B like this, and i want to stuck on line "*" untill user enters "exit".
# bash B
su test<<endOfTestOperation
./bashA.sh
endOfTestOperation
# bash A
# Start a process &
read LINE <------ I want to wait here until user inputs "exit"
if [ "$LINE" == "exit" ]; then
# end the process
exit
fi
If i call bash A only, it works fine. if i put "read" after "endOfTestOperation", it also works. So i guess the problem roots in using different permission or forked process.
[supplemental description] What i'm trying to do is: start a tcpdump (root permission required, so bash b is ran with sudo), then start a tcpserver progress, then end the tcpserver on demanding of user input in stdin.
Upvotes: 0
Views: 209
Reputation: 531165
You are redirecting standard input for su
from a here document. The command that su
runs will inherit its standard input from su
, so it is also the here document, not the terminal. Since su
consumes the entire here document in reading the command to run, there is nothing left for bashA.sh
to read, and so the read
command fails immediately rather than waiting for input.
You'll need to post more detail about what you are actually trying to do if you want a workaround.
Upvotes: 1
Reputation: 3646
Using an if
statement will not continue to check if the input is "exit", it will only check once.
You could use an until
loop to make sure user input is "exit".
You can find the process of the most recent command sent to the background by using $!
.
background command &
pid=$!
until [[ $LINE == exit ]]; do
read LINE
done && { kill -9 $pid; exit; }
Upvotes: 0