shell script learn

之前写测试程序使用脚本引发了兴趣,结合具体案例,检验学习成果。终于看懂了一个稍微复杂的shell脚本。里面还是有很多技巧性的东西存在。

声明的变量名,BLUETOOTH_SLEEP_PATH没有用到

[html]  view plain copy
  1. BLUETOOTH_SLEEP_PATH=/proc/bluetooth/sleep/proto  
  2. LOG_TAG="qcom-bluetooth"  
  3. LOG_NAME="${0}:"  


声明函数

$# ----传递给程序的总的参数数目
$? ----
上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值。
$* ----
传递给程序的所有参数组成的字符串。
    $- ----
Shell启动或使用set命令时提供选项
    $? ----
上一条命令执行后返回的值
    $$ ----
当前shell的进程号
    $! ----
上一个子进程的进程号
    $@ ----
所有的参数,每个都用双括号括起
    $n ----
位置参数值,n表示位置
    $0 ----
当前shell

[html]  view plain copy
  1. loge ()  
  2. {  
  3.   /system/bin/log -t $LOG_TAG -p e "$LOG_NAME $@"  
  4. }  
  5.   
  6. logi ()  
  7. {  
  8.   /system/bin/log -t $LOG_TAG -p i "$LOG_NAME $@"  
  9. }  
  10.   
  11. failed ()  
  12. {  
  13.   loge "$1: exit code $2"  
  14.   exit $2  
  15. }  

&符号我理解是后台运行的意思,这样$!表示上一个子进程的进程号,用法可参考test2.sh

[html]  view plain copy
  1. start_hciattach ()  
  2. {  
  3.   /system/bin/hciattach -n $BTS_DEVICE $BTS_TYPE $BTS_BAUD &  
  4.   hciattach_pid=$!  
  5.   logi "start_hciattach: pid = $hciattach_pid"  
  6.   echo 1 > $BLUETOOTH_SLEEP_PATH  
  7. }  
  8.   
  9. kill_hciattach ()  
  10. {  
  11.   echo 0 > $BLUETOOTH_SLEEP_PATH  
  12.   logi "kill_hciattach: pid = $hciattach_pid"  
  13.   ## careful not to kill zero or null!  
  14.   kill -TERM $hciattach_pid  
  15.   # this shell doesn't exit now -- wait returns for normal exit  
  16. }  


获取参数并解析,跟c语言中的类似。blnp是不带参数的,t:s:带一个冒号表示带参数、
默认输出USAGE

[html]  view plain copy
  1. while getopts "blnpt:s:" f  
  2. do  
  3.   case $f in  
  4.   b | l | n | p)  opt_flags="$opt_flags -$f" ;;  
  5.   t)      timeout=$OPTARG;;  
  6.   s)      initial_speed=$OPTARG;;  
  7.   \?)     echo $USAGE; exit 1;;  
  8.   esac  
  9. done  
  10. shift $(($OPTIND-1))  


涉及case的用法可以学习参考下

[html]  view plain copy
  1. case $POWER_CLASS in  
  2.   1) PWR_CLASS="-p 0" ;  
  3.      logi "Power Class: 1";;  
  4.   2) PWR_CLASS="-p 1" ;  
  5.      logi "Power Class: 2";;  
  6.   3) PWR_CLASS="-p 2" ;  
  7.      logi "Power Class: CUSTOM";;  
  8.   *) PWR_CLASS="";  
  9.      logi "Power Class: Ignored. Default(1) used (1-CLASS1/2-CLASS2/3-CUSTOM)";  
  10.      logi "Power Class: To override, Before turning BT ON; setprop qcom.bt.dev_power_class <1 or 2 or 3>";;  
  11. esac  
  12.   
  13. case $LE_POWER_CLASS in  
  14.   1) LE_PWR_CLASS="-P 0" ;  
  15.      logi "LE Power Class: 1";;  
  16.   2) LE_PWR_CLASS="-P 1" ;  
  17.      logi "LE Power Class: 2";;  
  18.   3) LE_PWR_CLASS="-P 2" ;  
  19.      logi "LE Power Class: CUSTOM";;  
  20.   *) LE_PWR_CLASS="-P 1";  
  21.      logi "LE Power Class: Ignored. Default(2) used (1-CLASS1/2-CLASS2/3-CUSTOM)";  
  22.      logi "LE Power Class: To override, Before turning BT ON; setprop qcom.bt.le_dev_pwr_class <1 or 2 or 3>";;  
  23. esac  

eval的用法:主要是解析完参数后,将参数作为指令重新执行一次。

再变量中先解析输出参数,eval将参数作为指令执行,因此exit_code_hci_qcomm_init作为变量赋值。如果hci_qcomm_init执行成功后,exit_code_hci_qcomm_init赋值为0,失败则赋值为1。关于用法可参考test1.sh

[html]  view plain copy
  1. eval $(/system/bin/hci_qcomm_init -e $PWR_CLASS $LE_PWR_CLASS && echo "exit_code_hci_qcomm_init=0" || echo "exit_code_hci_qcomm_init=1")  
  2.   
  3. case $exit_code_hci_qcomm_init in  
  4.   0) logi "Bluetooth QSoC firmware download succeeded, $BTS_DEVICE $BTS_TYPE $BTS_BAUD $BTS_ADDRESS";;  
  5.   *) failed "Bluetooth QSoC firmware download failed" $exit_code_hci_qcomm_init;;  
  6. esac  

 

trap用于指定收到某种信号时锁执行的命令。ctrl+c组合键触发的INT信号,执行中断命令。

这句话应该是表示收到 TERM和INT两种信号就执行kill_hciattach,不知道理解的对否

忽略的写法是trap " " TERM

由于TRANSPORT为空,执行默认值,start_hciattach等待进程结束后蓝牙停止。

[html]  view plain copy
  1. trap "kill_hciattach" TERM INT  
  2.   
  3. case $TRANSPORT in  
  4.     "smd")  
  5.         echo 1 > /sys/module/hci_smd/parameters/hcismd_set  
  6.      ;;  
  7.      *)  
  8.         logi "start hciattach"  
  9.         start_hciattach  
  10.   
  11.         wait $hciattach_pid  
  12.         logi "Bluetooth stopped"  
  13.      ;;  
  14. esac  

附件实验程序:

test1.sh

[html]  view plain copy
  1. #!/bin/bash  
  2. A=123  
  3. eval $( [ -n "$A" ] && echo "exit_code_hci_qcomm_init=0" || echo "exit_code_hci_qcomm_init=1")  
  4.   
  5. echo "$exit_code_hci_qcomm_init"  

test2.sh

[html]  view plain copy
  1. #!/bin/bash  
  2.   
  3. hciattach_pid=""  
  4.   
  5. func1 ()  
  6. {  
  7.     echo "test trap "  
  8.     echo "in func1" &  
  9.     hciattach_pid=$!  
  10.     echo $hciattach_pid  
  11.       
  12. }  
  13.   
  14. func1  


参考:

shell十三问,和华清出的linux shell编程从初学到精通

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值