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? Commented Jul 10, 2017 at 10:41
  • line=$(echo "$line" | tr -d '\r'). Commented Jul 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help ! I'll keep the while vs. for in mind.
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.