Ubuntu shell脚本笔记

这是一个用于Android系统工程师的自动化构建脚本,主要功能包括编译特定模块、清理项目、构建OTAPackage和打包镜像。脚本支持编译不同类型的模块(如APPS、SHARED_LIBRARIES、JAVA_LIBRARIES),并能根据模块类型推断输出路径,通过adb进行文件推送。此外,脚本还包含日志打印功能和用户交互提示,便于调试和使用。
摘要由CSDN通过智能技术生成

查找当前目录下以及二级目录以xxx开头的文件夹并且追加双引号输出

find . -mindepth 1 -maxdepth 2 -type d -name 'xxx-*' -printf '"%p",'

Android P 自动编译模块脚本 待完善

#!/bin/bash
# Author:@tuliyuan
# Address: 975150240@qq.com
# Description: This Script is Worked for Android System Engineer
# but I haven't completed yet in this Company, 
# If you need to contact me, please send an email to my mailbox,also you can fixed by yourself
# I have provided the relevant reference ideas, Come on!
# TODO parse Android.mk or Android.bp ,TO BE DONE
# Step 1  get Compile Module Name 
# Step 2  using gomod to enter Module Directory
# Step 3  test if Android.mk or Android.bp exist
# Step 4  parse their out directory path  
# ####Android.mk parse rules 
# LOCAL_PROPRIETARY_MODULE := true -->/vendor
# LOCAL_PRIVILEGED_MODULE := true -->priv-app
# LOCAL_BUILT_MODULE_STEM := package.apk --> means it's a application
# LOCAL_PRODUCT_MODULE := true -->/product
# LOCAL_REQUIRED_MODULES := SystemUIWithLegacyRecents -->need module SystemUIWithLegacyRecents
# LOCAL_PREBUILT_JNI_LIBS := lib/x86/libxx.so \ -->need push target output lib too
# ##### Android.bp parse rules 
# privileged: true; --> it's means we will output module(maybe a application) to priv-app directory
# dex_preopt: false; --> it's means we will never using dex preopt ,so we can push output files only,no need push /oat/arm anymore
# proprietary: true; --> target out vendor/
# product_specific: true; --> target out product /	
# product_specific: "hw" --> lib/hw/
# Step 5  after parse , you can get correct output path,then your can push them to Android device specific directory
source build/envsetup.sh > /dev/null
TARGET_BUILD_PRODUCT=`get_build_var TARGET_PRODUCT`
main() {
   local module=$1

   echo "Starting compile $module"
   if [ "$module" = "services" ]; then
	make $module
	if [ $? -eq 0 ]; then
	   echo -e "\033[36mCompile $module Success\033[0m"
	else
	   echo -e "\033[32mCompile $module Failed\033[0m"
	   exit 1
	fi
	adb root
	adb remount
	adb push out/target/product/$TARGET_BUILD_PRODUCT/system/framework/services.jar* /system/framework/
	adb push out/target/product/$TARGET_BUILD_PRODUCT/system/framework/oat/arm/services.* /system/framework/oat/arm/
	echo "Push $module success, we will reboot immediately"
	do_reboot
   elif [ "$module" = "Settings" ]; then
	make $module
	if [ $? -eq 0 ]; then
	   echo -e "\033[36mCompile $module Success\033[0m"
	else
	   echo -e "\033[32mCompile $module Failed\033[0m"
	   exit 1
	fi
	adb root
	adb remount
	adb push out/target/product/$TARGET_BUILD_PRODUCT/product/priv-app/Settings /product/priv-app/
	echo "Push $module success, we will reboot immediately"
	do_reboot
   elif [ "$module" = "Settings" ]; then
	make $module
	if [ $? -eq 0 ]; then
	   echo -e "\033[36mCompile $module Success\033[0m"
	else
	   echo -e "\033[32mCompile $module Failed\033[0m"
	   exit 1
	fi
	adb root
	adb remount
	adb push out/target/product/$TARGET_BUILD_PRODUCT/product/priv-app/$module /product/priv-app/
	echo "Push $module success, we will reboot immediately"
	do_reboot
   elif [ "$module" = "VProjectorLauncher" ]; then
	make $module
	if [ $? -eq 0 ]; then
	   echo -e "\033[36mCompile $module Success\033[0m"
	else
	   echo -e "\033[32mCompile $module Failed\033[0m"
	   exit 1
	fi
	adb root
	adb remount
	adb push out/target/product/$TARGET_BUILD_PRODUCT/product/priv-app/$module /product/priv-app/
	echo "Push $module success, we will reboot immediately"
	do_reboot
   elif [ "$module" = "WifiDisplay" ]; then
	make $module
	if [ $? -eq 0 ]; then
	   echo -e "\033[36mCompile $module Success\033[0m"
	else
	   echo -e "\033[32mCompile $module Failed\033[0m"
	   exit 1
	fi
	adb root
	adb remount
	adb push out/target/product/$TARGET_BUILD_PRODUCT/product/app/$module /product/app/
	echo "Push $module success, we will reboot immediately"
	do_reboot
   else 
       	echo "Unknow Module! You should add Compile module. Such as:"
	echo "$0 Settings"
	echo "$0 SystemUI"	
	echo "$0 Launcher3"
	echo "$0 services"
   
   fi
	

   
}
do_reboot(){
	if read -t 5 -p "We will reboot Android Device, Stop Reboot? : " ret
	then
	   if [ "$ret" = "y" ] || [ "$ret" = "Y" ]|| [ "$ret" = "yes" ] || [ "$ret" = "Yes" ] || [ "$ret" = "YES" ] ;then
		 echo -e "\033[32minterput Reboot by youself!\033[0m "
	   else
		adb reboot
	   fi
	else
	        echo -e "\033[36m \r\nAndroid Device Rebooting\033[0m "
		adb reboot
	fi
}
main $1

