简体   繁体   中英

ADB command doesn't read bash variable and returns empty line

I'm currently working on a BASH script to get the path of all apps through ADB in order to pull it afterwards. I get an empty line as the result of the last echo.

If I write a package name directly instead if $pkg, il works. Looks like the $pkg variable is not well "digested" by adb shell pm path

for line in $(adb shell pm list packages -3)
do
    line=$line | tr -d '\r'
    pkg=${line:8}
    path=$(adb shell pm path $pkg | tr -d '\r')
    echo $path
done

Your attempt to strip the carriage return from line is incorrect; as a result, pkg still ends with a carriage return. You need to write

line=$(echo "$line" | tr -d '\r')

However, a simpler method is to use parameter expansion instead:

line=${line%$'\r'}

Further, you shouldn't use a for loop to iterate over the output of a command. Use a while loop with read instead:

while IFS= read line; do
    line=${line%$'\r'}
    pkg=${line:8}
    path=$(adb shell pm path "$pkg" | tr -d '\r')
    echo "$path"
done

You have 2 nested loops - keep the internal one running inside the device and use adb exec-out instead of adb shell . This way you will not have to worry about extra \\r

for p in $(adb exec-out 'for p in $(pm list packages -3); do pm path ${p:8}; done')
do
  adb pull ${p:8}
done

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