0

When I launch the following script, it restarts as root, the new bash shell opens up, but "something" sends an "exit" command immediately and the script runs to its end.

#!/bin/bash
#set -x
PASSWORD=<rootpass>

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    echo "$PASSWORD" | sudo -S --prompt '' /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i
    echo Bash exitcode: $?
    echo "After bash: $$"
fi

BUT when I start the script in a way, that sudo has to ask for the password interactively, everything works as expected. The bash shell opens, I can do what I like, enter "exit" myself and the rest of the script is being executed.

#!/bin/bash
#set -x

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    sudo /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i
    echo Bash exitcode: $?
    echo "After bash: $$"
fi

Can someone explain to me, why piping the password into sudo -S is behaving so differently, than a "normal" sudo call?

EDIT: If I explicitely disable the bash internal exit command, it seems to work, but I still don't understand, where the first "exit" comes from. So this solution is more of a hack than anything else.

bash --rcfile <(echo "enable -n exit; echo; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i

EDIT 2: @WilliamPursell The following works now (added < /dev/ttyto bash call), but I don't know, if it has any further implications:

#!/bin/bash
#set -x
PASSWORD=<rootpass>

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    echo "$PASSWORD" | sudo -S --prompt '' /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i </dev/tty  
    echo Bash exitcode: $?
    echo "After bash: $$"
fi
  • 2
    If `bash` is running with its input from the `echo` pipe, then it has nothing to read. It's a terrible idea, but you could try `{ echo "$PASSWD"; cat /dev/tty; } | sudo -S --prompt '' /bin/bash "$0" -- "$@"`. But don't do this. – William Pursell Jan 17 '22 at 03:25
  • you can use tools like [tag:expect] (for [tag:tcl]) or [tag:pexpect] (for [tag:python]) for such requirements. try my [sexpect](https://github.com/clarkwang/sexpect/) if you prefer shell scripts. – sexpect - Expect for Shells Jan 17 '22 at 04:04
  • @WilliamPursell I did try this and many other variants, but it does not work either. The only way atm is to disable the in-built "exit" command. I am ok with that, but what I don't understand is that `bash` isn't simply terminating when it has nothing to read, but executing an `exit`command on its own command line??? Where is this `exit` coming from? I could not find anything in the `man` pages of `bash` and `sudo`. – Holger Pandel Jan 17 '22 at 14:10
  • https://stackoverflow.com/questions/8514735/what-is-special-about-dev-tty#comment95351211_8514853 – Holger Pandel Jan 17 '22 at 17:13

0 Answers0