awk


local str=`grep 'BOARD_WIFI_VENDOR :=' device/softwinner/venus-a1/BoardConfig.mk | awk '/^#/;{split($0,a,":="); print a[2]}'`
=============================
// $0 是指文本输出内容,这里是指grep 'BOARD_WIFI_VENDOR :=' device/softwinner/venus-a1/BoardConfig.mk 的输出内容,$1是第一列
=============================
// awk '/^#/;   {gsub(" ","",$0)};    {split($0,a,":=");  print a[2]}'
=============================
''里面的第一段 /^#/这个意思是指删除以#开头的行
=============================
''里面的第二段{gsub(" ","",$0)}; 是指删除空格副, 将" " 改为了""
=============================
''里面的第三段 {split($0,a,":=");  print a[2]}  这里是指以:=作为拆分符,且将拆分结果赋值给数组a ,然后输出数组
的第二个字符

awk里面有很多内置函数,可以百度查询

grep

grep "abc" file1 在file1中查找 abc字符串

grep "abc" -rn file1 在file1中查找abc字符串并输出所在行

grep '^BOARD_WIFI_VENDOR'  查询以BOARD_WIFI_VENDOR开头的

grep '$BOARD_WIFI_VENDOR'  查询以BOARD_WIFI_VENDOR结尾的

Sed

sed -i 's/a/bb/g'  file 将file中的a替换为bb 并输出log信息

sed -i  '/;/d' file 删除以;开头的行

完善的ubuntuAnroid源码调试脚本

#!/bin/bash
source build/envsetup.sh > /dev/null
LOG_TAG="[Tuliyuan:CustomDebugScript]"
# 32 绿色
# 31 红色
# 33 棕色
# 34 蓝色
# 35 紫色
# 36 兰绿色
# 37 白色
function printMainLog()
{
 local LOG_MAIN="\033[37m$1\033[0m"
 echo -e "${LOG_TAG} ${LOG_MAIN}"
}

