linux中的pushd、popd与dirs

最近在项目中,看到一些模块的启动脚本中,有一些 pushd、popd等操作。之前并没有接触过这类命令,但是目测它是与目录相关的,因为都是操作完了目录之后,才能运行启动命令。dirs展示目录栈。什么是目录栈?使用cd命令进入一个目录后,该目录会存放进以个栈中,当前目录永远位于栈顶。dirs 可以用来查
阅读更多

Linux中的特殊符号以及特殊语法

辨别||、&&、;、$*等符号在linux中的含义与或# 将&&前后的两个命令当做一个表达式,如果表达式出错,那么可以认为该表达式为false➜ ~ ls / && datebin boot dev etc home initrd.img
阅读更多

安卓系统应用调试脚本

这是一个对之前写的脚本的记录档案,自己也看不太懂当时的写法了,羞愧。用一个 repo 放这些脚本代码感觉有些浪费,干脆整个文章记录曾经的那段历史吧!

这代码的主要目的就是想少敲点命令,一步到位,做一个快男!

说明 - AndroidROMTools

Android run script tools for debugging system apps or frameworks. This was used by myself when debugging system apps or frameworks. In this project, module names was not given to avoid the project name, but the main structure was reminded.

此脚本主要分成一个入口与两个模块,详细的树状图如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
├── README.md
├── btool
├── modules
│   ├── handleFramework.sh
│   ├── handleXxxx.sh
│   └── handleXxxxxx.sh
└── utils
├── handleSpecificTask.sh
├── init.sh
├── printOperationTip.sh
├── tipWhenNotImplement.sh
└── tipWhenParametersError.sh

2 directories, 10 files

入口

名字叫 btool,为啥起这个怪名字,我也不知道。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
# initialize variables
var1=$1
var2=$2
var3=$3
UTILS_DIR=~/.PersonalTools/utils
MODULES_DIR=~/.PersonalTools/modules

case $var1 in
"init" )
${UTILS_DIR}/init.sh
;;
"log" )
adb logcat -b main \*:E | grep "TAG"
;;
"wxxxx" )
${MODULES_DIR}/handleXXXX.sh $var2 $var3
;;
"txxxx" )
${MODULES_DIR}/handlexxxxxx.sh $var2 $var3
#${UTILS_DIR}/tipWhenNotImplement.sh $var1
;;
"txxxx" )
${UTILS_DIR}/tipWhenNotImplement.sh $var1
;;
"mxxxx" )
${UTILS_DIR}/tipWhenNotImplement.sh $var1
;;
* )
#${UTILS_DIR}/tipWhenParametersError.sh init xxxxxx *xxxxxx* *xxxxxx* *xxxxxx*
if [ "${var1}" == "" ]; then
${UTILS_DIR}/tipWhenParametersError.sh init xxxx *xxxx* *xxxxxx* *xxxxxx*
elif [ -f ${MODULES_DIR}/$var1.sh ]; then
#${UTILS_DIR}/tipWhenParametersError.sh init xxxxxx *xxxxxx* *xxxxxx* *xxxxxx*
${MODULES_DIR}/$var1.sh
else
${UTILS_DIR}/tipWhenParametersError.sh init xxxxxx *txxxxxx* *txxxxxx* *mxxxxxx*
${UTILS_DIR}/tipWhenNotImplement.sh "${var1}"
fi
;;
esac
exit

Utils

  • 初始化:init.sh

    1
    2
    3
    #!/bin/bash
    adb root
    adb wait-for-device remount
  • 打印带颜色的字符:printOperationTip.sh

    1
    2
    3
    4
    5
    6
    #!/bin/bash
    #echo -e "\e[31;43m-----------------------------------------------------------\e[0m"
    echo ""
    echo -e "\e[31;47m\t"$1"\t\e[0m"
    echo ""
    #echo -e "\e[31;43m-----------------------------------------------------------\e[0m"
  • 模块未完成的告警提示:tipWhenNotImplement.sh

    1
    2
    #/bin/bash
    echo -e "\nModule < \e[41m$1\e[0m \e[31m>NOT IMPELEMNT!\e[0m\n"
  • 参数错误提示:tipWhenParametersError.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #/bin/bash
    #clear
    echo ""
    echo -e "\e[31;42mOops! Please give me the \e[0m\e[1;5;40;31mcorrect\e[0m\e[31;42m parameters!!!\e[0m"
    echo ""

    params=""
    cnt=1;
    for i in $@;
    do
    if [ $cnt != 1 ]; then
    params=$params",\e[1;5;31;40m "$i" \e[0m"
    else
    params="\e[1;5;31;40m "$i" \e[0m"
    fi
    cnt=$(($cnt+1))
    done
    if [ $cnt == 1 ]; then
    params="\e[4;31mNONE\e[0m"
    fi
    echo -e "\e[33mavaliable parameters are :\e[0m" $params"\n"
  • 处理特定任务:handleSpecificTask.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    #!/bin/bash

    UTILS_DIR=~/.PersonalTools/utils

    OP_INDEX=$1
    PROJECT_SRC_DIR=$2
    APP_SRC_DIR=$3
    APP_OUT_DIR=$4
    APP_REMOTE_DIR=$5
    APP_PKG_NAME=$6
    PROJECT_LUNCH_IDX=$7

    #for i in $@; do
    #statements
    # echo $i
    #done

    # for compile SnapCamera APK in


    #if [ -f /tmp/btoollog ]; then
    #echo "exist"
    #else
    #echo "/tmp/btoollog not exists"
    #mkdir /tmp/btoollog
    #fi

    function compileModifiedSourceCode(){
    ${UTILS_DIR}/printOperationTip.sh "<Prepare to Compile>"
    cd ${PROJECT_SRC_DIR}
    pwd
    source build/envsetup.sh
    lunch ${PROJECT_LUNCH_IDX}
    cd ${APP_SRC_DIR}
    pwd
    mm
    }

    function makeSureCloseCompletelyAndReopen(){
    ${UTILS_DIR}/printOperationTip.sh "<Prepare to force-stop APK>"

    echo -e "\n\e[32m\ttry to stop app\e[0m"
    adb shell am force-stop ${APP_PKG_NAME}
    echo -e ""

    echo -e "\n\e[32m\ttry to start app\e[0m"
    adb shell am start -n ${APP_PKG_NAME}
    echo -e ""
    }

    function pushCompiledAPK2Phone(){
    ${UTILS_DIR}/printOperationTip.sh "<Prepare to Push APK>"
    cd ${APP_OUT_DIR}
    #pwd
    #echo "${APP_OUT_DIR}"
    adb push . ${APP_REMOTE_DIR}
    #adb push oat/arm64/$appname.odex /system/priv-app/$appname/oat/arm64/$appname.odex
    if [ $? -eq 0 ]; then
    echo -e "\n\e[32m\tpush app to phone successfully\e[0m"
    fi
    echo -e ""
    }

    function checkPushedFilesStatus(){
    ${UTILS_DIR}/printOperationTip.sh "<Prepare to Check Push Result>"
    adb shell ls -l ${APP_REMOTE_DIR} ${APP_REMOTE_DIR}/oat/arm64/
    if [ $? -eq 0 ]; then
    echo -e "\n\e[32m\tconnect to phone successfully\e[0m"
    fi
    echo -e ""
    }

    case $1 in
    "0" )
    compileModifiedSourceCode
    pushCompiledAPK2Phone
    makeSureCloseCompletelyAndReopen
    checkPushedFilesStatus
    ;;
    "1" )
    compileModifiedSourceCode
    ;;
    "2" )
    pushCompiledAPK2Phone
    ;;
    "3" )
    makeSureCloseCompletelyAndReopen
    ;;
    "4" )
    checkPushedFilesStatus
    ;;
    "subl" )
    subl ${APP_SRC_DIR}
    ;;
    "src" )
    echo ${APP_SRC_DIR}
    ;;
    * )
    ${UTILS_DIR}/tipWhenParametersError.sh 0 1 2 3 4 subl src
    echo -e "\t\e[1;4;5;31;40m 0 \e[0m\t : \e[33m1 -> 2 -> 3 -> 4\e[0m"
    echo -e "\t\e[1;4;5;31;40m 1 \e[0m\t : \e[33mCompile Modified Source Code\e[0m"
    echo -e "\t\e[1;4;5;31;40m 2 \e[0m\t : \e[33mPush Compiled APK 2 Phone\e[0m"
    echo -e "\t\e[1;4;5;31;40m 3 \e[0m\t : \e[33mClose App\e[0m"
    echo -e "\t\e[1;4;5;31;40m 4 \e[0m\t : \e[33mShow App Info\e[0m"
    echo -e "\t\e[1;4;5;31;40m subl \e[0m\t : \e[33mOpen App Src With Sublime Text\e[0m\n"
    echo -e "\t\e[1;4;5;31;40m src \e[0m\t : \e[33mPrint SRC path\e[0m\n"
    ;;
    esac

