From bc481d385463da33492ded1584d2e79406f7337b Mon Sep 17 00:00:00 2001 From: Andre Maroneze <andre.maroneze@cea.fr> Date: Mon, 22 Mar 2021 10:03:01 +0100 Subject: [PATCH] [bin/analysis-scripts] improve shell scripts thanks to shellcheck --- bin/check-reference-configuration.sh | 21 +++++---------------- bin/check_newline.sh | 7 +++---- bin/frama-c-script | 26 +++++++++++--------------- share/analysis-scripts/cmd-dep.sh | 12 ++++++------ 4 files changed, 25 insertions(+), 41 deletions(-) diff --git a/bin/check-reference-configuration.sh b/bin/check-reference-configuration.sh index 4117369c5cc..1c718e5ce4b 100755 --- a/bin/check-reference-configuration.sh +++ b/bin/check-reference-configuration.sh @@ -17,19 +17,10 @@ fi version_via_opam() { v=$(opam info -f installed-version "$1" 2>/dev/null) - if [ "$v" = "" -o "$v" = "--" ]; then + if [ "$v" = "" ] || [ "$v" = "--" ]; then echo "NOT" else - echo $v - fi -} - -version_via_ocamlfind() { - v=$(ocamlfind query -format "$1" 2>/dev/null) - if [ "$v" = "" ]; then - echo "NOT" - else - echo $v + echo "$v" fi } @@ -54,13 +45,11 @@ all_packages="" for package in $packages; do name=${package%%.*} all_packages+=" $package" - working_version=$(echo $package | sed 's/^[^.]*\.//') + working_version=$(echo "$package" | sed 's/^[^.]*\.//') if [ "$opam" != "NOT" ]; then - actual_version=$(version_via_opam $name) - elif [ "$ocamlfind" != "NOT" ]; then - actual_version=$(version_via_ocamlfind $name) + actual_version=$(version_via_opam "$name") else - echo "error: neither opam nor ocamlfind found." + echo "error: opam not found." exit 1 fi if [ "$working_version" != "$actual_version" ]; then diff --git a/bin/check_newline.sh b/bin/check_newline.sh index c91311fb289..6546a6f8a13 100755 --- a/bin/check_newline.sh +++ b/bin/check_newline.sh @@ -19,19 +19,18 @@ is_likely_text_file() { errors=0 IFS='' -file -f "$1" --mime | grep '\btext' | cut -d: -f1 | while read file do if [ -n "$(is_likely_text_file "$file")" ]; then x=$(tail -c 1 "$file") - if [ "$x" != "" -a "$file" != "VERSION" -a "$file" != "VERSION_CODENAME" ]; then + if [ "$x" != "" ] && [ "$file" != "VERSION" ] && [ "$file" != "VERSION_CODENAME" ]; then echo "error: no newline at end of file: $file" errors=$((errors+1)) fi fi -done +done < <(file -f "$1" --mime | grep '\btext' | cut -d: -f1) if [ $errors -gt 0 ]; then - echo "Found $error(s) files with errors." + echo "Found $errors file(s) with errors." exit 0 fi diff --git a/bin/frama-c-script b/bin/frama-c-script index 7b63bf7f459..0179f39113f 100755 --- a/bin/frama-c-script +++ b/bin/frama-c-script @@ -22,7 +22,7 @@ ########################################################################## # Accept '-check' to avoid issues with ptests -while [ $# -ge 1 -a "$1" = "-check" ]; do +while [ $# -ge 1 ] && [ "$1" = "-check" ]; do shift done @@ -78,7 +78,7 @@ usage() { echo " Applies some transformations to an existing compile_commands.json" echo " (such as relativizing paths) to improve portability." echo " [default: ./compile_commands.json]" - exit $1 + exit "$1" } if [ $# -lt 1 ]; then @@ -141,11 +141,7 @@ flamegraph() { dir="$FRAMAC_SESSION" fi if [ ! -d "$dir" ]; then - mkdir "$dir" - if [ $? -ne 0 ]; then - echo "error: could not create '$dir'" - exit 1 - fi + mkdir "$dir" || { echo "error: could not create '$dir'"; exit 1; } fi out_svg="$dir/flamegraph.svg" "${FRAMAC_SHARE}/analysis-scripts/flamegraph.pl" \ @@ -179,7 +175,7 @@ configure_for_frama_c() { echo "error: 'configure' command requires a machdep"; exit 1 fi - MACHDEP="$(echo $1 | tr a-z A-Z)" + MACHDEP=${1^^} # to uppercase shift CPP="gcc -E -nostdinc -fno-builtin -I${FRAMAC_SHARE}/libc -D__FC_MACHDEP_${MACHDEP}" ./configure "$@" } @@ -190,22 +186,22 @@ case "$command" in ;; "make-template") shift; - ${FRAMAC_SHARE}/analysis-scripts/make_template.py "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/make_template.py "$@"; ;; "list-files") shift; - ${FRAMAC_SHARE}/analysis-scripts/list_files.py "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/list_files.py "$@"; ;; "list-functions") shift; # to avoid a slow startup, we only load plugins which perform syntactic # transformations. This may trigger annotation errors due to missing # plugins, so we disable those - ${DIR}/frama-c "$@" -no-autoload-plugins -load-module variadic,instantiate,${FRAMAC_SHARE}/analysis-scripts/list_functions.ml -kernel-warn-key annot-error=inactive -kernel-verbose 0; + "${DIR}"/frama-c "$@" -no-autoload-plugins -load-module variadic,instantiate,"${FRAMAC_SHARE}"/analysis-scripts/list_functions.ml -kernel-warn-key annot-error=inactive -kernel-verbose 0; ;; "find-fun") shift; - ${FRAMAC_SHARE}/analysis-scripts/find_fun.py "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/find_fun.py "$@"; ;; "flamegraph") shift; @@ -213,7 +209,7 @@ case "$command" in ;; "summary") shift; - ${FRAMAC_SHARE}/analysis-scripts/summary.py "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/summary.py "$@"; ;; "configure") shift; @@ -221,11 +217,11 @@ case "$command" in ;; "make-wrapper") shift; - ${FRAMAC_SHARE}/analysis-scripts/make_wrapper.py "$0" "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/make_wrapper.py "$0" "$@"; ;; "normalize-jcdb") shift; - ${FRAMAC_SHARE}/analysis-scripts/normalize_jcdb.py "$@"; + "${FRAMAC_SHARE}"/analysis-scripts/normalize_jcdb.py "$@"; ;; *) echo "error: unrecognized command: $command"; diff --git a/share/analysis-scripts/cmd-dep.sh b/share/analysis-scripts/cmd-dep.sh index 7dc15d918d3..d045b1cf563 100755 --- a/share/analysis-scripts/cmd-dep.sh +++ b/share/analysis-scripts/cmd-dep.sh @@ -6,7 +6,7 @@ then ( echo "usage: $0 FILE STRING" echo "Test whether the contents of FILE are different from STRING." \ - "If it does, FILE is updated to match STRING. The file"Â \ + "If it does, FILE is updated to match STRING. The file" \ "name is always printed." ) >&2 exit 1 @@ -17,11 +17,11 @@ shift STRING=$* if - [ ! -e $FILE ] || - ! (diff --brief --ignore-space-change $FILE - >/dev/null <<< "$STRING") + [ ! -e "$FILE" ] || + ! (diff --brief --ignore-space-change "$FILE" - >/dev/null <<< "$STRING") then - mkdir -p $(dirname "$FILE") - echo $STRING > "$FILE" + mkdir -p "$(dirname "$FILE")" + echo "$STRING" > "$FILE" fi -echo $FILE +echo "$FILE" -- GitLab