Fix shellcheck warnings
Fix shellcheck warnings in create_usbhid.sh script.
Tested: HID gadget was connected/disconnected properly even with the
fixed script.
Signed-off-by: Jae Hyun Yoo <quic_jaehyoo@quicinc.com>
Change-Id: I78ae45db2f50ef400d72dd92895e67c50d01753e
diff --git a/create_usbhid.sh b/create_usbhid.sh
index 12eebd0..fcde89a 100644
--- a/create_usbhid.sh
+++ b/create_usbhid.sh
@@ -6,7 +6,7 @@
create_hid() {
# create gadget
mkdir "${hid_conf_directory}"
- cd "${hid_conf_directory}"
+ cd "${hid_conf_directory}" || return
# add basic information
echo 0x0100 > bcdDevice
@@ -61,7 +61,7 @@
# 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
# 0x81, 0x00, // INPUT (Data,Ary,Abs)
# 0xc0 // END_COLLECTION
- echo -ne '\x05\x01\x09\x06\xa1\x01\x05\x07\x19\xe0\x29\xe7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07\x19\x00\x29\x65\x81\x00\xc0' > functions/hid.0/report_desc
+ printf '\x05\x01\x09\x06\xa1\x01\x05\x07\x19\xe0\x29\xe7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07\x19\x00\x29\x65\x81\x00\xc0' > functions/hid.0/report_desc
# Create HID mouse function
mkdir functions/hid.1
@@ -109,7 +109,7 @@
# 0x81, 0x06, // INPUT (Data,Var,Rel)
# 0xc0, // END_COLLECTION
# 0xc0 // END_COLLECTION
- echo -ne '\x05\x01\x09\x02\xa1\x01\x09\x01\xa1\x00\x05\x09\x19\x01\x29\x03\x15\x00\x25\x01\x95\x03\x75\x01\x81\x02\x95\x01\x75\x05\x81\x03\x05\x01\x09\x30\x09\x31\x35\x00\x46\xff\x7f\x15\x00\x26\xff\x7f\x65\x11\x55\x00\x75\x10\x95\x02\x81\x02\x09\x38\x15\xff\x25\x01\x35\x00\x45\x00\x75\x08\x95\x01\x81\x06\xc0\xc0' > functions/hid.1/report_desc
+ printf '\x05\x01\x09\x02\xa1\x01\x09\x01\xa1\x00\x05\x09\x19\x01\x29\x03\x15\x00\x25\x01\x95\x03\x75\x01\x81\x02\x95\x01\x75\x05\x81\x03\x05\x01\x09\x30\x09\x31\x35\x00\x46\xff\x7f\x15\x00\x26\xff\x7f\x65\x11\x55\x00\x75\x10\x95\x02\x81\x02\x09\x38\x15\xff\x25\x01\x35\x00\x45\x00\x75\x08\x95\x01\x81\x06\xc0\xc0' > functions/hid.1/report_desc
# Create configuration
mkdir configs/c.1
@@ -125,13 +125,13 @@
}
connect_hid() {
- if ! [[ `cat UDC` =~ "${dev_name}:p" ]]; then
+ if ! grep -q "${dev_name}:p" UDC; then
i=0
num_ports=5
base_usb_dir="/sys/bus/platform/devices/${dev_name}/${dev_name}:p"
- while [ $i -lt $num_ports ]; do
- port=$(($i + 1))
- i=$port
+ while [ "${i}" -lt "${num_ports}" ]; do
+ port=$(("${i}" + 1))
+ i="${port}"
if [ ! -e "${base_usb_dir}${port}/gadget/suspended" ]; then
break
fi
@@ -141,17 +141,15 @@
}
disconnect_hid() {
- if [[ `cat UDC` =~ "${dev_name}:p" ]]; then
+ if grep -q "${dev_name}:p" UDC; then
echo "" > UDC
fi
}
-original_directory="$(pwd)"
-
if [ ! -e "${hid_conf_directory}" ]; then
create_hid
else
- cd "${hid_conf_directory}"
+ cd "${hid_conf_directory}" || exit
fi
if [ "$1" = "connect" ]; then
@@ -159,7 +157,6 @@
elif [ "$1" = "disconnect" ]; then
disconnect_hid
else
- echo "Invalid option: $1. Use 'connect' or 'disconnect'."
+ echo >&2 "Invalid option: $1. Use 'connect' or 'disconnect'."
+ exit 1
fi
-
-cd "${original_directory}"