编写一个服务程序后,通过这三个脚本去编译、执行、停止。
#!/bin/bash
# 1.该行是如果出现错误,就退出脚本执行
set -e
# ---设置基本环境变量start---------------------
export LC_ALL=en_US.UTF-8
# 1. BASH_SOURCE[0]等价于BASH_SOURCE,取得当前脚本文件所在的路径及文件名, eg: my/src/file/build.sh
# 2. %/*代表取从头到最后一个slash之前的所有内容, eg:my/src/file
# 3. #*/代表去取从第一个slash之后的所有内容, eg: src/file/build.sh
export HOME_PATH=`cd ${BASH_SOURCE[0]%/*}; pwd`
export WORK_DIR=$HOME_PATH/build
export TARGET="android"
export ACTION=""
export SETENV_PATH=""
export BUILD_VERSION="debug"
export OUTPUT_PATH=""
#-----设置基本环境变量end---------------------
function process_param()
{
# 1. 命令行解析可参看另一篇博文Linux中shell一些变量解释
TEMP=`getopt -o w:t:a:s:v:o: --long work-dir:,target:,action:,setenv-path:,version:,output: -n 'build.sh' -- "$@"`
# 1.这里没有执行其他脚本,所以默认都是等于0的。
# 2.文件描述符:0代表标准输入,1点标准输出,2代表标准错误
# 3.重定向符号:>, <
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
# 1.对每个传入的参数进行处理
while :; do
case "$1" in
-w|--work-dir) export WORK_DIR=$2; shift 2;;
-t|--target) export TARGET=$2; shift 2;;
-a|--action) export ACTION=$2; shift 2;;
-s|--setenv-path) export SETENV_PATH=$2; shift 2;;
-v|--version) export BUILD_VERSION=$2; shift 2;;
-o|--output) export OUTPUT_PATH=$2; shift 2;;
--) shift; break;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
# 1.{var:-string}:如果var为空,则用string替换; 如果var不为空,就用var替换
# 2.{var:=string}:如果var为空,则用string替换并且赋值给var; 如果var不为空,就用var替换
export WORK_DIR=${WORK_DIR:-$HOME_PATH/build}
export TARGET=${TARGET:-"android"}
export ACTION=${ACTION:-""}
export SETENV_PATH=${SETENV_PATH:-""}
export BUILD_VERSION=${BUILD_VERSION:-"debug"}
export OUTPUT_PATH=${OUTPUT_PATH:-""}
}
function prepare_env()
{
# 1.设置工程环境变量
export MY_CODE=`pwd`
export MY_INTERFACE=$MY_CODE/itf
export MY_PATH=$MY_CODE
# -z:代表如果后面$SETENV_PATH的长度为0,则为真
if [ -z $SETENV_PATH ];then
echo "please set setenv via --setenv-path/-s "
exit 1
fi
}
function build_project()
{
# -d:如果$WORK_DIR存在且为一个目录则为真
if [ ! -d $WORK_DIR ]; then
mkdir -p $WORK_DIR
else
if [ "`ls -A $WORK_DIR`" != "" ];then
echo "rm files in work directory"
rm -r $WORK_DIR/*
fi
fi
# 1.切换到$WORK_DIR目录下
pushd $WORK_DIR
if [ "$TARGET" == "android" ];then
source $SETENV_PATH
toolchain_path=$TOP/toolchain cmake -DCMAKE_TOOLCHAIN_FILE=$TOP/build/aos-toolchain.cmake -DCMAKE_INSTALL_PREFIX=$TOP/out ..
elif [ "$TARGET" == "linux" ];then
echo "start make target linux"
cmake ..
else
echo "please set target as android or linux via -t/--target"
fi
if [ "$ACTION" == "install" ];then
make install
if [ "$OUTPUT_PATH" == "" ];then
return
fi
if [ ! -d $OUTPUT_PATH ]; then
mkdir -p $OUTPUT_PATH
else
if [ "`ls -A $OUTPUT_PATH`" != "" ];then
echo "rm current output file"
rm -r $OUTPUT_PATH/*
fi
fi
cp -r $HOME_PATH/bin/* $OUTPUT_PATH
else
make -j16
fi
}
process_param $*
prepare_env
build_project
#!/bin/bash
cur_path=`pwd`
export LD_LIBRARY_PATH=${LD_LIBARAY_PATH}:${cur_path}/libs/
export MY_MODULE_ENV=${cur_path}
if [ ${ANDROID_DATA} ]
then
# 1.grep -v代表过滤掉包含grep的选项
processId=`ps -ef | grep myModule | grep -v grep | busybox awk '{print $2}'`
else
processId=`ps -ef | grep myModule| grep -v grep | awk '{print $2}'`
fi
if [ $processId ]
then
echo "please stop first!"
exit 1;
fi
./hmi_maintenace &
logcat --pid $processId
#!/bin/bash
if [ ${ANDROID_DATA} ]
then
processId=`ps -ef | grep touchEvent | grep -v grep | busybox awk '{print $2}'`
else
processId=`ps -ef | grep touchEvent | grep -v grep | awk '{print $2}'`
fi
if [ $processId ]
then
kill -9 $processId
fi