简体   繁体   中英

strange adb behaviour if script is piped into bash

I stumpled upon a strange behaviour that I can not explain. I tried to narrow down the problem. I have the following test testscript.sh script:

echo before
adb shell ls
echo after

If I run the script with bash -x testscript.sh , everything works as expected and I get the following output:

+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
+ echo before
before

But if I run the script as piped script with cat testscript.sh | bash -sxcat testscript.sh | bash -sx , I get the following output:

+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor

The last echo after is not executed, and I can not figure out why. The script is running on an Ubuntu server 18.04. The adb is the one from the official Ubuntu package.

$ adb --version
Android Debug Bridge version 1.0.39
Version 1:8.1.0+r23-5~18.04
Installed as /usr/lib/android-sdk/platform-tools/adb
$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)

Please could someone enlight me, what is happening here.

When you run a script with bash scriptname , standard input of all the commands it runs is still connected to the terminal. So adb will read its standard input from the terminal.

When you redirect the input of bash , this redirection is inherited by adb . Unless you use the -n option to adb shell , it will read additional input from standard input and send it to the remote system as possible input for the command you run (it doesn't know that ls doesn't read standard input).

Change it to

adb shell -n ls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM