1 # Copyright (C) 2010 The Android Open Source Project 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 16 # Extract the pid of a given package name. This assumes that the 17 # input is the product of 'adb shell ps' and that the PACKAGE variable 18 # has been initialized to the package's name. In other words, this should 19 # be used as: 20 # 21 # adb shell ps | awk -f <this-script> -v PACKAGE=<name> 22 # 23 # The printed value will be 0 if the package is not found. 24 # 25 # NOTE: For some reason, simply using $9 == PACKAGE does not work 26 # with this script, so use pattern matching instead. 27 # 28 29 BEGIN { 30 PID=0 31 FS=" " 32 # Need to escape the dots in the package name 33 # 34 # The first argument is the regular expression '\.' 35 # corresponding to a single dot character. The second 36 # argument is the replacement string, which will be '\.' 37 # for every input dot. Finally, we need to escape each 38 # backslash in the Awk strings. 39 # 40 gsub("\\.","\\.",PACKAGE) 41 } 42 43 # We use the fact that the 9th column of the 'ps' output 44 # contains the package name, while the 2nd one contains the pid 45 # 46 $9 ~ PACKAGE { 47 PID=$2 48 } 49 50 END { 51 print PID 52 } 53