declare -a simpleOperations=(simplify factor derive integrate zeroes roots tangent area cos sin tan arccos arcsin arctan abs log)
## Allows to call the users configured client without if statements everywhere
httpGet()
{
case "$configuredClient" in
curl) curl -A curl -s "$@" ;;
wget) wget -qO- "$@" ;;
fetch) fetch -o "..." ;;
esac
}
checkInternet()
{
httpGet google.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query google with a get request
}
update()
{
# Author: Alexander Epstein https://github.com/alexanderepstein
# Update utility version 1.2.0
# To test the tool enter in the defualt values that are in the examples for each variable
repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite
githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein
nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one
latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option
if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then
echo "Error: update utility has not been configured correctly." >&2
exit 1
elif [[ $latestVersion == "" ]]; then
echo "Error: no active internet connection" >&2
exit 1
else
if [[ "$latestVersion" != "$currentVersion" ]]; then
echo "Version $latestVersion available"
echo -n "Do you wish to update $repositoryName [Y/n]: "
read -r answer
if [[ "$answer" == [Yy] ]] ;then
cd ~ || { echo 'Update Failed'; exit 1; }
if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi
originalEquation=$(echo $1 | sed "s/\[/\(/g" | sed "s/\]/\)/g") # accont for the fact that we have to use brackets and not parenthesis
parsedExpression=$(echo $1 | sed "s/\[/\(/g" | sed "s/\]/\)/g" | grep -Eo "[0-9 + -- / * ^ . a-z A-Z ~ : ( ) ]*") # only grepping valid characters
if [[ "$parsedExpression" != "$originalEquation" ]];then { echo "Error: Expression contains invalid characters"; return 1; }; fi # compare result to original
if ! $validOp; then { echo "Error: invalid operation, run newton -h to get a list of valid operations"; return 1; }; fi # if not a valid operation error out
if [[ $operation == "roots" ]]; then operation=zeroes;fi # I gave the ability to use root or zeores but real op is zeroes
result=$(httpGet https://newton.now.sh/$operation/$equation | grep -Eo '"result":"[a-z A-Z 0-9 ( ) \^ / -- + , ]*' | sed s/'"result":"'//g) # get reponse, grab result
if [[ $result == "" ]];then { echo "Error: no result was returned, did you use valid characters?"; return 1; }; fi # if result is empty sometthing went wrong...