function printErrorLog()
{
 local LOG_ERROR="\033[31m$1\033[0m"
 echo -e "${LOG_TAG} ${LOG_ERROR}"
}
function printWarnLog()
{
 local LOG_ERROR="\033[33m$1\033[0m"
 echo -e "${LOG_TAG} ${LOG_ERROR}"
}
function printInfoLog()
{
 local LOG_INFO="\033[36m$1\033[0m"
 echo -e "${LOG_TAG} ${LOG_INFO}"
}
function printSuccessLog()
{
 local LOG_SUCCESS="\033[32m$1\033[0m"
 echo -e "${LOG_TAG} ${LOG_SUCCESS}"
}
function usage()
{
   echo "USAGE: [-m module_name] [-a] [-c] [-o] [-p]"
   echo "WHERE: -m = build module"
   echo "       -a = build android"
   echo "       -c = clean All build"
   echo "       -o = build otapackage"
   echo "       -p = pack img"
   exit 1
}

PAR_BUILD_ANDROID=false
PAR_BUILD_CLEAN=false
PAR_BUILD_OTAPACKAGE=false
PAR_PACK_BIN=false
PAR_BUILD_MOUDLE=false
PAR_BUILD_MOUDLE_NAME=""
moduleType=""
PAR_BUILD_MOUDLE_APP=false
PAR_BUILD_MOUDLE_SO=false
PAR_BUILD_MOUDLE_JAR=false
LOCAL_DIR_NAME=`gettop`
local_company="visionmagic"
local_product_target=`get_build_var TARGET_PRODUCT`
local_android_version=`get_build_var PLATFORM_VERSION`
local_android_arch=`get_build_var TARGET_ARCH`
local_version_key=""
local_cpu_manufacture="mtk"
local_prop_custom_verison_num="ro.custom.version.release"
local_target_out="";

local_out_parition="system/"
local_out_target_priv="app/"
local_out_app_path="system/app"

