From 876361f5625b789da04dac3f86c2dd840598795d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 14:58:29 +0200 Subject: [PATCH] install gnu utils on BSD and MacOS ... --- README.html | 8 ++++---- README.md | 10 +++++----- README.txt | 17 ++++++++-------- bashbot.sh | 31 ++++++++++++++++------------- doc/0_install.md | 13 ++++++++----- examples/bash2env.sh | 46 ++++++++++++++++++++++++++++++-------------- 6 files changed, 76 insertions(+), 49 deletions(-) diff --git a/README.html b/README.html index 4d337e6..2f448c3 100644 --- a/README.html +++ b/README.html @@ -91,7 +91,8 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel

Released to the public domain wherever applicable. Elsewhere, consider it released under the WTFPLv2.

Prerequisites

Uses JSON.sh, but no more TMUX.

-

Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. More concret on the common commands provided by coreutils, busybox or toybox, see Developer Notes

+

Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. More concret on the common commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes

+

Note for MacOS and BSD Users: As bashbot use behaivior of recent bash and (gnu)sed versions, bashbot may not run without installing additional software, see Install Bashbot

Bashbot Documentation and Downloads are availible on www.github.com

Documentation

Your really first bashbot in a nutshell

-

Note for MacOS: you must install install a more current bash, see Install Bashbot

-

