shell案例
shell案例
批量更新所有仓库代码、切换分支、创建分支
将当前.sh 放到要操作的仓库平级,输入下面指令: 1 批量切换分支 2 批量创建新分支 默认:更新所有分支(其它输入或者直接回车)
获取当前目录下所有 git 仓库的 branch name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/zsh
function gitLog(){
curr_dir="."
# dirs=$(ls $curr_dir)
# echo $dirs
for element in `ls $curr_dir`
do
dir_or_file=$curr_dir"/"$element
if [ -d $dir_or_file ]
then
cd $dir_or_file
local_branch=$(git rev-parse --abbrev-ref HEAD)
echo "$element $local_branch"
cd ..
fi
done
}
BASEDIR=$(cd "$(dirname "$0")";pwd)
cd $BASEDIR
gitLog
Shell 案例
读取 applicationId
读取 applicationId 并启动某个 App
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
#!/bin/zsh
CUR_DIR=$(cd `dirname $0` && pwd -P)
echo 'relocation to project root dir'
# 读取gradle.properties(兼容不同在不同目录执行sh路径找不到问题,默认在remix项目根目录执行))
## 1. 项目根目录 sh sh/launch.sh 可选配置REMIX_HOME环境变量为remix项目根目录
## 2. cd到sh目录 ./launch.sh 必须配置REMIX_HOME环境变量为remix项目根目录
echo "REMIX_HOME=$REMIX_HOME"
gradle_properties_path=""
if [ "$REMIX_HOME" = "" ]; then
gradle_properties_path="gradle.properties"
else
gradle_properties_path="${REMIX_HOME}/gradle.properties"
fi
echo "gradle_properties_path=$gradle_properties_path"
# 读取applicationId的值
line=$(sed -n '/mj_applicationId/=' "$gradle_properties_path")
appId=$(sed -n "$line"'p' "$gradle_properties_path")
appId=${appId#*\=}
appId=${appId%,*}
length=${#appId}
# shellcheck disable=SC2003,SC2006,SC2086
last=`expr $length`
appId=${appId: 0: $last}
main="$appId/qsbk.app.remix.ui.SplashActivity"
echo "applicationId = $main"
# launch multiple mobiles SplashActivity
# shellcheck disable=SC2207
array=($(adb devices | tail -n +2 | cut -sf 1 | xargs -I {}))
for item in ${array[*]}
do
# 安装apk忽略掉wifi debug连接的设备
if [[ $item != *":5555"* ]]; then
adb -s $item shell am start -n $main -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
fi
done
通过 shell 修改 build.gradle 配置文件
需求
自动化打包前,修改版本号
思路如下
- 获取要修改字符串在 build.gradle 配置文件的所在行
- 整行删除旧字符串
- 新将新字符串写入配置文件
技术点
- sed 读写配置文件
- 获取指定字符串所在行
- 将字符串写入配置文件指定位置 (指定行)
- 拼接字符串,字符串包含特殊符号,双隐号,单隐号,转义符号的使用
实战
build.gradle 配置文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defaultConfig {
applicationId "com.wawj.app.t"
// applicationId "com.wawj.app.tt"
minSdkVersion 16
targetSdkVersion 26
versionCode 20191226
versionName "8.8.8" #要修改的字符串
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunn$
multiDexEnabled true
// resConfigs "en", "de", "fr", "it"git
// resConfigs "nodpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"
ndk {
//极光推送--选择要添加的对应cpu类型的.so库
abiFilters 'xx', 'xx-xxxa', 'xxx'
}
}
脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd AppFrameWork/app/ #build.gradle配置文件所在目录
VERSION=" versionName \"$Version\"" #拼接修改后的字符串
#获取行数,并保存到变量
line=$(sed -n '/versionName/=' build.gradle) #获取要修改的字符串所在行,并将它保存到变量line
echo "版本号所在行$line"
#插入内容的位置
newline=$(expr $line - 1)#计算要插入行的的行号,因后面使用 追加(注意不是插入)的方式将要修改的字符串 追加所在行,所以里这里要将line-1,写入字符串的位置才是准确的
#删除指定行
sed -i "$line d" build.gradle
#在指定行追加要字符串,并保存修改( -i表示保存修改)
sed -i "$newline a\\$VERSION" build.gradle
echo "修改的版本号是$VERSION"
本文由作者按照 CC BY 4.0 进行授权