do_main(){
    if [ "${local_product_target}" = "aosp_arm" ]; then
		printInfoLog "You need choose lunch combo first!! Using lunch xxx-userdebug To Complete Enviroment Build"
		exit -1;
	fi
#以full_开头的正则表达式匹配
	if [[ "$local_product_target" =~ ^full_* ]]; then
    	#printInfoLog "This is Mtk Project"
    	local_product_target=`echo ${local_product_target} | sed 's/full_//g'`
	fi
	if [[ "$1" == "" ]]; then
    	   usage
    	   exit 1;
	fi
	local debug_module_path=""
	while getopts "m:acop" OPT;
	do
        case $OPT in
        	m)
        	debug_module_path="$(pathmod $OPTARG)"
        	parse_moudle $OPTARG
        	PAR_BUILD_MOUDLE_NAME="$OPTARG"
            	printMainLog "Will Debug Module $OPTARG "
            	PAR_BUILD_MOUDLE=true
            	;;
        	a)
            	printMainLog "Will build android"
            	PAR_BUILD_ANDROID=true
            	;;
        	c)
            	printMainLog "Will clean All"
            	PAR_BUILD_CLEAN=true
            	;;
        	o)
            	printMainLog "Will build otapackage"
            	PAR_BUILD_OTAPACKAGE=true
            	;;
        	p)
            	printMainLog "Will build pack bin"
            	PAR_PACK_BIN=true
            	;;
        	?)
            	usage
            	;;
    	esac
    done
 
    if [ ! -d "out/target/product/${local_product_target}" ]; then
        printErrorLog "There was no out dir!!!!You Need Compile Project First!"
        exit -1;
    fi
    local_target_out="out/target/product/${local_product_target}"
    if [ $PAR_BUILD_MOUDLE == true ]; then
       printMainLog "will compile Module $2"
       debug_module $2 $debug_module_path
       return 0;
    fi
  
    if [ $PAR_BUILD_CLEAN == true ]; then
       printMainLog "will clean project"
       make clean
    fi
  
    if [ $PAR_BUILD_ANDROID == true ]; then
       if [ -d "./device/rockchip" ]; then
       	    if [ ! $PAR_BUILD_OTAPACKAGE == true ]; then
       	    	 printMainLog "will make rockchip project without otapackage"
       	         make installclean;./build.sh -Aup
       	    fi
       else
           printMainLog "will make android"
       	   make installclean;make -j8 2>&1 | tee build.log
       fi

    fi
    
    if [ $PAR_BUILD_OTAPACKAGE == true ]; then
       if [ -d "./device/rockchip" ]; then
       	   printMainLog "will make rockchip android with otapackage"
       	   ./build.sh -Aoup
       else
           printMainLog "will make android with otapackage "
       	   make -j8 otapackage 2>&1 | tee build_ota.log
       fi
    fi
  
    if [ $PAR_PACK_BIN == true ]; then
        if [  -d "./device/rockchip" ]; then
            printMainLog "Rockchip command has already pack image into $pwd/rockdev"
        else
            versionNumber=`grep -r "$local_prop_custom_verison_num" out/target/product/${local_product_target}/vendor/build.prop | sed 's/'$local_prop_custom_verison_num='/''/g'`
            printMainLog "$versionNumber"
            croot
            local binPath="$LOCAL_DIR_NAME/${local_product_target}_bin_$(date +%Y%m%d)"
            if [ "$versionNumber" = "" ]; then
  	          mkdir -p "$binPath"
  	          cp -ra out/target/product/${local_product_target}/*.img $binPath
  	          cp -ra out/target/product/${local_product_target}/*.bin $binPath
  	          cp -ra out/target/product/${local_product_target}/*_Android_scatter.* $binPath
  	          if [ $PAR_BUILD_OTAPACKAGE == true ]; then
	              cp -ra out/target/product/${local_product_target}/full_${local_product_target}-ota-*.zip $binPath/${versionNumber}_OTA.zip
  	              cp -ra out/target/product/${local_product_target}/obj/PACKAGING/target_files_intermediates/full_${local_product_target}-target_files-*.zip $binPath/localUpdate.zip
  	          fi
  	          printSuccessLog "CopyFile Success!!!"
            else
    	          binPath="$LOCAL_DIR_NAME/${versionNumber}_bin"
  	          mkdir -p "$binPath"
  	          cp -ra out/target/product/${local_product_target}/*.img $binPath
  	          cp -ra out/target/product/${local_product_target}/*.bin $binPath
  	          cp -ra out/target/product/${local_product_target}/*_Android_scatter.* $binPath
  	          if [ $PAR_BUILD_OTAPACKAGE == true ]; then
	             cp -ra out/target/product/${local_product_target}/full_${local_product_target}-ota-*.zip $binPath/${versionNumber}_OTA.zip
  	             cp -ra out/target/product/${local_product_target}/obj/PACKAGING/target_files_intermediates/full_${local_product_target}-target_files-*.zip $binPath/${versionNumber}_FOTA.zip
  	          fi
  	          printSuccessLog "CopyFile Success!!!"
            fi 
        fi
    fi

  
}
function pathmod() {
    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
        echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
        return 1
    fi

    if [[ $# -ne 1 ]]; then
        echo "usage: pathmod <module>" >&2
        return 1
    fi

    if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
        echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
        refreshmod || return 1
    fi
    local relpath=$(python -c "import json, os
module = '$1'
module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
if module not in module_info:
    exit(1)
print(module_info[module]['path'][0])" 2>/dev/null)

    if [ -z "$relpath" ]; then
        echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
        return 1
    else
        echo "$relpath"
    fi
    
}
function refreshmod() {
    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
        echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
        return 1
    fi

    echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2

    # for the output of the next command
    mkdir -p $ANDROID_PRODUCT_OUT || return 1

    # Note, can't use absolute path because of the way make works.
    m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
        > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
}

function parse_moudle(){
if [ ! "$ANDROID_PRODUCT_OUT" ]; then
        echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
        return 1
    fi

    if [[ $# -ne 1 ]]; then
        echo "usage: pathmod <module>" >&2
        return 1
    fi

    if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
        echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
        refreshmod || return 1
    fi

    moduleType=$(python -c "import json, os
module = '$1'
module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
if module not in module_info:
    exit(1)
print(module_info[module]['class'][0])" 2>/dev/null)


    if [ "${moduleType}" == "APPS" ]; then
    	PAR_BUILD_MOUDLE_APP=true
    	printInfoLog "Compile Module $1 is Application *.apk "
    elif  [ "${moduleType}" == "SHARED_LIBRARIES" ]; then
    	PAR_BUILD_MOUDLE_SO=true
    	printInfoLog "Compile Module $1 is SharedLibraries *.so"
    elif [ "${moduleType}" == "JAVA_LIBRARIES" ]; then
   	PAR_BUILD_MOUDLE_JAR=true
   	printInfoLog "Compile Module $1 is JavaLibraries *.jar "
    else
        printInfoLog "Compile Module $1 is ${moduleType}"
    fi
}

function debug_module(){
   local module_path=$2
   local module_name=$1
   croot
   if [ $local_android_version -gt 8 ]; then
        printInfoLog "Android Version is ${local_android_version}, Compile Command is : make ${module_name}"    
        make ${module_name}
     else
        printWarnLog "Android Version is ${local_android_version}, Compile Command is : mmm ${module_path}"
        mmm ${module_path}
     fi
     
     if [ $? -eq 1 ]; then
  	 printErrorLog "Compile Failed. Please Recheck Compile result"
      	 return -1;
     fi
     adb root
     if [ $? -eq 1 ]; then
      	 printErrorLog "Adb root Failed. Please Recheck USB Device Connect State"
      	 return 1;
     fi
     sleep 1s
     adb remount
     local outPutFilePath=$(python -c "import json, os
module = '${module_name}'
module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
if module not in module_info:
  exit(1)
for val in module_info[module]['installed']:
  print(val)" 2>/dev/null)
     printInfoLog "\nNeed push following files: \n$outPutFilePath"
     if [ "$outPutFilePath" == "" ];then
     	  printErrorLog "No Installed Path, Maybe is a static library,Module Type is ${moduleType}"
     	  exit 1;
     fi
     echo "$outPutFilePath" > tempFilePush.txt

     while read eachline
        do
          #printMainLog "LINE: $eachline"
          #裁剪box右侧的所有字符
	  target_file=${eachline#*$local_product_target} 
	  printMainLog "push to ${target_file}"
	  adb push $eachline  ${target_file}
	  done < tempFilePush.txt 
     rm -rf tempFilePush.txt
     do_reboot
}


do_reboot(){
	if read -t 3 -p $'We will reboot Android Device in 3 seconds, Press Enter Key to Stop Reboot? ' ret
	then
	   if [ -z "$ret" ];then
		printErrorLog "interput Reboot by youself!"
	   fi
	else
	        echo -e "\n${LOG_TAG}\033[32m Android Device Rebooting ! \033[0m"
		adb reboot
	fi
}

#获取配置文件值

getProjectKey(){
	local file=$1
	local key=$2
	if [ -f "${file}" ]; then
	     #local value=$(grep -v "^\s*#" ${file} | grep -w ${key} | tail -1 | sed 's#^[^=]*=\(.*\)$#\1#g' | sed 's#[[:space:]]##g')
	     local value=`grep -v "^\s*#" ${file} | grep -w ${key} | tail -1 | sed 's#^[^=]*=\(.*\)$#\1#g' | sed 's#[[:space:]]##g'`
	fi
	echo ${value}
}


do_main $1 $2
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值