To install and run bashbot you need acess to a linux/unix/bsd command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-(

+

To install and run bashbot you need acess to a linux/unix command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-(

In addition you need a Telegram client and a mobile phone to register an account. If you don't want to register for Telegram you should stop reading here ;-)

After you're registered to Telegram send a message to @botfather, create a new Telegram Bot token and write it down. You need the token to install the bot.

Now open a linux/unix/bsd terminal and check if bash is installed: which bash && echo "bash installed!". If you get an error message bash is not installed.

@@ -228,6 +228,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ V0.94-2-gced78d3

+

$$VERSION$$ V0.94-7-g3d92bf3

diff --git a/README.md b/README.md index fdde078..e3db184 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. -More concret on the common commands provided by [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands) +More concret on the common commands provided by recent versions of [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands) + +Note for MacOS and BSD Users: As bashbot use behaivior of recent bash and (gnu)sed versions, bashbot may not run without installing additional software, see [Install Bashbot](doc/0_install.md) Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on www.github.com @@ -60,9 +62,7 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do ### Your really first bashbot in a nutshell -Note for MacOS: you must install install a more current bash, see [Install Bashbot](doc/0_install.md) - -To install and run bashbot you need acess to a linux/unix/bsd command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-( +To install and run bashbot you need acess to a linux/unix command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-( In addition you need a [Telegram client](https://telegram.org) and a mobile phone to [register an account](https://telegramguide.com/create-a-telegram-account/). If you don't want to register for Telegram you should stop reading here ;-) @@ -192,4 +192,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ V0.94-2-gced78d3 +#### $$VERSION$$ V0.94-7-g3d92bf3 diff --git a/README.txt b/README.txt index 490cded..a3740c2 100644 --- a/README.txt +++ b/README.txt @@ -17,12 +17,16 @@ Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. -More concret on the common commands provided by +More concret on the common commands provided by recent versions of [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands) +Note for MacOS and BSD Users: As bashbot use behaivior of recent bash and +(gnu)sed versions, bashbot may not run without installing additional software, +see [Install Bashbot](doc/0_install.md) + Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are @@ -71,12 +75,9 @@ availible on www.github.com ### Your really first bashbot in a nutshell -Note for MacOS: you must install install a more current bash, see [Install -Bashbot](doc/0_install.md) - -To install and run bashbot you need acess to a linux/unix/bsd command line. If -you don't know how to get accces to a linux/unix/bsd like command line you -should stop reading here :-( +To install and run bashbot you need acess to a linux/unix command line. If you +don't know how to get accces to a linux/unix/bsd like command line you should +stop reading here :-( In addition you need a [Telegram client](https://telegram.org) and a mobile phone to [register an @@ -269,4 +270,4 @@ tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ V0.94-2-gced78d3 +#### $$VERSION$$ V0.94-7-g3d92bf3 diff --git a/bashbot.sh b/bashbot.sh index c4148af..f7b5d04 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.94-6-gdcf6534 +#### $$VERSION$$ V0.94-7-g3d92bf3 # # Exit Codes: # - 0 sucess (hopefully) @@ -111,12 +111,9 @@ if [ -z "${BOTTOKEN}" ]; then printf '%s\n' "${BOTTOKEN}" > "${TOKENFILE}" fi fi - if [ "$(cat -v "${TOKENFILE}" | wc | sed -e 's/[[:space:]]//g')" != "1146" ]; then - echo -e "${ORANGE}Warning, something may wrong with your token file.${NC}" - echo -e "${ORANGE}The file musst be 1 newline, 1 word, and 46 byte, your's is:${NC}\c" - cat -v "${TOKENFILE}" | wc | sed -e 's/[[:space:]]\+/ /g' - fi + # read BOTTOKEN from file and removen everyting from first newline to end BOTTOKEN="$(< "${TOKENFILE}")" + BOTTOKEN="${BOTTOKEN%%$'\n'*}" # setup botadmin file if [ ! -f "${BOTADMIN}" ]; then @@ -155,16 +152,24 @@ if [ -z "${BOTTOKEN}" ]; then exit 2 fi fi -if [[ ! "${BOTTOKEN}" =~ ^[0-9]+:[a-zA-Z0-9_-]+$ ]]; then - echo -e "${ORANGE}Warning, your bottoken may incorrent. it sould have the following format:${NC}" +# do we have BSD sed +if ! sed '1ia' /dev/null; then + echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" +fi +# BOTTOKEN format checks +if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then + echo -e "${ORANGE}Warning, your bottoken may incorrect. it should have the following format:${NC}" echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c" - echo -e "${GREY}9 digits${RED}:${GREY}35 alnum characters + '_-'${NC}" - echo -e "${ORANGE}Your current token is: '${GREY}$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")' (one $ is ok){NC}" -fi -if [ "${BOTTOKEN#bot}" != "${BOTTOKEN}" ]; then - echo -e "${ORANGE}Warning, your token starts with '${GREY}bot${NC}${ORANGE}', did you forget to remove it?.${NC}" + echo -e "${GREY}8-10 digits${RED}:${GREY}35 alnum characters + '_-'${NC}" + echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}" + [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]] &&\ + echo -e "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NC}" + [[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ + echo -e "${ORANGE}Posilbe problem in the charatcers part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" fi +exit + ################## # here we start with the real stuff URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" diff --git a/doc/0_install.md b/doc/0_install.md index 66207bf..1b054b2 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -43,13 +43,16 @@ If you modified ```commands.sh``` move your changes to ```mycommands.sh```, this Now you can restart your bashbot instances. -### Note for MacOS Users +### Note for BSD and MacOS -You must install a more current version of bash, as the default bash is way to old, +**On MacOS** You must install a more current version of bash, as the default bash is way to old, see e.g. [Install Bash on Mac](http://macappstore.org/bash/) -After installation / update of bashbot you must change the shebang line to point to your bash location -e.g. with the provided script ```bash -c "examples/bash2env *.sh */*.sh"``` +**On BSD and MacOS** I recommend to install the gnu variants of coreutils and include them in front of your PATH +environment variable before running bashbot, e.g. sed, grep, find + +In adition you must adjust the shebang line of the scripts ```bashbot.sh``` and ```json.sh``` to point to to the correct bash +or use the example script: ```examples/bash2env *.sh */*.sh``` We stay with /bin/bash shebang, because using the system bash is more save, see [Security Considerations](../README.md#Security-Considerations) @@ -98,5 +101,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ V0.94-4-gf0a7d85 +#### $$VERSION$$ V0.94-7-g3d92bf3 diff --git a/examples/bash2env.sh b/examples/bash2env.sh index c0ee605..15a81cf 100755 --- a/examples/bash2env.sh +++ b/examples/bash2env.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # file: bash2env.sh # simole helper script to convert bash shebang from # ! /bin/bash TO ! /usr/bin/env bash @@ -6,7 +6,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.94-0-gbdb50c8 +#### $$VERSION$$ V0.94-7-g3d92bf3 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -15,23 +15,41 @@ export 'LANG=C.UTF-8' export 'LANGUAGE=C.UTF-8' unset IFS +MYSHEBANG="" ################ -# uncomment thenfollowing line to make the conversion -# DOIT="yes" +# uncomment one of the following lines to make the conversion +# linux/unix bash +# MYSHEBANG="#!/bin/bash" + +# BSD bash +# MYSHEBANG="#!/usr/bin/bash" + +# homebrew gnu bash on MacOS +# MYSHEBANG="#!/usr/local/opt/bash" + +# use portable /usr/bin/env +# MYSHEBANG="#!/usr/bin/env bash" + +# bashbot default bash +FROMSHEBANG="#!/bin/bash" + +# uncomment to convert back to bashbot default bash +# FROMSHEBANG="#!/usr/bin/env bash" +# MYSHEBANG="#!/bin/bash" if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then - echo "$0: convert bash shebang to /usr/bin/env bash" + echo "$0: convert bash shebang to point to your local installation" echo "usage: $0 script [script ...]" exit fi # loop tomprocess files -if [ "${DOIT}" = "yes" ]; then - echo "Warning, changes will be done!" +if [ "${MYSHEBANG}" != "" ]; then + echo "Warning, shebang will changed from ${FROMSHEBANG} changed to ${MYSHEBANG}!" else - echo "Dry run, output changes only!" - echo "Uncomment DOIT=\"yes\" in script to make the changes permanent." + echo "Dry run, demonstration only!" + echo "Uncomment one of the MYSHEBANG= lines fitting your environment to make the changes permanent." fi @@ -45,15 +63,15 @@ do file "${file}" if [[ "$(file -b "${file}")" =~ Bourne.*script.*text ]]; then echo "Processing ${file} ..." - if head -n 1 "${file}" | grep -q '^#!/bin/bash'; then - if [ "${DOIT}" = "yes" ]; then - sed -i -e '1 s|^#!/bin/bash|#!/usr/bin/env bash|' "${file}" + if head -n 1 "${file}" | grep -q "^${FROMSHEBANG}"; then + if [ "${MYSHEBANG}" != "" ]; then + sed -i -e '1 s|^'"${FROMSHEBANG}"'|'"${MYSHEBANG}"'|' "${file}" head -n 1 "${file}" else - sed -n -e '1 s|^#!/bin/bash|#!/usr/bin/env bash (dry run)|p' "${file}" + sed -n -e '1 s|^'"${FROMSHEBANG}"'|#!/some/shebang/bash (dry run)|p' "${file}" fi else - echo "No #!/bin/bash shebang, nothing to convert." + echo "Found: $(head -n 1 "${file}") - Nothing to convert." fi echo -e "... done.\n" else