From 7253a5abf1e4b1aaa06a738adbafcc6db939b891 Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:13:02 -0800 Subject: [PATCH 1/7] style: remove optional $ from arithmetic expansion Remove the optional dollar signs from arithmetic expansions. SHELLCHECK: SC2004 --- cpp_competitive_template.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp_competitive_template.sh b/cpp_competitive_template.sh index eac99bf..105a327 100755 --- a/cpp_competitive_template.sh +++ b/cpp_competitive_template.sh @@ -32,16 +32,16 @@ main() { exit 1 fi - if (($contest_flag > 0)); then + if ((contest_flag > 0)); then echo "creating contest problems..." problems=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T") - for ((i=0;i<$num_problems;i++)); + for ((i=0;i 0)) ;then + elif ((specify_flag > 0)) ;then names=$(echo $problem_names | tr "," " ") for name in $names From e0653f28039c80320e36c62ef0c406bf3bde0368 Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:18:53 -0800 Subject: [PATCH 2/7] fix: add default case to case statements Add a default (*) case to scripts with case statements, so the scripts print usage information and exit with an error code when an invalid flag is used. SHELLCHECK: SC2220 --- cpp_competitive_template.sh | 15 +++++++++------ run_script.sh | 7 +++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cpp_competitive_template.sh b/cpp_competitive_template.sh index 105a327..9c3a863 100755 --- a/cpp_competitive_template.sh +++ b/cpp_competitive_template.sh @@ -14,14 +14,17 @@ main() { while getopts "c:s:h" flag; do case $flag in c) - num_problems=$OPTARG - contest_flag=1 ;; + num_problems=$OPTARG + contest_flag=1 ;; s) - problem_names=$OPTARG - specify_flag=1 ;; + problem_names=$OPTARG + specify_flag=1 ;; h) - print_usage - exit ;; + print_usage + exit ;; + *) + print_usage + exit 1 ;; esac done diff --git a/run_script.sh b/run_script.sh index f292e5a..d9945c6 100755 --- a/run_script.sh +++ b/run_script.sh @@ -14,8 +14,11 @@ main() { while getopts "h" flag; do case $flag in h) - print_usage - exit ;; + print_usage + exit ;; + *) + print_usage + exit 1 ;; esac done From 64d64edfe0bbab17ebe1138e5dc65f5d86aa88ad Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:21:27 -0800 Subject: [PATCH 3/7] refactor: remove unused variables from run_script.sh Remove unused variables from run_script.sh, as pointed out by shellcheck. SHELLCHECK: SC2034 --- run_script.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/run_script.sh b/run_script.sh index d9945c6..bda1f94 100755 --- a/run_script.sh +++ b/run_script.sh @@ -8,9 +8,6 @@ main() { fi # get flags - contest_flag=0 - specify_flag=0 - num_problems=0 while getopts "h" flag; do case $flag in h) From cd162fa12de1df157e2164ca1672fea199f9bfb0 Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:27:02 -0800 Subject: [PATCH 4/7] refactor: replace escape-heavy strings with heredocs Replace strings with a heavy use of escape characters with heredocs, so the backslashes are not taken literally and don't have to be interpreted by a builtin like `echo -e`. SHELLCHECK: SC2089 --- c_template.sh | 24 +++++++++++++----------- cpp_competitive_template.sh | 33 ++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/c_template.sh b/c_template.sh index 64b142b..1ab1f9a 100755 --- a/c_template.sh +++ b/c_template.sh @@ -7,15 +7,17 @@ fi filename="$1" -str="#include -\n#include -\n\nint main() -\n{ - \n\tint aux;\n - \n\tscanf(\"%d\", &aux);\n - \n\tprintf(\"%d\", aux);\n - \n\treturn 0; -\n}" +cat << EOF > "$filename.c" +#include +#include -echo -e $str > "$filename.c" -echo "Done!" \ No newline at end of file +int main() +{ +\tint aux; +\tscanf("%d", &aux); +\tprintf("%d", aux); +\treturn 0; +} +EOF + +echo "Done!" diff --git a/cpp_competitive_template.sh b/cpp_competitive_template.sh index 9c3a863..69d3b90 100755 --- a/cpp_competitive_template.sh +++ b/cpp_competitive_template.sh @@ -63,22 +63,25 @@ main() { } create_file() { - local code="#include - \nusing namespace std; - \n#define DEBUG(x) cout << #x << \" >>>> \" << x << endl - \n#define MID(l, r) (l + (r - l) / 2) - \n#define CEILDIVISION(x, y) ((x + y - 1) / y) - \n#define INF (int)1e9 - \n#define LONGINF (long long)1e18 - \n#define MEM(arr, val) memset(arr, (val), sizeof(arr)) - \n#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); - \nconst int MOD = 1000000007; // 10^9 - 7 - \n - \nint main() { - \n\tFASTIO;\n\t\n\t\n\treturn 0; - \n}" + cat << EOF > "$1.cpp" +#include +using namespace std; +#define DEBUG(x) cout << #x << " >>>> " << x << endl +#define MID(l, r) (l + (r - l) / 2) +#define CEILDIVISION(x, y) ((x + y - 1) / y) +#define INF (int)1e9 +#define LONGINF (long long)1e18 +#define MEM(arr, val) memset(arr, (val), sizeof(arr)) +#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +const int MOD = 1000000007; // 10^9 - 7 - echo -e $code > "$1.cpp" +int main() { +\tFASTIO; + + +\treturn 0; +} +EOF } create_io_files() { From 8c5a8fc9865f4643aa44939d11fe0f3195aab7a4 Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:32:42 -0800 Subject: [PATCH 5/7] refactor: double quote variables to prevent globs and splits Double quote environment variables when they are used to prevent globbing and word splitting. SHELLCHECK: SC2086 --- cpp_competitive_template.sh | 8 ++++---- create_bootable_usbstick.sh | 8 ++++---- pdf2csv.sh | 8 ++++---- run_script.sh | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cpp_competitive_template.sh b/cpp_competitive_template.sh index 69d3b90..7d34899 100755 --- a/cpp_competitive_template.sh +++ b/cpp_competitive_template.sh @@ -42,20 +42,20 @@ main() { for ((i=0;i 0)) ;then - names=$(echo $problem_names | tr "," " ") + names=$(echo "$problem_names" | tr "," " ") for name in $names do echo "creating file $name.cpp..." - create_file $name + create_file "$name" done else filename="$1" echo "creating file $filename.cpp..." - create_file $filename + create_file "$filename" fi create_io_files diff --git a/create_bootable_usbstick.sh b/create_bootable_usbstick.sh index e79d4a7..73d0123 100755 --- a/create_bootable_usbstick.sh +++ b/create_bootable_usbstick.sh @@ -14,11 +14,11 @@ if [ "$os" = "linux" ]; then deviceToUmount="${1}1" - sudo umount $deviceToUmount + sudo umount "$deviceToUmount" echo "Unmounting device..." - sudo dd bs=4M if=$2 of=$1 status=progress oflag=sync + sudo dd bs=4M if="$2" of="$1" status=progress oflag=sync echo "Done!" @@ -37,11 +37,11 @@ elif [ "$os" = "windows" ]; then deviceToUmount="${1}1" - sudo umount $deviceToUmount + sudo umount "$deviceToUmount" echo "Unmounting device..." - sudo woeusb -v --target-filesystem NTFS --device $2 $1 + sudo woeusb -v --target-filesystem NTFS --device "$2" "$1" echo $'This script requires woeusb to be installed, install it using the following PPA:\nsudo add-apt-repository ppa:nilarimogard/webupd8\nsudo apt update\nsudo apt install woeusb' diff --git a/pdf2csv.sh b/pdf2csv.sh index 8c6f45f..2e036fe 100755 --- a/pdf2csv.sh +++ b/pdf2csv.sh @@ -7,7 +7,7 @@ fi echo "Converting pdf to txt..." -pdftotext -layout $1 $2.txt +pdftotext -layout "$1" "$2.txt" # After convert it to txt we have to parse through the lines and separate the fields by commas @@ -15,9 +15,9 @@ echo "Converting txt to csv..." # Let's add an space after the first field, replace the commas with dots and then separate them -awk '{ sub(/^[ \t]+/, ""); print}' $2.txt | sed 's/ / /' | awk '{gsub(/,/,".")} 1' | -awk -F'[[:space:]][[:space:]]+' 'BEGIN{OFS=",";} {print $1, $2, $3, $4, $5, $6, $7}' > $2.csv +awk '{ sub(/^[ \t]+/, ""); print}' "$2.txt" | sed 's/ / /' | awk '{gsub(/,/,".")} 1' | +awk -F'[[:space:]][[:space:]]+' 'BEGIN{OFS=",";} {print $1, $2, $3, $4, $5, $6, $7}' > "$2.csv" -rm $2.txt +rm "$2.txt" echo "Done!" diff --git a/run_script.sh b/run_script.sh index bda1f94..e75028a 100755 --- a/run_script.sh +++ b/run_script.sh @@ -20,11 +20,11 @@ main() { done filename="$1" - run_file $filename + run_file "$filename" } run_file() { - g++ $1 && ./a.out -fmax-errors=1 out.txt + g++ "$1" && ./a.out -fmax-errors=1 out.txt } print_usage() { From 70e0045abdc8ee14da5bf97d8b7e50474842c8c4 Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:36:03 -0800 Subject: [PATCH 6/7] refactor: prevent backslash mangling in read Prevent mangling of backslashes in read by passing it the `-r` flag. This way backslashes cannot be used by the user as escape characters. SHELLCHECK: SC2162 --- create_bootable_usbstick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create_bootable_usbstick.sh b/create_bootable_usbstick.sh index 73d0123..aead588 100755 --- a/create_bootable_usbstick.sh +++ b/create_bootable_usbstick.sh @@ -8,7 +8,7 @@ if (($# != 2)); then fi -read -p $'\nEnter the Operating System: For Windows type: windows | For Linux type: linux \n' os +read -rp $'\nEnter the Operating System: For Windows type: windows | For Linux type: linux \n' os if [ "$os" = "linux" ]; then From a0de3b1b79d0dcf08fea89bfbeee0dc289af91ae Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Sun, 5 Nov 2023 20:39:32 -0800 Subject: [PATCH 7/7] ci: invoke shellcheck on push and prs Invoke ShellCheck when pushing to master or when pull requests are opened. --- .github/workflows/ci.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fec41d9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,18 @@ +name: CI + +on: + pull_request: {} + push: + branches: [ master ] + +jobs: + shellcheck: + name: ShellCheck + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - uses: actions/checkout@v4 + - uses: ludeeus/action-shellcheck@2.0.0 + with: + severity: warning