I was interested in writing test programs and using scripts. I checked the learning results based on specific cases. Finally, I understood a slightly complicated shell script. There are still many technical things in it.
Declared variable name, which is not used
[Html]
Export th_sleep_path =/proc/bluetooth/sleep/proto
LOG_TAG = "qcom-bluetooth"
LOG_NAME = "$ {0 }:"
Declare a function
$ # ---- Total number of parameters passed to the program
$? ---- When the previous code or shell program exits in the shell, if it Exits normally, 0 is returned, and vice versa.
$ * ---- A string consisting of all parameters passed to the program.
$----- Options are provided when Shell is started or the set command is used
$? ---- The value returned after the previous command is executed
$ ---- Process ID of the Current shell
$! ---- Process number of the previous sub-process
$ @ ---- All parameters, each enclosed in double brackets
$ N ---- location parameter value, where n represents location
$0 ---- Current shell Name
[Html]
Loge ()
{
/System/bin/log-t $ LOG_TAG-p e "$ LOG_NAME $ @"
}
Logi ()
{
/System/bin/log-t $ LOG_TAG-p I "$ LOG_NAME $ @"
}
Failed ()
{
Loge "$1: exit code $2"
Exit $2
}
& Symbol I understand the meaning of running in the background, so $! Indicates the process number of the previous sub-process. For usage, refer to test2.sh.
[Html]
Start_hciattach ()
{
/System/bin/hciattach-n $ BTS_DEVICE $ BTS_TYPE $ BTS_BAUD &
Hciattach_pid = $!
Logi "start_hciattach: pid = $ hciattach_pid"
Echo 1> $ th_sleep_path
}
Kill_hciattach ()
{
Echo 0> $ th_sleep_path
Logi "kill_hciattach: pid = $ hciattach_pid"
# Careful not to kill zero or null!
Kill-TERM $ hciattach_pid
# This shell doesn' t exit now -- wait returns for normal exit
}
Obtain and parse parameters, similar to in C. Blnp does not contain parameters. t: s: contains a colon to indicate parameters,
USAGE output by default
[Html]
While getopts "Maid: s:" f
Do
Case $ f in
B | l | n | p) opt_flags = "$ opt_flags-$ f ";;
T) timeout = $ OPTARG ;;
S) initial_speed = $ OPTARG ;;
\?) Echo $ USAGE; exit 1 ;;
Esac
Done
Shift $ ($ OPTIND-1 ))
For more information about case-related usage, see
[Html]
Case $ POWER_CLASS in
1) PWR_CLASS = "-p 0 ";
Logi "Power Class: 1 ";;
2) PWR_CLASS = "-p 1 ";
Logi "Power Class: 2 ";;
3) PWR_CLASS = "-p 2 ";
Logi "Power Class: CUSTOM ";;
*) PWR_CLASS = "";
Logi "Power Class: Ignored. Default (1) used (1-CLASS1/2-CLASS2/3-CUSTOM )";
Logi "Power Class: To override, Before turning bt on; setprop qcom. bt. dev_power_class <1 or 2 or 3> ";;
Esac
Case $ LE_POWER_CLASS in
1) LE_PWR_CLASS = "-P 0 ";
Logi "LE Power Class: 1 ";;
2) LE_PWR_CLASS = "-P 1 ";
Logi "LE Power Class: 2 ";;
3) LE_PWR_CLASS = "-P 2 ";
Logi "LE Power Class: CUSTOM ";;
*) LE_PWR_CLASS = "-P 1 ";
Logi "LE Power Class: Ignored. Default (2) used (1-CLASS1/2-CLASS2/3-CUSTOM )";
Logi "LE Power Class: To override, Before turning bt on; setprop qcom. bt. le_dev_pwr_class <1 or 2 or 3> ";;
Esac
Eval usage: After the parameters are parsed, the parameters are re-executed as commands.
First, the output parameter is parsed in the variable, and eval executes the parameter as the command. Therefore, exit_code_hci_qcomm_init is assigned as the variable. If the execution of hci_qcomm_init is successful, the value of exit_code_hci_qcomm_init is 0, and the value of failure is 1. For usage instructions, refer to test1.sh
[Html]
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 ")
Case $ exit_code_hci_qcomm_init in
0) logi "Bluetooth QSoC firmware download succeeded, $ BTS_DEVICE $ BTS_TYPE $ BTS_BAUD $ BTS_ADDRESS ";;
*) Failed "Bluetooth QSoC firmware download failed" $ exit_code_hci_qcomm_init ;;
Esac
Trap specifies the command to lock execution when a signal is received. The INT signal triggered by the ctrl + c combination key to execute the interrupt command.
This statement indicates that kill_hciattach is executed when the signal of TERM and INT is received.
The ignoring method is trap "" TERM
Because TRANSPORT is empty, execute the default value. start_hciattach stops Bluetooth after the process ends.
[Html]
Trap "kill_hciattach" TERM INT
Case $ TRANSPORT in
"Smd ")
Echo 1>/sys/module/hci_smd/parameters/hcismd_set
;;
*)
Logi "start hciattach"
Start_hciattach
Wait $ hciattach_pid
Logi "Bluetooth stopped"
;;
Esac
Attachment lab procedure:
Test1.sh
[Html]
#! /Bin/bash
A = 123
Eval $ ([-n "$ A"] & echo "exit_code_hci_qcomm_init = 0" | echo "exit_code_hci_qcomm_init = 1 ")
Echo "$ exit_code_hci_qcomm_init"
Test2.sh
[Html] view plaincopy
#! /Bin/bash
Hciattach_pid = ""
Func1 ()
{
Echo "test trap"
Echo "in func1 "&
Hciattach_pid = $!
Echo $ hciattach_pid
}
Func1