Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • big thank you for this : I used the last (script -qec ....) trick today. I added some commands before and after : ssh host 'cmds before ; SHELL=/bin/sh script -qec "sudo cmdsudoed here with args <&3 >&4 2>&5 3<&- 4>&- 5>&-" /dev/null 3<&0 4>&1 5>&2 ; cmds after here' # the ones after don't display ... probably due to all those &- ? Are they mandatory? (if I understand the order correctly: they retrieve the current values(=fd) of stdin, stdout and stderr, redirects (copy) sudo's in/out/err to the same destinations, then close them for script? thus it closes the outer stdin/out/err too?) Commented Dec 3, 2020 at 13:23
  • moreover I remember reading on this site somewhere that closing fd0 1 or 2 is not recommended: [edit: found it: of course, it was from you :) ) : unix.stackexchange.com/a/446591/27616 : as I don't see output from the commands after the closing of 3, 4, 5: does it mean it closed my ssh shell's 0, 1 and 2 ? and therefore... my successive commands could end up writing in the wrong place? :/ Commented Dec 3, 2020 at 13:35
  • @OlivierDulac, no n>&- (or n<&- which is the same) closes fd n. Only exec 0<&- 1<&- 2<&- (or exec <&- >&- 2>&-) would close stdin/stdout/stderr from now on. We close those fds 3, 4, 5 for sudo, in the shell started by script because neither sudo nor cmd need them and they've served their purpose (restore the original remote shell stdin/out/err inside the script session). I don't know why you don't see the output of cmds after here. Commented Dec 3, 2020 at 14:15
  • Thanks for the answer, I am puzzled as well, and will need further testing to figure why this is happening... Commented Dec 7, 2020 at 8:33
  • Stephane, I have noticed I lose stdout after the first error: in your last (script) exemple, instead of sudo cmd, I have: sudo su - someuser -c \"id ; echo $? ; cd /_not_a_dir_ ; echo $? ; id\" (the rest is the same as in your answer) : I only see the output of the first id and the 0, and then nothing else. I don't see the error from the cd /_not_a_dir_ , nor the subsequent echo $?, nor the last id. if I instead : cd / ; I see all (stdout) outputs Commented Feb 2, 2021 at 16:40