0

Try the source block below with android device connected, the output is empty:

#+begin_src bash :results output
  out=$(adb shell date)
  echo $out
#+end_src

But if I change bash to sh, it outputs the date correctly:

#+begin_src sh :results output
  out=$(adb shell date)
  echo $out
#+end_src
    
#+RESULTS:
: Wed Dec 14 13:02:29 CST 2022

What's wrong with the bash code block?

5
  • Try using an ssh command to a remote host that you have access to (ssh remote.host.example.com date) instead of the adb command and see if the shell makes any difference. If not, I would suspect adb but I cannot test that.
    – NickD
    Dec 14, 2022 at 19:30
  • I installed adb and although running the adb shell date command from the command line works fine, I cannot get output from the source block with either sh or bash. Doing a similar ssh command works with both. There is funkiness galore, but I'm not sure where.
    – NickD
    Dec 14, 2022 at 20:00
  • @NickD how to use ssh instead of adb?
    – lucky1928
    Dec 14, 2022 at 22:17
  • You cannot replace adb with ssh, but they have similar properties, so I wanted to test the source blocks using ssh, rather than adb. I just did out=$(ssh <remotehost> date) where <remotehost> is a machine I could ssh into: that works with both sh and bash code blocks. However, neither the sh code block nor the bash code block worked with out=$(adb shell date) - but I don't know why that is the case.
    – NickD
    Dec 15, 2022 at 2:30
  • @NickD Great, it's helpful. and I found after out=$(adb shell date) lien, any echo command not work either. maybe the "adb shell" command has changed output property!
    – lucky1928
    Dec 15, 2022 at 2:45

1 Answer 1

1

I don't quite understand the details yet, but it turns out that for both ssh and adb, you need to prevent them from reading stdin - otherwise, they block waiting for input. The confusing part is that it is not always necessary, but it is necessary in some (most?) cases, so it's better to be safe than sorry.

In the case of ssh, I got the same behavior (blocking) when I tried to ssh to localhost. ssh has a flag -n (or a setting StdinNull) that allows you to tell it to ignore stdin.

It turns out that adb has a similar flag in its shell subcommand, also denoted by -n (surely, not a coincidence).

So the final outcome is that both source blocks (sh and bash) will work fine, as long as you provide the flag:

#+begin_src sh :results output
   echo Start
   out=$(ssh -n localhost date)
   echo $out
   echo Done
#+end_src

#+RESULTS:
: Start
: Thu Dec 15 01:24:31 AM EST 2022
: Done

#+begin_src bash :results output
   echo Start
   out=$(ssh -n localhost date)
   echo $out
   echo Done
#+end_src

#+RESULTS:
: Start
: Thu Dec 15 01:24:36 AM EST 2022
: Done

#+begin_src sh :results output
   echo Start
   out=$(adb shell -n date)
   echo $out
   echo Done
#+end_src

#+RESULTS:
: Start
: Thu Dec 15 01:25:14 EST 2022
: Done

#+begin_src bash :results output
   echo Start
   out=$(adb shell -n date)
   echo $out
   echo Done
#+end_src

#+RESULTS:
: Start
: Thu Dec 15 01:25:19 EST 2022
: Done
1
  • Great, it works now. I try to add '\n' or ';' before but never work, '-n' works!
    – lucky1928
    Dec 15, 2022 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.