From 4e860e357c7dff13e34978075858c792d39817bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 13 Aug 2018 07:49:22 +0200 Subject: [PATCH] Tools: add run-shellcheck.sh to statically analyze startup scripts Use './Tools/run-shellcheck.sh ROMFS/px4fmu_common' to run it. --- .../init.d-posix/1010_iris_opt_flow.post | 1 + .../init.d-posix/6011_typhoon_h480.post | 2 + ROMFS/px4fmu_common/init.d-posix/rcS | 16 +++++--- Tools/run-shellcheck.sh | 41 +++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100755 Tools/run-shellcheck.sh diff --git a/ROMFS/px4fmu_common/init.d-posix/1010_iris_opt_flow.post b/ROMFS/px4fmu_common/init.d-posix/1010_iris_opt_flow.post index c28e1986a1..45635c9633 100644 --- a/ROMFS/px4fmu_common/init.d-posix/1010_iris_opt_flow.post +++ b/ROMFS/px4fmu_common/init.d-posix/1010_iris_opt_flow.post @@ -1,4 +1,5 @@ +# shellcheck disable=SC2154 mavlink stream -r 10 -s DISTANCE_SENSOR -u $udp_gcs_port_local mavlink stream -r 10 -s VISION_POSITION_ESTIMATE -u $udp_gcs_port_local diff --git a/ROMFS/px4fmu_common/init.d-posix/6011_typhoon_h480.post b/ROMFS/px4fmu_common/init.d-posix/6011_typhoon_h480.post index 78da0d2ff3..80264373dc 100644 --- a/ROMFS/px4fmu_common/init.d-posix/6011_typhoon_h480.post +++ b/ROMFS/px4fmu_common/init.d-posix/6011_typhoon_h480.post @@ -3,6 +3,8 @@ mixer append /dev/pwm_output0 etc/mixers/mount_legs.aux.mix mavlink start -x -u 14558 -r 4000 -f -m onboard -o 14530 +# shellcheck disable=SC2154 mavlink stream -r 10 -s MOUNT_ORIENTATION -u $udp_gcs_port_local +# shellcheck disable=SC2154 mavlink stream -r 50 -s ATTITUDE_QUATERNION -u $udp_offboard_port_local mavlink stream -r 10 -s MOUNT_ORIENTATION -u $udp_offboard_port_local diff --git a/ROMFS/px4fmu_common/init.d-posix/rcS b/ROMFS/px4fmu_common/init.d-posix/rcS index e8a8a4626f..3ca18d6456 100644 --- a/ROMFS/px4fmu_common/init.d-posix/rcS +++ b/ROMFS/px4fmu_common/init.d-posix/rcS @@ -2,13 +2,17 @@ # PX4 commands need the 'px4-' prefix in bash. # (px4-alias.sh is expected to be in the PATH) +# shellcheck disable=SC1091 source px4-alias.sh +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + # # Main SITL startup script # # check for ekf2 replay +# shellcheck disable=SC2154 if [ "$replay_mode" == "ekf2" ] then sh etc/init.d-posix/rc.replay @@ -96,11 +100,12 @@ else fi # multi-instance setup -param set MAV_SYS_ID $((1+$px4_instance)) -simulator_udp_port=$((14560+$px4_instance)) -udp_offboard_port_local=$((14557+$px4_instance)) -udp_offboard_port_remote=$((14540+$px4_instance)) -udp_gcs_port_local=$((14556+$px4_instance)) +# shellcheck disable=SC2154 +param set MAV_SYS_ID $((1+px4_instance)) +simulator_udp_port=$((14560+px4_instance)) +udp_offboard_port_local=$((14557+px4_instance)) +udp_offboard_port_remote=$((14540+px4_instance)) +udp_gcs_port_local=$((14556+px4_instance)) if [ $AUTOCNF == yes ] then @@ -163,6 +168,7 @@ fi # Autostart ID autostart_file_match="etc/init.d-posix/$(param show -q SYS_AUTOSTART)_*" +# shellcheck disable=SC2206 autostart_files=( $autostart_file_match ) autostart_file="${autostart_files[0]}" # use first match, but there should really only be one if [ ! -e "$autostart_file" ]; then diff --git a/Tools/run-shellcheck.sh b/Tools/run-shellcheck.sh new file mode 100755 index 0000000000..44640cf5a5 --- /dev/null +++ b/Tools/run-shellcheck.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Script to run ShellCheck (a static analysis tool for shell scripts) over a +# script directory + +if [ -z "$1" ]; then + echo "usage: $0 " + echo "" + echo " Directory to search for scripts" + exit -1 +fi +search_directory="$1" + +command -v shellcheck >/dev/null 2>&1 || { echo -e >&2 \ +"Error: shellcheck required but it's not installed. On Ubuntu use:\n sudo apt-get install shellcheck\n\nAborting."; exit 1; } + +scripts="$(find "$search_directory" -type f ! -name '*.txt' ! -name '*.mix')" + +echo "Running shellcheck in '$search_directory'." + +# Disabled rules: +# SC2121: allow 'set' as assignment (NuttX-style) +# SC1008: unrecognized shebang +# SC2086: double quote to prevent globbing and word splitting +# SC2166: allow the form [ $OUTPUT_MODE == fmu -o $OUTPUT_MODE == io ] +# SC2148: allow files w/o shebang +shellcheck -a -x -e SC2121 -e SC1008 -e SC2086 -e SC2166 -e SC2148 \ + $scripts +ret=$? +if [ $ret -ne 0 ]; then + echo "Please fix the above script problems." + echo "If an error is raised that should be ignored, \ +add the following right before the offending line:" + echo "# shellcheck disable=SCxxxx" + echo "" + echo "Re-run the script with '$0 $@'" + exit $ret +fi + +echo "No problems found." +exit 0