From a13ecbcfcf7a8d09abb3bec06f09f317dbcff44b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 11 Apr 2019 09:59:56 +0200 Subject: [PATCH] Bashbot Version 0.51 shellcheck testet --- README.md | 2 +- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 54 +++++++++++++++++++++++++++++++++++------------ notify | 2 +- question | 2 +- version | 2 +- 13 files changed, 52 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index fc62974..d1eaa7b 100644 --- a/README.md +++ b/README.md @@ -73,4 +73,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/bashbot.cron b/bashbot.cron index d7a371b..3ea993f 100644 --- a/bashbot.cron +++ b/bashbot.cron @@ -7,7 +7,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 9624f9c..fadd19d 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 56f26d9..c86f222 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,7 +10,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/commands.sh b/commands.sh index 88c3638..35c908d 100755 --- a/commands.sh +++ b/commands.sh @@ -4,7 +4,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 934f3fe..62527f0 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -70,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ``` 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/doc/2_usage.md b/doc/2_usage.md index acf7b30..b1d444f 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index eabd071..7e8425a 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -97,5 +97,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/doc/4_expert.md b/doc/4_expert.md index e5d1fee..b82e0bc 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -102,5 +102,5 @@ An example crontab is provided in ```bashbot.cron```. - if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/doc/5_practice.md b/doc/5_practice.md index 289921b..a66a662 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -25,7 +25,7 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh: after editing commands.sh restart Bot. ### Seperate Bot logic from command -If a Bot command needs more than 2-3 lines of code I recommend to factor it out to bash functions in a seperate file, e.g. +If a Bot command needs more than 2-3 lines of code I recommend to factor it out to a bash function in a seperate file, e.g. ```mybotcommands.inc.sh``` and source the file from bashbot.sh. ```bash source mybotcommands.inc.sh @@ -44,19 +44,16 @@ If a Bot command needs more than 2-3 lines of code I recommend to factor it out send_action "${CHAT[ID]}" "typing" send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash." ;; - [...] + #[...] esac ``` -Doing it this way keeps command.sh small and clean, while allowing complex tasks to be done in the included function. example ```mybotcommands.inc.sh```: +Doing it this way keeps commands.sh small and clean, while allowing complex tasks to be done in the included function. example ```mybotcommands.inc.sh```: ```bash #!/bin/bash # process_message() { - - local MESSAGE="$1" # store arg - local ARGS="${MESSAGE#/r* }" # remove command - local TEXT="" - local OUTPUT="" + local ARGS="${1#/* }" # remove command /* + local TEXT OUTPUT="" # process every word in MESSAGE, avoid globbing from MESSAGE set -f @@ -65,16 +62,13 @@ process_message() { set +f # process links if [[ "$WORD" == "https://"* ]]; then - # remove utf chars from URL - WORD="$(echo "$WORD" | uni2ascii -q -a F -B)" REPORT="$(dosomething_with_link "$WORD")" # no link, add as text else - # TEXT incl UTF to ascii transformation - TEXT="$(echo "${TEXT} $WORD"'| iconv -c -f utf-8 -t ascii//TRANSLIT)" + TEXT="$(echo "${TEXT} $WORD")" continue fi - # compose result components + # compose result OUTPUT="* ${REPORT} ${WORD} ${TEXT}" TEXT="" done @@ -87,6 +81,38 @@ process_message() { ``` ### Test your Bot with shellcheck +Shellcheck is a static linter for shell scripts providing excellent tips and hints for shell coding pittfalls. You can [use it online](https://www.shellcheck.net/) or [install it on your system](https://github.com/koalaman/shellcheck#installing). -#### $$VERSION$$ v0.51-0-g4d5d386 +All bashbot scripts are linted by shellcheck. + +Shellcheck examples: +```bash +$ shellcheck -x mybotcommands.inc.sh + +Line 17: + TEXT="$(echo "${TEXT} $WORD")" + ^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'. + +``` +```bash +$ shellcheck -x notify +OK +$ shellcheck -x question +OK +$ shellcheck -x commands.sh +OK +$ shellcheck -x bashbot.sh + +In bashbot.sh line 123: + text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts + ^-- SC2001: See if you can use ${variable//search/replace} instead. + + +In bashbot.sh line 490: + CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + ^-- SC2034: CONTACT appears unused. Verify it or export it. +``` +As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) + +#### $$VERSION$$ v0.51-0-g0356270 diff --git a/notify b/notify index bdf25d5..9b2335f 100755 --- a/notify +++ b/notify @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 701b7dd..1ea360f 100755 --- a/question +++ b/question @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 0ef84f3..a82271d 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.51-0-g4ddd122 +#### $$VERSION$$ v0.51-0-g0356270 # shellcheck disable=SC2016 # # Easy Versioning in git: