嵌入式linux应用开发之常用shell脚本总结(下)

简介: 嵌入式linux应用开发之常用shell脚本总结

简化常用指令操作,写个脚本


比如设置环境变量和scp往机器中拷贝文件,整个脚本简化下操作,省得重复敲不少命令


#!/bin/bash
source /opt/myir-imx-fb-qt5/4.1.15-2.0.1/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
scp myapp root@192.168.79.1:/app/my_app/bin


或者是启动应用,手动执行可能还得进入某个目录或设置环境变量,写一个脚本搞定:


run_app.sh


#!/bin/bash
export LD_LIBRARY_PATH=/app/city_app/lib/
echo $LD_LIBRARY_PATH
cd /app/city_app/bin/
./b503_app


查找应用并设置应用的环境变量并运行


run_app.sh


应用升级脚本


start_remoteupdate.sh


#!/bin/bash
singelName="remoteupdate"
fileName="/usr/bin/remoteupdate"
configFile="/app/update.conf"
function CheckProcess()
{
  PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`
  return $PROCESS_NUM
}
function startProcess()
{
  echo "check process run status"
  CheckProcess $singelName
  if [ $? -eq 0 ];then
    echo "progress is not run"
  else
    echo "process is running"
    #killall -9 $singelName
    #sleep 1
    exit 0
  fi 
  echo "start remoteupdate ..."
  $singelName &
  echo "start end"
}
#copy config file
if [ -f $configFile ];then
  echo "config file exist"
else
  echo "copy update.conf file"
  cp /app/city_app/opt/update.conf /app/
  sync
  if [ -f $configFile ];then
    echo "copy ok"
  fi
fi
#start process remoteupdate
if [ ! -f $fileName ];then
  echo "copy remoteupdate process to /usr/bin"
  cp /app/city_app/opt/$singelName /usr/bin/
  sync
  if [ -f $fileName ];then
    echo "copy ok"
    startProcess
    exit 0
  else
    echo "copy fail"
    exit 1
  fi
else
  echo "remoteupdate file exsit"
  startProcess
  exit 0
fi


应用监控和升级服务脚本


app_monitor.sh


#!/bin/bash
#######city app monitor deamon ########
function checkAppRun()
{
   flag=0
   val=0
   cnt=0
   max=$2
   #echo "checkApprun...$1"
   while [ $val -lt $max ]
   do           
      cnt=$(ps | grep $1 | wc -l)          
      if [ $cnt -le 1 ];
      then
         let flag+=1
         sleep 1
         let val+=1
      else
          break
      fi
   done  
   if [ $flag -ge $max ];
   then
      return 0
   #$1 app is not running
   else 
      return 1   
   #$1 app has been run
   fi
}
function CheckProcess()
{
  PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`
  return $PROCESS_NUM
}
function monitor_app()
{
   while true
   do
       CheckProcess b503_app
       if [ $? -ne 1 ] ;
       then 
            if [ $? -ne 0 ];
      then
          killall -9 b503_app
      fi
            source /etc/init.d/app_update_b503_app.sh
       fi
#       CheckProcess remoteupdate
#       if [ $? -ne 1 ] ;
#       then
#            if [ $? -ne 0 ];
#            then
#                killall -9 remoteupdate
#            fi
#            source /etc/init.d/app_start_remoteupdate.sh
#       fi
   done
}
function monitor_b503_ft()
{
   while true
   do
       CheckProcess b503_ft
       if [ $? -eq 0 ] ;
       then
      break
       else 
      sleep 1
       fi
   done
}
function monitor_ft_app()
{
   while true
   do
   checkAppRun b503_ft 2
   if [ $? -eq 0 ] ;
   then  
      break
   fi
   done
}
################main#####################
####synchronize date and rtc time####### 
#date 
#hwclock -w --local
########################################
source /etc/init.d/app_update_b503_ft.sh
#monitor_ft_app &
checkAppRun b503_ft 2
if [ $? -eq 0 ] ;
then
    source /etc/init.d/app_update_b503_app.sh
    monitor_app &
else
    monitor_b503_ft 
    source /etc/init.d/app_update_b503_app.sh
    monitor_app &
fi


应用ipk包安装脚本


#!/bin/bash
#b503 application update mechnism
#search directory in order, /update,/media/usb/
#set -e
buzzGPIO="/sys/class/gpio/gpio15"
curdir=$(pwd)
#b503_city_app_name=city_app
#enable buzzse for notifying success
function beep_notify()
{
   if [ ! -d "$buzzGPIO" ]; then
        echo 15 > /sys/class/gpio/export
   fi
   if [ -d "$buzzGPIO" ]; then
        echo "out" > "/sys/class/gpio/gpio15/direction"
        echo "1" > "/sys/class/gpio/gpio15/value"
        sleep 1
        echo "0" > "/sys/class/gpio/gpio15/value"
   fi
}
#install all ipks in specified directory
function install_ipks()
{
   path=$1
   files=$(ls $path)
   for filename in $files
   do
      if [ "${filename##*.}"x = "ipk"x ]; 
      then
          echo $filename
          cd $path
          opkg install --force-reinstall --force-downgrade --force-overwrite $filename
#         sleep 1
      fi 
   done
}
#check and create directory by specified path 
function check_and_mkdir()
{
   result_path=$1
   result_name=$2
   cd $result_path
#   echo "$result_path"
   if [ ! -d $result_name ];
   then
      #echo "mkdir $result_name"
      mkdir -p $result_name
   #else
   #   echo "$result_name is exit!!"
   fi
}
#check if updating from update directory 
function isUpdateIpkFromUpdir()
{
   #echo "1--->search update ipk directory."
   cd /update  
   #cnt=$(find -name "*.ipk" | wc -l)
   if [ -d ipk ];
   then 
      cnt=$(ls ipk | wc -l)
      if [ $cnt -ge 1 ] ;
      then  
         echo "1.1--->check update flag !"
         cnt=$(find -name "update.txt" | wc -l )
         if [ $cnt -ge 1 ];
         then 
            var=$(cat update.txt)
            if [[ $var -eq 1 ]];
            then
               echo "1.2--->update-flag is 1!" 
               return 0 
            else              
               echo "1.3--->update flag is 0" 
               return 1
            fi
         else
            echo "1.8--->no update.txt.."
            return 1
         fi      
      else
         echo "1.9--->there is no  ipk files in update directory!"
         return 1
      fi
   fi 
   return 1
}
#check if updating  bin  from update directory 
function isUpdateBinFromUpdir()
{
   #echo "1--->search update bin directory."
   cd /update  
   if [ -d bin ];
   then  
   cnt=$(ls bin | wc -l)
   if [ $cnt -ge 1 ] ;     
   then 
      cnt=$(find -name "update.txt" | wc -l )
      if [ $cnt -ge 1 ];
      then 
         var=$(cat update.txt)
         if [[ $var -eq 2 ]];
         then
            return 0 
         else              
            return 1
         fi
      else
         return 1
      fi
   fi
   fi
   return 1
}
#check if updating  lib  from update directory 
function isUpdateLibFromUpdir()
{
   #echo "1--->search update lib  directory."
   cd /update   i
   if [ -d lib ];
   then 
   cnt=$(ls lib | wc -l)
   if [ $cnt -ge 1 ] ;  
   then     
      cnt=$(find -name "update.txt" | wc -l )
      if [ $cnt -ge 1 ];
      then 
         var=$(cat update.txt)
         if [[ $var -eq 3 ]];
         then
            return 0 
         else              
            return 1
         fi
      else
         return 1
      fi    
   fi
   fi 
   return 1
}
#check if updating  audio  from update directory 
function isUpdateAudioFromUpdir()
{
   #echo "1--->search update audio directory."
   cd /update  
   if [ -d audio ];
   then 
   cnt=$(ls audio | wc -l)
   if [ $cnt -ge 1 ] ;     
   then  
      cnt=$(find -name "update.txt" | wc -l )
      if [ $cnt -ge 1 ];
      then 
         var=$(cat update.txt)
         if [[ $var -eq 4 ]];
         then
            return 0 
         else              
            return 1
         fi
      else
         return 1
      fi    
   fi
   fi
   return 1
}
#check if updating form udisk 
function isUpdateFromUdisk()
{
   #echo "2--->search u disk directory."
   cd /media
   cnt=$(find -name "usb" | wc -l )
   if [ $cnt -ge 1 ];
   then 
      cd /media/usb 
      cnt=$(find -name "*.ipk" | wc -l)
      if [ $cnt -ge 1 ] ;
      then 
         echo "2.1--->udisk,find ipk files"
         return 0
      else
         echo "2.2--->udisk,no b503-app.ipk......" 
         return 1
      fi
   else
      #echo "2.9:no udisk is inserted......."    
      return 1 
   fi
}


服务管理,启动或停止服务


service.sh


#!/usr/bin/env sh
export ALIPAY_ROOT=$(cd `dirname $0`; cd ../../; pwd)
# define the global value
pid=
start_service() {
  cd ${ALIPAY_ROOT}/iotsdk/bin
  nohup ./alipay_iotd >/dev/null 2>&1 &
}
PS_LINE=`ps |grep alipay_iotd|grep -v grep`
if [ ! -z "${PS_LINE}" ]; then
  #pids=(${PS_LINE// / })
  pid=`echo ${PS_LINE} | cut -d ' ' -f 1`
  #pid=${pids[0]}
  echo "snapshot pid is ${pid}"
fi
case $1 in
  "keepalive")
    if [ -z "${pid}" ]; then
      start_service
    fi
  ;;
  "startup")
    if [ -z "${pid}" ]; then
      start_service
    else
      echo "Service was already started!"
    fi
  ;;
  "shutdown")
    if [ -n "${pid}" ]; then
      kill -9 "${pid}"
      echo "Service was terminated!"
    fi
  ;;
  "restart")
    if [ -n "${pid}" ]; then
      echo "Stop service..."
      kill -9 "${pid}"
      echo "Service was terminated!"
    fi
    sleep 3
    start_service
  ;;
  "status")
     echo "Service is running on proc: ${pid}"
  ;;
  *)
     echo "Unsupported command!"
  ;;
esac


统一改写目录下的文件属性


#!/bin/bash
#查找当前目录下(递归级数1)的所有目录文件
SRC_DIR=$(find ./ -maxdepth 1 -type d)
#变量SRC_DIR可以用${}引用,可以$直接引用,但不可以用$()引用
echo ${SRC_DIR}
#将当前目录下所有一级目录文件的Other写属性去掉
chmod o-w ${SRC_DIR}


自动下载并构建freetype脚本


#!/bin/bash
set -x
set -o errexit -o nounset
# 22.0.16 is the libtool version of 2.9.0
if pkg-config --atleast-version 22.0.16 freetype2; then exit; fi
pushd $HOME
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.bz2
tar xf freetype-2.9.tar.bz2
pushd freetype-2.9
./autogen.sh
./configure --prefix=$HOME/.local
make -j4 install
popd
popd


首次安装脚本install.sh



#!/usr/bin/env sh
#SOURCE_DIR=`pwd`
SOURCE_DIR=/app/city_app/alipay
TARGET_DIR=/app/alipay
if [ ! -d $TARGET_DIR ]; then
    mkdir $TARGET_DIR
fi
if [ ! -d $TARGET_DIR/iotsdk ]; then
    mkdir $TARGET_DIR/iotsdk
fi
if [ ! -d $TARGET_DIR/iotsdk/bin ]; then
    mkdir $TARGET_DIR/iotsdk/bin
fi
if [ ! -d $TARGET_DIR/iotsdk/conf ]; then
    mkdir $TARGET_DIR/iotsdk/conf
fi
if [ ! -d $TARGET_DIR/runtime ]; then
    mkdir $TARGET_DIR/runtime
fi
if [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotd ]; then
    cp $SOURCE_DIR/iotsdk/bin/alipay_iotd $TARGET_DIR/iotsdk/bin
fi
if [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotmd ]; then
    cp $SOURCE_DIR/iotsdk/bin/alipay_iotmd $TARGET_DIR/iotsdk/bin
fi
if [ -e $SOURCE_DIR/iotsdk/bin/monitor.sh ]; then
    cp $SOURCE_DIR/iotsdk/bin/monitor.sh $TARGET_DIR/iotsdk/bin
fi
if [ -e $SOURCE_DIR/iotsdk/bin/service.sh ]; then
    cp $SOURCE_DIR/iotsdk/bin/service.sh $TARGET_DIR/iotsdk/bin
fi


autogen.sh


#!/bin/sh
# Run this to generate all the initial makefiles, etc.
test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.
olddir=`pwd`
cd $srcdir
#echo -n "checking for ragel... "
#which ragel || {
# echo "You need to install ragel... See http://www.complang.org/ragel/"
# exit 1
#}
echo -n "checking for pkg-config... "
which pkg-config || {
  echo "*** No pkg-config found, please install it ***"
  exit 1
}
echo -n "checking for libtoolize... "
which glibtoolize || which libtoolize || {
  echo "*** No libtoolize (libtool) found, please install it ***"
  exit 1
}
echo -n "checking for gtkdocize... "
if which gtkdocize ; then
  gtkdocize --copy || exit 1
else
  echo "*** No gtkdocize (gtk-doc) found, skipping documentation ***"
  echo "EXTRA_DIST = " > gtk-doc.make
fi
echo -n "checking for autoreconf... "
which autoreconf || {
  echo "*** No autoreconf (autoconf) found, please install it ***"
  exit 1
}
echo "running autoreconf --force --install --verbose"
autoreconf --force --install --verbose || exit $?
cd $olddir
test -n "$NOCONFIGURE" || {
  echo "running configure $@"
  "$srcdir/configure" "$@"
}


引用


linux系统中开机自启的三种方式_灬紫荆灬-CSDN博客_linux开机自启动


linux /etc/init.d和/etc/rc/init.d联系,运行级别,/etc/rc.d/init.d执行流程_mengzuchao的专栏-CSDN博客

相关文章
|
2天前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
12 2
|
2天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
2天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
13 1
|
2天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
2天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
2天前
|
Linux Shell 程序员
【Linux】权限(shell运行原理、概念,Linux权限)
【Linux】权限(shell运行原理、概念,Linux权限)
15 2
|
2天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
13 1
|
2天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
14 1
|
2天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
2天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份