模块

  • Android Framework 处理:handleFramework.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #!/bin/bash
    SRC_HOME=/home/ckt/work/xxxxxx/msm8909_go/LINUX/android/frameworks/base
    OUT_HOME=/home/ckt/work/xxxxxx/msm8909_go/LINUX/android/out/target/product/msm8909go/system/framework

    cd /home/ckt/work/xxxxxx/msm8909_go/LINUX/android/

    source build/envsetup.sh
    lunch 29
    make framework
    make snod

    adb push ${OUT_HOME}/arm/boot.oat /system/framework/arm/
    adb push ${OUT_HOME}/arm/boot.art /system/framework/arm/

    adb push ${OUT_HOME}/framework.jar /system/framework/
    adb push ${OUT_HOME}/framework-res.apk /system/framework/

    adb reboot bootloader
  • 桌面时钟:handleXxxx.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #/bin/bash

    UTILS_DIR=~/.PersonalTools/utils

    PROJECT_DIR="/home/ckt/work/android8"
    PROJECT_SRC_DIR=${PROJECT_DIR}
    PROJECT_OUT_DIR=${PROJECT_DIR}/out/target/product/msm8953_64
    PROJECT_LUNCH_IDX=39

    DESKCLOCK_SRC_DIR=${PROJECT_SRC_DIR}/packages/apps/DeskClock/
    DESKCLOCK_OUT_DIR=${PROJECT_OUT_DIR}/system/app/DeskClock/
    DESKCLOCK_PKG_NAME=com.android.deskclock
    DESKCLOCK_REMOTE_DIR=/system/app/DeskClock


    case $1 in
    "deskclock" )
    ${UTILS_DIR}/handleSpecificTask.sh $2 ${PROJECT_SRC_DIR} ${DESKCLOCK_SRC_DIR} ${DESKCLOCK_OUT_DIR} ${DESKCLOCK_REMOTE_DIR} ${DESKCLOCK_PKG_NAME} ${PROJECT_LUNCH_IDX}
    ;;
    * )
    ${UTILS_DIR}/tipWhenParametersError.sh deskclock
    ;;
    esac
  • 多媒体应用:handleXxxxxx.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    #!/bin/bash

    UTILS_DIR=~/.PersonalTools/utils

    PROJECT_DIR="/home/ckt/work/xxxxxxx/LINUX/android"
    PROJECT_SRC_DIR="${PROJECT_DIR}"
    PROJECT_OUT_DIR="${PROJECT_DIR}/out/target/product/msm8909go"
    PROJECT_LUNCH_IDX=29

    SOUNDRECORDER_SRC_DIR="${PROJECT_SRC_DIR}/packages/apps/SoundRecorder/"
    SOUNDRECORDER_OUT_DIR="${PROJECT_OUT_DIR}/system/priv-app/SoundRecorder/"
    SOUNDRECORDER_PKG_NAME=com.android.soundrecorder
    SOUNDRECORDER_REMOTE_DIR=/system/priv-app/SoundRecorder

    GALLERY_SRC_DIR="${PROJECT_SRC_DIR}/packages/apps/Gallery2/"
    GALLERY_OUT_DIR="${PROJECT_OUT_DIR}/system/app/Gallery2/"
    GALLERY_PKG_NAME=com.android.gallery3d
    GALLERY_REMOTE_DIR=/system/app/Gallery2

    SNAPDRAGONMUSIC_SRC_DIR="${PROJECT_SRC_DIR}/packages/apps/Music/"
    SNAPDRAGONMUSIC_OUT_DIR="${PROJECT_OUT_DIR}/system/app/Music/"
    SNAPDRAGONMUSIC_PKG_NAME=com.android.music
    SNAPDRAGONMUSIC_REMOTE_DIR=/system/app/Music

    CLOCK_SRC_DIR="${PROJECT_SRC_DIR}/packages/apps/DeskClock"
    CLOCK_OUT_DIR="${PROJECT_OUT_DIR}/system/app/DeskClock"
    CLOCK_PKG_NAME=com.android.deskclock
    CLOCK_REMOTE_DIR=/system/app/DeskClock

    case $1 in
    "soundrecorder" )
    ${UTILS_DIR}/handleSpecificTask.sh $2 ${PROJECT_SRC_DIR} ${SOUNDRECORDER_SRC_DIR} ${SOUNDRECORDER_OUT_DIR} ${SOUNDRECORDER_REMOTE_DIR} ${SOUNDRECORDER_PKG_NAME} ${PROJECT_LUNCH_IDX}
    ;;
    "gallery" )
    ${UTILS_DIR}/handleSpecificTask.sh $2 ${PROJECT_SRC_DIR} ${GALLERY_SRC_DIR} ${GALLERY_OUT_DIR} ${GALLERY_REMOTE_DIR} ${GALLERY_PKG_NAME} ${PROJECT_LUNCH_IDX}
    ;;
    "music" )
    ${UTILS_DIR}/handleSpecificTask.sh $2 ${PROJECT_SRC_DIR} ${SNAPDRAGONMUSIC_SRC_DIR} ${SNAPDRAGONMUSIC_OUT_DIR} ${SNAPDRAGONMUSIC_REMOTE_DIR} ${SNAPDRAGONMUSIC_PKG_NAME} ${PROJECT_LUNCH_IDX}
    ;;
    "clock" )
    ${UTILS_DIR}/handleSpecificTask.sh $2 ${PROJECT_SRC_DIR} ${CLOCK_SRC_DIR} ${CLOCK_OUT_DIR} ${CLOCK_REMOTE_DIR} ${CLOCK_PKG_NAME} ${PROJECT_LUNCH_IDX}
    ;;
    * )
    ${UTILS_DIR}/tipWhenParametersError.sh soundrecorder gallery music clock
    ;;
    esac

目前这些脚本对我来说,已无实际用途,不能保证准确性,只是做一个纪念,纪念那段无知却在挣扎的时光。

基础Shell脚本大法

shell中的变量变量的设置规则读取变量的时候,$PATH 与 $是等同的双引号内的特殊符号如$等,可以保持原有的特性。单引号内的特殊符号则仅为一般字符(纯文本)`命令` 与 $(命令)是等同的变量如何加1语法((i=i+1));let i=i+1;x=$(( $x + 1 ))x=`expr $x
阅读更多