1

Motivation: I often open a remote shell on android devices (using adb/ Android debug bridge), and want to write scripts on my machine (not android devices, because I have multiple) to help me when i first connect into the shell.

For example, I might want to change directory into a specific folder, echo "cd /storage/self/primary/Download; mkdir bobby" | adb shell, however the stdin in immediately closed once the code is executed, and the shell is immediately closed too. I've added mkdir bobby just to test that the line is actually executed, and it is.

For example, the shell will automatically close in 5 seconds when I run echo "cd /storage/self/primary/Download; mkdir bobby; sleep 5" | adb shell. Unfortunately, a heredoc doesn't help, because it will also close the shell once the heredoc delimiter is found.

Question: How do I keep stdin connected to terminal after piping a command into it, so that I can type into it. This is not a challenge with ssh, because you can directly pass an optional command.


Apologies if this question has already asked, I wasn't sure how to phrase it.

8
  • Try adding ' /bin/sh -i `
    – Ángel
    Commented Nov 19, 2020 at 23:52
  • Hi Angel, I guess you mean echo "cd /storage/self/primary/Download; /bin/sh -i" | adb shell, but unfortunately that didn't work. Commented Nov 19, 2020 at 23:59
  • Yes, I meant that. What did it do?
    – Ángel
    Commented Nov 20, 2020 at 0:03
  • It immediately closed the shell after completion Commented Nov 20, 2020 at 0:06
  • 1
    I think this would be difficult in normal interactive shells if it was not for initialization files (such as .bashrc). I'm afraidadb doesn't have anything of the kind?
    – Quasímodo
    Commented Nov 20, 2020 at 0:36

1 Answer 1

2

To keep the pipe open, don't let it close. You may do this by adding cat to the left hand side in the pipe:

{ echo "cd /storage/self/primary/Download; mkdir bobby"; cat; } | adb shell

The cat process will execute after echo, and have its standard output connected to the adb shell command, while its standard input reads from the terminal (where you may type).

As soon as cat terminates (by you pressing either Ctrl+C to interrupt, or Ctrl+D to signal end of input) the pipeline will terminate.

Note that this may not give you a full interactive shell with a prompt and command line completion or whatever other interactive features the adb shell command provides normally, but will let you send commands to the right hand side of the pipe.

This is similar to what's explained in What is meant by "keeping the pipe open"?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .