From 7ce2c5be2cc81b36e56fe1badbe7135ddb013968 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 29 Apr 2019 18:50:36 +0200 Subject: [PATCH] add some more useful background examples --- examples/background-scripts/mycommands.sh | 105 ++++++++++++++++++ examples/background-scripts/run_diskusage.sh | 42 +++++++ .../background-scripts/run_filecontent.sh | 32 ++++++ examples/background-scripts/run_filename.sh | 32 ++++++ examples/background-scripts/run_notify.sh | 34 ++++++ examples/calc.sh | 4 +- examples/notify.sh | 4 +- examples/question,sh | 30 ----- examples/question.sh | 4 +- 9 files changed, 254 insertions(+), 33 deletions(-) create mode 100644 examples/background-scripts/mycommands.sh create mode 100755 examples/background-scripts/run_diskusage.sh create mode 100755 examples/background-scripts/run_filecontent.sh create mode 100755 examples/background-scripts/run_filename.sh create mode 100755 examples/background-scripts/run_notify.sh delete mode 100755 examples/question,sh diff --git a/examples/background-scripts/mycommands.sh b/examples/background-scripts/mycommands.sh new file mode 100644 index 0000000..311f061 --- /dev/null +++ b/examples/background-scripts/mycommands.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# files: mycommands.sh.dist +# copy to mycommands.sh and add all your commands an functions here ... +export res + +# your additional bahsbot commands ... +mycommands() { + + case "$MESSAGE" in + '/run_'*) + myback="run_${MESSAGE#*_}" + if [ -x "./$myback.sh" ]; then + checkback "$myback" + if [ "$res" -gt 0 ] ; then + send_normal_message "${CHAT[ID]}" "Starte $myback" + background "./$myback.sh" "$myback" + else + send_normal_message "${CHAT[ID]}" "Prozess $myback laeuft bereits." + fi + fi + ;; + '/kill_'*) + myback="run_${MESSAGE#*_}" + if [ -x "./$myback.sh" ]; then + checkback "$myback" + if [ "$res" -eq 0 ] ; then + killback "$myback" + send_normal_message "${CHAT[ID]}" "Beende $myback." + else + send_normal_message "${CHAT[ID]}" "Prozess $myback laeuft nicht." + fi + fi + ;; + esac +} + +# place your additional processing functions here ... + +# returns true if function exist +_is_function() +{ + [ "$(LC_ALL=C type -t "$1")" = "function" ] +} + +# inifnite loop for waching a given dir for new files +# $1 dir to wtach for new files +watch_dir_loop() { + local newfile old + # wait for new files in WATCHDIR + inotifywait -q -m "$1" -e create --format "%f" \ + | while true + do + # read in newfile + read -r newfile + + #skip if not match or same name as last time + [ "${newfile}" = "${old}" ] && continue + sleep 0.2 + + # process content and output message + echo "$(date) found ${newfile}" >&2 + # note: loop callback must a function in the calling script! + if _is_function loop_callback ; then + loop_callback "$1/$newfile" + else + echo "ERROR: loop_callback not found!" >&2 + fi + done +} # 2>>"$0.log" + + +output_telegram() { + # output to telegram + sed <<< "${1}" -e ':a;N;$!ba;s/\n/ mynewlinestartshere /g' +} # 2>>"$0.log" + +# name and localtion of the tml file + +# $1 string to output +# $2 file to add file to +output_html_file() { + local date + date="$(date)" + output_file "$(sed <<< "
$1
${date}
" ' + s/ my[a-z]\{3,15}\(start\|ends\)here.*
/
/g + s/ *mynewlinestartshere */
/ + s/\n/
/ + ')" +} # >>"$0.log" 2>&1 + +# $1 string to output +# $2 file to add file to +output_file() { + local publish="${2}" + [ ! -w "${publish}" ] && echo "ERROR: file ${publish} is not writeable or does not exist!" && exit + + # output at beginnung of file, add date to message + sed <<< "${1}" ' + s/ *mynewlinestartshere */\n/ + s/ my[a-z]\{3,15}\(start\|ends\)here.*//g + ' >"$publish$$" + cat "$publish" >>"$publish$$" + mv "${publish}$$" "${publish}" +} # >>"$0.log" 2>&1 + diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh new file mode 100755 index 0000000..cc66ecb --- /dev/null +++ b/examples/background-scripts/run_diskusage.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# file: run_diskcusage.sh +# example for an background job display a system value + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4 + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +# discard STDIN for background jobs! +cat >/dev/null & + +source "./mycommands.sh" + +# check if $1 is a number +re='^[0-9]+$' +if [[ $1 =~ $re ]] ; then + SLEEP="$1" +else + SLEEP=100 # time between time notifications +fi + +NEWLINE=$'\n' + +# output disk usgae every $1 seconds +WAIT=0 +while sleep $WAIT +do + output_telegram "Current Disk usage ${NEWLINE} $(df -h / /tmp /usr /var /home)" + # only for testing, delete echo line for production ... + echo "Current Disk usage ${NEWLINE} $(df -h / /tmp /usr /var /home)" + WAIT="$SLEEP" +done + diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh new file mode 100755 index 0000000..fd9215d --- /dev/null +++ b/examples/background-scripts/run_filecontent.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# file: run_filename +# background job to display content of all new files in WATCHDIR +# + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +# discard STDIN for background jobs! +cat >/dev/null & + +# watch for new logfiles +WATCHDIR="/my_special/dir_to_watch" +source "./mycommands.sh" + +# test your script and the remove ... +WATCHDIR="/tmp" + +# this is calles by watch loop +# $1 is name of the new file +loop_callback() { + # output content of file, you MUST trust creator of the file because it contest are sent as message to you! + output_telegram "Contents of ${1}: mynewlinestartshere $(cat "${1}")" +} + +watch_dir_loop "$WATCHDIR" diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh new file mode 100755 index 0000000..53bd926 --- /dev/null +++ b/examples/background-scripts/run_filename.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# file: run_filename +# background job to display all new files in WATCHDIR +# + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +# discard STDIN for background jobs! +cat >/dev/null & + +# watch for new logfiles +WATCHDIR="/var/log" +source "./mycommands.sh" + +# test your script and the remove ... +WATCHDIR="/tmp" + +# this is calles by watch loop +# $1 is name of the new file +loop_callback() { + # output one simple line ... + echo "New file ${1} created in ${WATCHDIR}!" +} + +watch_dir_loop "$WATCHDIR" diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh new file mode 100755 index 0000000..c336728 --- /dev/null +++ b/examples/background-scripts/run_notify.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# file: notify.sh +# example for an background job, run with startback notify.sh + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4 + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +# discard STDIN for background jobs! +cat >/dev/null & + +# check if $1 is a number +re='^[0-9]+$' +if [[ $1 =~ $re ]] ; then + SLEEP="$1" +else + SLEEP=10 # time between time notifications +fi + +# output current time every $1 seconds +while sleep $SLEEP +do + date "+* It's %k:%M:%S o' clock ..." +done + diff --git a/examples/calc.sh b/examples/calc.sh index a5df907..0c8cca4 100755 --- a/examples/calc.sh +++ b/examples/calc.sh @@ -1,9 +1,11 @@ #!/bin/bash +# file: calc.sh +# example for an interactive chat, run with startprog calc.sh # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.70-pre1-0-g490c472 +#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index 13c8979..c336728 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -1,8 +1,10 @@ #!/bin/bash +# file: notify.sh +# example for an background job, run with startback notify.sh # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.70-pre1-0-g490c472 +#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question,sh b/examples/question,sh deleted file mode 100755 index 42c634a..0000000 --- a/examples/question,sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -# This file is public domain in the USA and all free countries. -# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) - -#### $$VERSION$$ v0.70-pre1-0-g490c472 - -# adjust your language setting here -# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment -export 'LC_ALL=C.UTF-8' -export 'LANG=C.UTF-8' -export 'LANGUAGE=C.UTF-8' - -unset IFS -# set -f # if you are paranoid use set -f to disable globbing - -echo "Why hello there. -Would you like some tea (y/n)?" -read -r answer -[[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." -until [ "$SUCCESS" = "y" ] ;do - echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' - read -r answer - case $answer in - 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; - 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; - *) SUCCESS=n;; - esac -done -exit diff --git a/examples/question.sh b/examples/question.sh index 42c634a..86807bc 100755 --- a/examples/question.sh +++ b/examples/question.sh @@ -1,9 +1,11 @@ #!/bin/bash +# file: question.sh +# example for an interactive chat, run with startprog question.sh # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.70-pre1-0-g490c472 +#### $$VERSION$$ v0.70-pre1-11-g3dfa5e4 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment