嵌入式linux应用开发之常用shell脚本总结
总结下日常工作中常用的linux下的shell脚本。
linux的shell脚本很强大,可以用来做一些特殊功能。shell脚本语法虽然很简单,但是有时候把经常忘,还得再写一遍且验证ok才能用,这里总结下留作备忘。
关于shell脚本的学习觉得不需要太刻意,根据需要来。看的多用的多了自然就会了,至少能看懂吧。
目录
启用硬件IO口
开机后应用启动其它应用服务
触发执行其它应用
拷贝U盘中的文件到设备中
拷贝U盘数据
Linux应用开机自启动
Linux的引导过程
rc和rcS脚本
简化常用指令操作,写个脚本
查找应用并设置应用的环境变量并运行
应用升级脚本
服务管理,启动或停止服务
统一改写目录下的文件属性
自动下载并构建freetype脚本
首次安装脚本install.sh
autogen.sh
启用硬件IO口
enGPIO.sh
#!/bin/sh
mygpioPath1="/sys/class/gpio/gpio115"
mygpioPath2="/sys/class/gpio/gpio116"
buzzGPIO="/sys/class/gpio/gpio15"
qrd_pwr="/sys/class/gpio/gpio121"
power_down="/sys/class/gpio/gpio128"
touch_key="/sys/class/gpio/gpio112"
oth_pwr="/sys/class/gpio/gpio130"echo "shell exec open..."#enable voice
if [ ! -d "$mygpioPath1" ]; thenecho 115 > /sys/class/gpio/export
fi
if [ -d "$mygpioPath1" ]; thenecho "out" > "/sys/class/gpio/gpio115/direction"echo "1" > "/sys/class/gpio/gpio115/value"
fi#enable voice
#if [ ! -d "$mygpioPath2" ]; then
# echo 116 > /sys/class/gpio/export
#fi
#if [ -d "$mygpioPath2" ]; then
# echo "out" > "/sys/class/gpio/gpio116/direction"
# echo "1" > "/sys/class/gpio/gpio116/value"
#fi#enable buzzse
if [ ! -d "$buzzGPIO" ]; thenecho 15 > /sys/class/gpio/export
fi
if [ -d "$buzzGPIO" ]; thenecho "out" > "/sys/class/gpio/gpio15/direction"echo "0" > "/sys/class/gpio/gpio15/value"
fiif [ ! -d "$qrd_pwr" ]; thenecho 121 > /sys/class/gpio/export
fi
if [ -d "$qrd_pwr" ]; thenecho "out" > "/sys/class/gpio/gpio121/direction"echo "0" > "/sys/class/gpio/gpio121/value"
fi#enable power down check
if [ ! -d "$power_down" ]; thenecho 128 > /sys/class/gpio/export
fi
if [ -d "$power_down" ]; thenecho "in" > "/sys/class/gpio/gpio128/direction"
fi#enable touch key check
if [ ! -d "$touch_key" ]; thenecho 112 > /sys/class/gpio/export
fi
if [ -d "$touch_key" ]; thenecho "in" > "/sys/class/gpio/gpio112/direction"
fi#enable oth_pwr
if [ ! -d "$oth_pwr" ]; thenecho 130 > /sys/class/gpio/export
fi
if [ -d "$qrd_pwr" ]; thenecho "out" > "/sys/class/gpio/gpio130/direction"echo "0" > "/sys/class/gpio/gpio130/value"
fi
开机后应用启动其它应用服务
int main(int argc, char ** argv)
{printf("main\n");system("../opt/enGPIO.sh");system("killall -9 monitor");//启动监控服务monitor进程system("../opt/startmonitor.sh");
}
startmonitor.sh
#!/bin/bash
fileName="/app/city_app/opt/monitor"
buzzGPIO="/sys/class/gpio/gpio15"
#enable buzzse for notifying success
function beep_notify()
{if [ ! -d "$buzzGPIO" ]; thenecho 15 > /sys/class/gpio/exportfiif [ -d "$buzzGPIO" ]; thenecho "out" > "/sys/class/gpio/gpio15/direction"echo "1" > "/sys/class/gpio/gpio15/value"sleep 1echo "0" > "/sys/class/gpio/gpio15/value"fi
}function CheckProcess()
{PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`return $PROCESS_NUM
}
if [ ! -f $fileName ]; then echo "error!monitor exe not exit!"exit 1else echo "find monitor exe,begin start..."CheckProcess monitorif [ $? -eq 0 ];thenecho "no monitor progress find!"elseecho "find monitor,..."killall -9 monitorsleep 1fi cd /app/city_app/opt/./monitor & echo "start ok"beep_notifyexit 0
fi
触发执行其它应用
如uplog.sh,触发ftp应用上传日志文件
#!/bin/sh
echo "upload log to ftp server..."
echo $1
if [ -z $1 ]; then today=`date +"%Y%m%d"`elsetoday=$1
fiecho $todayfunction getfname()
{echo $1filename=$(find /log/ -name "$1")echo $filenamereturn 0
}
#filename="find ./ -name "*${today}.log""#echo $filename
getfname *${today}.logecho $filename
../opt/ftp -ftpcfg=/../opt/ftpcfg.ini -fname=$filename
拷贝U盘中的文件到设备中
setconfig.sh,把U盘根目录下的config.ini文件拷贝到设置的指定目录下。
#!/bin/bashusbPath="/media/usb"
fileName="/media/usb/config_up.ini"buzzGPIO="/sys/class/gpio/gpio15"
#enable buzzse for notifying success
function beep_notify()
{if [ ! -d "$buzzGPIO" ]; thenecho 15 > /sys/class/gpio/exportfiif [ -d "$buzzGPIO" ]; thenecho "out" > "/sys/class/gpio/gpio15/direction"echo "1" > "/sys/class/gpio/gpio15/value"sleep 1echo "0" > "/sys/class/gpio/gpio15/value"fi
}setconfig()
{echo "find usb device,begin copy config_ini..."if [ ! -f $fileName ]; then echo "config file not exit!"return 1else cp $fileName /app/city_app/etc/return 0fi
}if [ -d "$usbPath" ]; thensetconfigif [ $? -ne 0 ] ;thenecho "faild to set config"exit 1fibeep_notifybeep_notifybeep_notifyecho "set ok"exit 0
else
echo "no usb device found!"
exit 1
fi
拷贝U盘数据
#!/bin/bashusbPath="/media/usb"
fileName="/media/usb/qrlinux"dbName="/app/city_app/opt/bus.db"buzzGPIO="/sys/class/gpio/gpio15"
#enable buzzse for notifying success
function beep_notify()
{if [ ! -d "$buzzGPIO" ]; thenecho 15 > /sys/class/gpio/exportfiif [ -d "$buzzGPIO" ]; thenecho "out" > "/sys/class/gpio/gpio15/direction"echo "1" > "/sys/class/gpio/gpio15/value"sleep 1echo "0" > "/sys/class/gpio/gpio15/value"fi
}function CheckProcess()
{PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`return $PROCESS_NUM
}copydata()
{echo "find usb device,begin copy data..."if [ ! -f $dbName ]; then echo "db file not exit!"return 1else cp -v /app/city_app/opt/bus.db /media/usb/cd /media/usb/ls -lreturn 0fi
}if [ -d "$usbPath" ]; thencopydataif [ $? -ne 0 ] ;thenecho "faild to copy data"fi
else
echo "no usb device found!"
exit 1
fiif [ ! -f $fileName ]; then echo "qrlinux file not exit!"else echo "find qrlinux file,begin updata..."CheckProcess qrlinuxif [ $? -eq 0 ];then echo "no qrlinux progress find!"elseecho "find qrlinux,..."killall -9 qrlinuxsleep 2fi cp -v $fileName /app/city_app/opt/cd /app/city_app/opt/ls -lecho "cp ok"umount /media/usb/beep_notifyexit 0
fi
Linux应用开机自启动
/etc/init.d/文件夹下放置有很多常用服务的控制脚本和其他的一些脚本。
K开头的脚本文件代表运行级别加载时需要关闭的,S开头的代表需要执行。
因此当我们需要开机启动自己的脚本时,只需要将可执行脚本丢在/etc/init.d目录下,然后在/etc/rc.d/rc*.d中建立软链接即可。如:
[root@localhost ~]# ln -s /etc/init.d/sshd /etc/rc.d/rc3.d/S100ssh
sshd是具体服务的脚本文件,S100ssh是其软链接,S开头代表加载时自启动
如果需要在多个运行级别下设置自启动,则需建立多个软链接。
Linux的引导过程
系统启动之后,在进入init.d之前,我们先来看看系统都做了什么工作。系统加电之后,首先进行的硬件自检,然后是bootload对系统的初始化,加载内核。
内核被加载到内存之后,就开始执行了。一旦内核启动运行,对硬件的检测就会决定需要对哪些设备驱动进行初始化。
从这开始内核就能够挂装根文件系统。内核挂装了根文件系统,并已初始化所有的设备驱动程序和数据结构等之后,就通过启动一个叫init的用户级程序,完成引导进程。
Init进程是系统启动之后的第一个用户进程,所以它的pid(进程编号)始终为1。init进程上来首先做的事是去读取/etc/目录下inittab文件中initdefault id值,这个值称为运行级别(run-level)。它决定了系统启动之后运行于什么级别。运行级别决定了系统启动的绝大部分行为和目的。
这个级别从0到6 ,具有不同的功能。不同的运行级定义如下:
0 – 停机(千万别把initdefault设置为0,否则系统永远无法启动)
1 – 单用户模式,root权限,用于系统维护,禁止远程登陆
2 – 多用户状态,没有 NFS
3 – 标准多用户模式,登陆后进入命令行模式
4 – 系统未使用,保留
5 – 多用户图形模式,登陆后进入图形GUI模式
6 – 重新启动(千万不要把initdefault 设置为6,否则将一直在重启 )
服务器一般都是命令行模式,所以默认运行级别为 3
如何添加自启程序?
(1)/etc/init.d 目录中添加
以启动SVN为例
1)在 /etc/init.d 目录下创建启动服务的脚本
vim /etc/init.d/svn
#!/bin/bash
svnserve -d -r /svn仓库路径
设置执行权限
chmod 755 /etc/init.d/svn
2)把这个脚本软链接到 /etc/rc.d/rc3.d
ln -s /etc/init.d/svn /etc/rc.d/rc3.d/S101svn
S 代表是要执行这个脚本,101 是执行顺序,通常要大于60,这样可以保证基础服务都已经启动完成
重启linux测试
(2)/etc/rc.local 文件中添加
直接修改 /etc/rc.local
该脚本是在系统初始化级别脚本运行之后再执行的,因此可以在里面添加想在系统启动之后执行的脚本
(3)chkconfig 命令添加
如何禁止自启程序?
(1)直接删除 /etc/rc.d/rcN.d 目录的目标链接文件
(2)删除 /etc/rc.local 文件中定义的启动脚本
(3)查看自启动服务列表,从中选择目标服务执行禁止操作
chkconfig –list
chkconfig –del 服务名
init.d里面放的都是什么东西。这个目录存放的是一些脚本,一般是linux以rpm包安装时设定的一些服务的启动脚本。系统在安装时装了好多rpm包,这里面就有很多对应的脚本。执行这些脚本可以用来启动,停止,重启这些服务。
前面说到,/etc/rc.d/init.d这个目录下的脚本就类似与windows中的注册表,在系统启动的时候执行。程序运行到这里(init进程读取了运行级别),该行/etc/rc.d/init.d里面的脚本,但是并不是直接运行,而是有选择的因为系统并不需要启动所有的服务。
系统是如何选择哪些需要启动哪些不需要启动?运行级别就起作用了。在决定了系统启动的run level之后,/etc/rc.d/rc这个脚本先执行。在有些linux源码中它都是一上来就check_runlevel(),知道了运行级别之后,对于每一个运行级别,在rc.d下都有一个子目录分别是rc0.d,rc1.d ….. rc6.d。每个目录下都是到init.d目录的一部分脚本一些链接。每个级别要执行哪些服务就在相对应的目录下,比如级别5要启动的服务就都放在rc5.d下,但是放在这个rc5.d下的都是一些链接文件,链接到init.d中相对应的文件,真正干活到init.d里的脚本。
/etc/init.d里面还放置了一些脚本可以用来管理服务用。比如:
可以使用start,stop,restart选项。例如,如果你想关闭网络,你可以使用如下形式的命令:
/etc/init.d/networking stop
又比如,你改变了网络设置,并且需要重启网络。你可以使用如下命令:
/etc/init.d/networking restart
rc.local也是经常使用的一个脚本。该脚本是在系统初始化级别脚本运行之后再执行的,因此可以安全地在里面添加你想在系统启动之后执行的脚本。
rc和rcS脚本
#!/bin/sh
#
# rc This file is responsible for starting/stopping
# services when the runlevel changes.
#
# Optimization feature:
# A startup script is _not_ run when the service was
# running in the previous runlevel and it wasn't stopped
# in the runlevel transition (most Debian services don't
# have K?? links in rc{1,2,3,4,5} )
#
# Author: Miquel van Smoorenburg <miquels@cistron.nl>
# Bruce Perens <Bruce@Pixar.com>
#
# Version: @(#)rc 2.78 07-Nov-1999 miquels@cistron.nl
#. /etc/default/rcS
export VERBOSEstartup_progress() {step=$(($step + $step_change))if [ "$num_steps" != "0" ]; thenprogress=$((($step * $progress_size / $num_steps) + $first_step))elseprogress=$progress_sizefi#echo "PROGRESS is $progress $runlevel $first_step + ($step of $num_steps) $step_change $progress_size"if type psplash-write >/dev/null 2>&1; thenTMPDIR=/mnt/.psplash psplash-write "PROGRESS $progress" || truefi#if [ -e /mnt/.psplash/psplash_fifo ]; then# echo "PROGRESS $progress" > /mnt/.psplash/psplash_fifo#fi
}#
# Start script or program.
#
startup() {# Handle verbosity[ "$VERBOSE" = very ] && echo "INIT: Running $@..."case "$1" in*.sh)# Source shell script for speed.(trap - INT QUIT TSTPscriptname=$1shift. $scriptname);;*)"$@";;esacstartup_progress
}# Ignore CTRL-C only in this shell, so we can interrupt subprocesses.trap ":" INT QUIT TSTP# Set onlcr to avoid staircase effect.stty onlcr 0>&1# Limit stack size for startup scripts[ "$STACK_SIZE" == "" ] || ulimit -S -s $STACK_SIZE# Now find out what the current and what the previous runlevel are.runlevel=$RUNLEVEL# Get first argument. Set new runlevel to this argument.[ "$1" != "" ] && runlevel=$1if [ "$runlevel" = "" ]thenecho "Usage: $0 <runlevel>" >&2exit 1fiprevious=$PREVLEVEL[ "$previous" = "" ] && previous=Nexport runlevel previous# Is there an rc directory for this new runlevel?if [ -d /etc/rc$runlevel.d ]then# Find out where in the progress bar the initramfs got to.PROGRESS_STATE=0#if [ -f /dev/.initramfs/progress_state ]; then# . /dev/.initramfs/progress_state#fi# Split the remaining portion of the progress bar into thirdsprogress_size=$(((100 - $PROGRESS_STATE) / 3))case "$runlevel" in0|6)# Count down from -100 to 0 and use the entire barfirst_step=-100progress_size=100step_change=1;;S)# Begin where the initramfs left off and use 2/3# of the remaining spacefirst_step=$PROGRESS_STATEprogress_size=$(($progress_size * 2))step_change=1;;*)# Begin where rcS left off and use the final 1/3 of# the space (by leaving progress_size unchanged)first_step=$(($progress_size * 2 + $PROGRESS_STATE))step_change=1;;esacnum_steps=0for s in /etc/rc$runlevel.d/[SK]*; docase "${s##/etc/rc$runlevel.d/S??}" ingdm|xdm|kdm|reboot|halt)break;;esacnum_steps=$(($num_steps + 1))donestep=0# First, run the KILL scripts.if [ $previous != N ]thenfor i in /etc/rc$runlevel.d/K[0-9][0-9]*do# Check if the script is there.[ ! -f $i ] && continue# Stop the service.startup $i stopdonefi# Now run the START scripts for this runlevel.for i in /etc/rc$runlevel.d/S*do[ ! -f $i ] && continueif [ $previous != N ] && [ $previous != S ]then## Find start script in previous runlevel and# stop script in this runlevel.#suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffixprevious_start=/etc/rc$previous.d/S[0-9][0-9]$suffix## If there is a start script in the previous level# and _no_ stop script in this level, we don't# have to re-start the service.#[ -f $previous_start ] && [ ! -f $stop ] && continueficase "$runlevel" in0|6)startup $i stop;;*)startup $i start;;esacdonefi
#!/bin/bash#myapp application start mechnism
#manu run myapp#set -e
curdir=$(pwd)
myapp_city_app_name=city_app#search executable file in bin directory
#and start the file.
#the file number must be only one!!!!
function searchBinAndRun()
{
# echo "searchBinAndRun...enter"path=$1cd $pathif [ ! -d bin ];then echo "no bin directory ..."return 1ficd bin files=$(ls $path/bin)echo "path= $path"for filename in $filesdoecho $filenameif [ ! -d $filename ];then chmod 777 $filename#./$filename &./$filenamereturn 0fidonereturn 1
}#set env variable for application
function setAppEnv()
{
# echo "setAppEnv...enter"path=$1echo "path= $path"if [ -d lib ];thencd lib export LD_LIBRARY_PATH=$(pwd)#echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"fi
}#start myapp specified application
#after opkg the app ipk
function startmyappApp()
{echo "startmyappApp...start"if [ $# -lt 1 ] ;thenecho "para is not enough!!,quit" return 1fi appname=$1cd /app #echo "---->$appname"if [ -d $appname ];then cd $appname#echo "pwd = $(pwd)"setAppEnv /app/$appnameif searchBinAndRun /app/$appname ; then echo "$appname starts success"return 0elseecho "$appname starts error"return 1fielse echo "$appname dir isnot exist!!"return 1fi
}#
###myapp start main
########try to start myapp customersized app########
####monitor myapp-app,may be restarted!!############
echo "2--->try to start customersized app....."
startmyappApp $myapp_city_app_name
exit 0
简化常用指令操作,写个脚本
比如设置环境变量和scp往机器中拷贝文件,整个脚本简化下操作,省得重复敲不少命令
#!/bin/bash
source /opt/myir-imx-fb-qt5/4.1.15-2.0.1/environment-setup-cortexa7hf-neon-poky-linux-gnueabiscp 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 $singelNameif [ $? -eq 0 ];thenecho "progress is not run"elseecho "process is running"#killall -9 $singelName#sleep 1exit 0fi echo "start remoteupdate ..."$singelName &echo "start end"
}#copy config file
if [ -f $configFile ];thenecho "config file exist"
elseecho "copy update.conf file"cp /app/city_app/opt/update.conf /app/syncif [ -f $configFile ];thenecho "copy ok"fi
fi#start process remoteupdate
if [ ! -f $fileName ];thenecho "copy remoteupdate process to /usr/bin"cp /app/city_app/opt/$singelName /usr/bin/syncif [ -f $fileName ];thenecho "copy ok"startProcessexit 0elseecho "copy fail"exit 1fi
elseecho "remoteupdate file exsit"startProcessexit 0
fi
应用监控和升级服务脚本
app_monitor.sh
#!/bin/bash#######city app monitor deamon ########function checkAppRun()
{flag=0val=0cnt=0max=$2#echo "checkApprun...$1"while [ $val -lt $max ]do cnt=$(ps | grep $1 | wc -l) if [ $cnt -le 1 ];thenlet flag+=1sleep 1let val+=1elsebreakfidone if [ $flag -ge $max ];thenreturn 0#$1 app is not runningelse return 1 #$1 app has been runfi
}function CheckProcess()
{PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`return $PROCESS_NUM
}function monitor_app()
{while truedoCheckProcess b503_appif [ $? -ne 1 ] ;then if [ $? -ne 0 ];thenkillall -9 b503_appfisource /etc/init.d/app_update_b503_app.shfi# CheckProcess remoteupdate
# if [ $? -ne 1 ] ;
# then
# if [ $? -ne 0 ];
# then
# killall -9 remoteupdate
# fi
# source /etc/init.d/app_start_remoteupdate.sh
# fidone
}function monitor_b503_ft()
{while truedoCheckProcess b503_ftif [ $? -eq 0 ] ;thenbreakelse sleep 1fidone
}function monitor_ft_app()
{while truedocheckAppRun b503_ft 2if [ $? -eq 0 ] ;then breakfidone
}################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 ] ;
thensource /etc/init.d/app_update_b503_app.shmonitor_app &
elsemonitor_b503_ft source /etc/init.d/app_update_b503_app.shmonitor_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" ]; thenecho 15 > /sys/class/gpio/exportfiif [ -d "$buzzGPIO" ]; thenecho "out" > "/sys/class/gpio/gpio15/direction"echo "1" > "/sys/class/gpio/gpio15/value"sleep 1echo "0" > "/sys/class/gpio/gpio15/value"fi
}
#install all ipks in specified directory
function install_ipks()
{path=$1files=$(ls $path)for filename in $filesdoif [ "${filename##*.}"x = "ipk"x ]; thenecho $filenamecd $pathopkg install --force-reinstall --force-downgrade --force-overwrite $filename
# sleep 1fi done
}#check and create directory by specified path
function check_and_mkdir()
{result_path=$1result_name=$2cd $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 ]];thenecho "1.2--->update-flag is 1!" return 0 else echo "1.3--->update flag is 0" return 1fielseecho "1.8--->no update.txt.."return 1fi elseecho "1.9--->there is no ipk files in update directory!"return 1fifi 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 ]];thenreturn 0 else return 1fielsereturn 1fififireturn 1
}#check if updating lib from update directory
function isUpdateLibFromUpdir()
{#echo "1--->search update lib directory."cd /update iif [ -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 ]];thenreturn 0 else return 1fielsereturn 1fi fifi 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 ]];thenreturn 0 else return 1fielsereturn 1fi fifireturn 1
}#check if updating form udisk
function isUpdateFromUdisk()
{#echo "2--->search u disk directory."cd /mediacnt=$(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 0elseecho "2.2--->udisk,no b503-app.ipk......" return 1fielse#echo "2.9:no udisk is inserted......." return 1 fi
}
服务管理,启动或停止服务
service.sh
#!/usr/bin/env shexport ALIPAY_ROOT=$(cd `dirname $0`; cd ../../; pwd)# define the global value
pid=start_service() {cd ${ALIPAY_ROOT}/iotsdk/binnohup ./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}"
ficase $1 in"keepalive")if [ -z "${pid}" ]; thenstart_servicefi;;"startup")if [ -z "${pid}" ]; thenstart_serviceelseecho "Service was already started!"fi;;"shutdown")if [ -n "${pid}" ]; thenkill -9 "${pid}"echo "Service was terminated!"fi;;"restart")if [ -n "${pid}" ]; thenecho "Stop service..."kill -9 "${pid}"echo "Service was terminated!"fisleep 3start_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/bashset -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; fipushd $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/alipayif [ ! -d $TARGET_DIR ]; thenmkdir $TARGET_DIR
fiif [ ! -d $TARGET_DIR/iotsdk ]; thenmkdir $TARGET_DIR/iotsdk
fiif [ ! -d $TARGET_DIR/iotsdk/bin ]; thenmkdir $TARGET_DIR/iotsdk/bin
fiif [ ! -d $TARGET_DIR/iotsdk/conf ]; thenmkdir $TARGET_DIR/iotsdk/conf
fiif [ ! -d $TARGET_DIR/runtime ]; thenmkdir $TARGET_DIR/runtime
fiif [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotd ]; thencp $SOURCE_DIR/iotsdk/bin/alipay_iotd $TARGET_DIR/iotsdk/bin
fiif [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotmd ]; thencp $SOURCE_DIR/iotsdk/bin/alipay_iotmd $TARGET_DIR/iotsdk/bin
fiif [ -e $SOURCE_DIR/iotsdk/bin/monitor.sh ]; thencp $SOURCE_DIR/iotsdk/bin/monitor.sh $TARGET_DIR/iotsdk/bin
fiif [ -e $SOURCE_DIR/iotsdk/bin/service.sh ]; thencp $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 ; thengtkdocize --copy || exit 1
elseecho "*** No gtkdocize (gtk-doc) found, skipping documentation ***"echo "EXTRA_DIST = " > gtk-doc.make
fiecho -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博客
嵌入式linux应用开发之常用shell脚本总结相关推荐
- 嵌入式 Linux 入门(五、Shell 脚本编程上:认识 Shell 脚本)
大家好,是矜辰所致,嵌入式 Linux入 门第五课,本课开始简单学习一下 Shell 脚本编程. 目录 前言 一.Shell 脚本基础说明 1.1 什么是 Shell 脚本 1.2 Shell 脚本的 ...
- Linux 常用Shell脚本,后端开发必备
Linux 常用Shell脚本,后端开发必备 文章目录 Linux 常用Shell脚本,后端开发必备 1 用户猜数字 2 查看有多少远程的 IP 在连接本机 3 helloworld 4 打印 tom ...
- Linux常用Shell脚本珍藏
我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...
- linux经典脚本实例,Linux常用Shell脚本珍藏
[实例简介] Linux常用Shell脚本珍藏 (2)编写任务定时器: C代码☆ 1. echo 00 1 *k */bin/sh /usr/bin/fileback">:/root/ ...
- 嵌入式Linux驱动开发常用目录及文件梳理
前言: 在Linux中一切皆文件,在根文件系统中有许多的目录,学习了一段时间的嵌入式Linux驱动开发,本文对在驱动开发中常见的一些目录及文件的作用进行梳理和总结,以便日后的查询及复习. 1:根文件系 ...
- 嵌入式linux的开发流程
嵌入式linux开发流程: 1.搭建前期的开发环境,例如搭建linux主机环境,交叉编译器的安装,还有一些常用的开发辅助软件如source insight等等. 2.学习对硬件的直接操作,这里和单片机 ...
- 嵌入式 Linux 驱动开发你想知道的都在这
最近看到公众号上写的一篇文章,关于嵌入式 Linux 驱动开发的方方面面,感觉提供不错,此处特意贴出来供大家参考借鉴. 1.嵌入式驱动开发到底学什么 嵌入式大体分为以下四个方向: 嵌入式硬件开发:熟悉 ...
- 嵌入式Linux系统开发笔记(七)
嵌入式Linux系统开发笔记(七) 七.U-Boot基础 1.概述 在移植 Linux之前我们需要先移植一个 bootloader 代码,这个 bootloader 代码用于启动 Linux 内核, ...
- linux 没有那个文件或目录_【新视频进度】国庆节至今更新6节quot;嵌入式Linux应用开发基础quot;...
说到做到,韦东山老师国庆没有休息,录了4节新视频,到目前快速入门-嵌入式Linux应用开发基础知识已更新6节: 01.HelloWorld背后没那么简单 02.GCC编译器的使用 03.Makefil ...
- 嵌入式linux应用层中断函数,嵌入式LINUX驱动开发(中断处理函数)
嵌入式LINUX驱动开发(中断处理函数) 2020年08月11日 | 萬仟网网络运营 | 我要评论 嵌入式LINUX驱动学习之7中断相关(一)中断处理函数一.函数.头文件及说明二.编译举例:一.函数. ...
最新文章
- 当前日期得到本周的开始和结束日期
- Redis运维和开发学习目录
- vs 中自带的代码混淆器 Dotfuscator (图)
- ssm radis mysql_SSM完美整合Redis
- c语言:从标准输入读取几行输入,输入打印到标准输出上,加上行号
- 中挪动正请求第三方支出牌照竖立支出公司
- 如何批量将 Txt 文本文档转换为 xlsx 格式的 Excel 文档
- 显示器、显卡的接口类型
- 三星sm-g7106com.android.mms,三星g7106官方原版固件rom系统刷机包_三星g7106线刷包
- 【SemiDrive源码分析】【驱动BringUp】40 - Touch Panel 触摸屏调试 (Android侧)
- Python语音基础操作--5.4小波分解
- JPA mappedBy、JoinColumn、FetchType.LAZY和FetchType.EAGER介绍
- 刷题方法(五步刷题法)
- 链塔短评合集| XAS、ARDR为BB级,LSK为B级
- 企业上云的选择及好处
- 计算机无法识别打印机驱动,打印机连电脑,电脑说无法识别。为什么?
- UKF-协方差矩阵分解
- 解决Unable to determine application id: com.android.tools.idea.run.ApkProvisionException:
- 混合云是什么,主要有什么优缺点
- 公钥密码体系基于的数学难题
热门文章
- python文字转语音的五种方式win32com,pyttsx3,百度api,可使用自己的声音
- 最新Python3.10官方文档PDF来了(最新Python大词典)
- C语言如何打开shx文件,shx文件怎么打开 .shx格式打开方式解答
- 用Python写一个Excel汇总和比对小程序
- Linux下.run文件的安装和卸载
- 【软件安装管家】Axure RP 8软件安装包下载地址及安装教程
- office+visio2016版本一同安装说明
- Linux开发之libaio源码分析及应用
- TeXworks 设置成默认用XeLaTeX排版
- 威联通Nas进阶配置