0

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 
2
  • Can you post the output of adb shell pm list packages -3 and what is your action out of it?
    – Inian
    CommentedJul 10, 2017 at 10:41
  • line=$(echo "$line" | tr -d '\r').
    – chepner
    CommentedJul 10, 2017 at 11:07

2 Answers 2

1

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 
1
  • Thanks for your help ! I'll keep the while vs. for in mind.CommentedJul 10, 2017 at 12:49
0

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 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.