#!/bin/bash # Author: Alexander Epstein https://github.com/alexanderepstein global currentVersion := '"1.11.1'" global state := ''"" proc checkOpenSSL { if ! command -v openssl &>/dev/null ;{ echo "Error: to use this tool openssl must be installed" > !2 return 1 } else { return 0 } } ## uses openssl aes 256 cbc encryption to encrypt file salting it with password designated by user proc encrypt { echo "Encrypting $1..." openssl enc -aes-256-cbc -salt -a -in $1 -out $2 || do { echo "File not found"; return 1; } echo "Successfully encrypted" } ## uses openssl aes 256 cbc decryption to decrypt file proc decrypt { echo "Decrypting $1..." openssl enc -aes-256-cbc -d -a -in $1 -out $2 || do { echo "File not found"; return 1; } echo "Successfully decrypted" } proc 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 global repositoryName := '"Bash-Snippets'" #Name of repostiory to be updated ex. Sandman-Lite global githubUserName := '"alexanderepstein'" #username that hosts the repostiory ex. alexanderepstein global nameOfInstallFile := '"install.sh'" # change this if the installer file has a different name be sure to include file extension if there is one global latestVersion := $[curl -s 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 == "" ]]{ echo "Error: update utility has not been configured correctly." > !2 exit 1 } elif [[ $latestVersion == "" ]]{ echo "Error: no active internet connection" > !2 exit 1 } else { if [[ "$latestVersion" != "$currentVersion" ]] { echo "Version $latestVersion available" echo -n "Do you wish to update $repositoryName [Y/n]: " read -r answer if [[ "$answer" == "Y" || "$answer" == "y" ]] { cd ~ || do { echo 'Update Failed' ; exit 1 ; } if [[ -d ~/$repositoryName ]] { rm -r -f $repositoryName || do { echo "Permissions Error: try running the update as sudo"; exit 1; } ; } git clone "https://github.com/$githubUserName/$repositoryName" || do { echo "Couldn't download latest version" ; exit 1; } cd $repositoryName || do { echo 'Update Failed' ; exit 1 ;} git checkout "v$latestVersion" !2 > /dev/null || git checkout $latestVersion !2 > /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit." chmod a+x install.sh #this might be necessary in your case but wasnt in mine. ./$nameOfInstallFile "update" || exit 1 cd .. rm -r -f $repositoryName || do { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; } } else { exit 1 } } else { echo "$repositoryName is already the latest version" } } } proc usage { echo "Crypt" echo "Description: A wrapper around openssl that facilitates encrypting and decrypting files." echo "Usage: crypt [flag] [inputFile] [outputFile]" echo " -e Encrypt the inputFile and store it in the outputFile" echo " -d Decrypt the inputFile and store it in the outputFile" echo " -u Update Bash-Snippet Tools" echo " -h Show the help" echo " -v Get the tool version" echo "Examples:" echo " crypt -e mySecretFile.txt myEncryptedFile.jpg (change filetype so default program is incorrect)" echo " crypt -d myEncryptedFile.jpg thisIsNowDecrypted.txt (change filetype back so now default program is correct)" } checkOpenSSL || exit 1 while getopts "huve:d:" opt { ## alows for using options in bash matchstr $opt { e { ## when trying to encrypt run this if [[ $state != "decrypt" ]]{ global state := '"encrypt'" } else { echo "Error: the -d and -e options are mutally exclusive" > !2 exit 1 } if [[ $# -ne 3 ]] { echo "Option -e needs and only accepts two arguments [file to encrypt] [output file]" > !2 exit 1 } } \? { echo "Invalid option: -$OPTARG" > !2 exit 1 } d { ## when trying to decrypt run this if [[ $state != "encrypt" ]]{ global state := '"decrypt'" } else { echo "Error: the -e and -d options are mutally exclusive" > !2 exit 1 } if [[ $# -ne 3 ]] { echo "Option -d needs and only accepts two arguments [file to decrypt] [output file]" > !2 exit 1 } } u { update exit 0 } h { usage exit 0 } v { echo "Version $currentVersion" exit 0 } : { ## will run when no arguments are provided to to e or d options echo "Option -$OPTARG requires an argument." > !2 exit 1 } } } if [[ $# == 0 ]] { usage exit 0 } elif [[ $1 == "update" ]]{ update exit 0 } elif [[ $1 == "help" ]]{ usage exit 0 } elif [[ $state == "encrypt" ]]{ encrypt $2 $3 || exit 1 exit 0 } elif [[ $state == "decrypt" ]]{ decrypt $2 $3 || exit 1 exit 0 } (CommandList children: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:currentVersion) op:Equal rhs:{(DQ (1.11.1))} spids:[7])] spids: [7] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:state) op:Equal rhs:{(DQ )} spids:[12])] spids: [12] ) (FuncDef name: checkOpenSSL body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (Pipeline children:[(C {(command)} {(-v)} {(openssl)})] negated:True) terminator: ) (Sentence child: (SimpleCommand redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [35] ) ] ) terminator: ) ] action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: to use this tool openssl must be installed"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[48])] ) (ControlFlow token: arg_word:{(1)}) ] spids: [-1 39] ) ] else_action: [(ControlFlow token: arg_word:{(0)})] spids: [57 65] ) ] spids: [21] ) spids: [17 20] ) (FuncDef name: encrypt body: (BraceGroup children: [ (C {(echo)} {(DQ ("Encrypting ") ($ VSub_Number "$1") (...))}) (AndOr children: [ (C {(openssl)} {(enc)} {(-aes-256-cbc)} {(-salt)} {(-a)} {(-in)} {($ VSub_Number "$1")} {(-out)} {($ VSub_Number "$2")} ) (BraceGroup children: [ (Sentence child: (C {(echo)} {(DQ ("File not found"))}) terminator: ) (Sentence child: (ControlFlow token: arg_word:{(1)}) terminator: ) ] spids: [108] ) ] op_id: Op_DPipe ) (C {(echo)} {(DQ ("Successfully encrypted"))}) ] spids: [76] ) spids: [72 75] ) (FuncDef name: decrypt body: (BraceGroup children: [ (C {(echo)} {(DQ ("Decrypting ") ($ VSub_Number "$1") (...))}) (AndOr children: [ (C {(openssl)} {(enc)} {(-aes-256-cbc)} {(-d)} {(-a)} {(-in)} {($ VSub_Number "$1")} {(-out)} {($ VSub_Number "$2")} ) (BraceGroup children: [ (Sentence child: (C {(echo)} {(DQ ("File not found"))}) terminator: ) (Sentence child: (ControlFlow token: arg_word:{(1)}) terminator: ) ] spids: [173] ) ] op_id: Op_DPipe ) (C {(echo)} {(DQ ("Successfully decrypted"))}) ] spids: [141] ) spids: [137 140] ) (FuncDef name: update body: (BraceGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:repositoryName) op: Equal rhs: {(DQ (Bash-Snippets))} spids: [218] ) ] spids: [218] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:githubUserName) op: Equal rhs: {(DQ (alexanderepstein))} spids: [227] ) ] spids: [227] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:nameOfInstallFile) op: Equal rhs: {(DQ (install.sh))} spids: [236] ) ] spids: [236] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:latestVersion) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(curl)} {(-s)} {(https) (Lit_Other ":") (//api.github.com/repos/) ($ VSub_Name "$githubUserName") (/) ($ VSub_Name "$repositoryName") (/tags) } ) (C {(grep)} {(-Eo)} {(SQ <"\"name\":.*?[^\\\\]\",">)}) (C {(head)} {(-1)}) (C {(grep)} {(-Eo)} {(DQ ("[0-9.]+"))}) ] negated: False ) ] ) left_token: spids: [246 284] ) } spids: [245] ) ] spids: [245] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$currentVersion")} right: {(DQ )} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$repositoryName")} right: {(DQ )} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$githubUserName")} right: {(DQ )} ) right: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$nameOfInstallFile")} right: {(DQ )} ) ) ) ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} {(DQ ("Error: update utility has not been configured correctly."))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[340])] ) (C {(exit)} {(1)}) ] spids: [-1 331] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$latestVersion")} right: {(DQ )} ) ) terminator: ) ] action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: no active internet connection"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[371])] ) (C {(exit)} {(1)}) ] spids: [349 362] ) ] else_action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {(DQ ($ VSub_Name "$latestVersion"))} right: {(DQ ($ VSub_Name "$currentVersion"))} ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Version ") ($ VSub_Name "$latestVersion") (" available"))}) (C {(echo)} {(-n)} { (DQ ("Do you wish to update ") ($ VSub_Name "$repositoryName") (" [Y/n]: ")) } ) (C {(read)} {(-r)} {(answer)}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$answer"))} right: {(DQ (Y))} ) right: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$answer"))} right: {(DQ (y))} ) ) ) terminator: ) ] action: [ (AndOr children: [ (C {(cd)} {(TildeSubPart prefix:"")}) (BraceGroup children: [ (Sentence child: (C {(echo)} {(SQ <"Update Failed">)}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [468] ) ] op_id: Op_DPipe ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id: BoolUnary_d child: {(Lit_Tilde "~") (/) ($ VSub_Name "$repositoryName") } ) ) terminator: ) ] action: [ (Sentence child: (AndOr children: [ (C {(rm)} {(-r)} {(-f)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} { (DQ ( "Permissions Error: try running the update as sudo" ) ) } ) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [512] ) ] op_id: Op_DPipe ) terminator: ) ] spids: [-1 500] ) ] spids: [-1 530] ) (AndOr children: [ (C {(git)} {(clone)} { (DQ ("https://github.com/") ($ VSub_Name "$githubUserName") (/) ($ VSub_Name "$repositoryName") ) } ) (BraceGroup children: [ (Sentence child: (C {(echo)} {(DQ ("Couldn't download latest version"))}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [546] ) ] op_id: Op_DPipe ) (AndOr children: [ (C {(cd)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} {(SQ <"Update Failed">)}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [570] ) ] op_id: Op_DPipe ) (AndOr children: [ (SimpleCommand words: [ {(git)} {(checkout)} {(DQ (v) ($ VSub_Name "$latestVersion"))} ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [597] ) ] ) (AndOr children: [ (SimpleCommand words: [ {(git)} {(checkout)} {(DQ ($ VSub_Name "$latestVersion"))} ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [611] ) ] ) (C {(echo)} { (DQ ( "Couldn't git checkout to stable release, updating to latest commit." ) ) } ) ] op_id: Op_DPipe ) ] op_id: Op_DPipe ) (C {(chmod)} {(a) (Lit_Other "+") (x)} {(install.sh)}) (AndOr children: [ (C {(./) ($ VSub_Name "$nameOfInstallFile")} {(DQ (update))}) (C {(exit)} {(1)}) ] op_id: Op_DPipe ) (C {(cd)} {(..)}) (AndOr children: [ (C {(rm)} {(-r)} {(-f)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} { (DQ ( "Permissions Error: update succesfull but cannot delete temp files located at ~/" ) ($ VSub_Name "$repositoryName") (" delete this directory with sudo") ) } ) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [665] ) ] op_id: Op_DPipe ) ] spids: [-1 459] ) ] else_action: [(C {(exit)} {(1)})] spids: [684 692] ) ] spids: [-1 400] ) ] else_action: [ (C {(echo)} {(DQ ($ VSub_Name "$repositoryName") (" is already the latest version"))} ) ] spids: [695 706] ) ] spids: [380 709] ) ] spids: [203] ) spids: [199 202] ) (FuncDef name: usage body: (BraceGroup children: [ (C {(echo)} {(DQ (Crypt))}) (C {(echo)} { (DQ ( "Description: A wrapper around openssl that facilitates encrypting and decrypting files." ) ) } ) (C {(echo)} {(DQ ("Usage: crypt [flag] [inputFile] [outputFile]"))}) (C {(echo)} {(DQ (" -e Encrypt the inputFile and store it in the outputFile"))}) (C {(echo)} {(DQ (" -d Decrypt the inputFile and store it in the outputFile"))}) (C {(echo)} {(DQ (" -u Update Bash-Snippet Tools"))}) (C {(echo)} {(DQ (" -h Show the help"))}) (C {(echo)} {(DQ (" -v Get the tool version"))}) (C {(echo)} {(DQ ("Examples:"))}) (C {(echo)} { (DQ ( " crypt -e mySecretFile.txt myEncryptedFile.jpg (change filetype so default program is incorrect)" ) ) } ) (C {(echo)} { (DQ ( " crypt -d myEncryptedFile.jpg thisIsNowDecrypted.txt (change filetype back so now default program is correct)" ) ) } ) ] spids: [720] ) spids: [716 719] ) (AndOr children:[(C {(checkOpenSSL)})(C {(exit)} {(1)})] op_id:Op_DPipe) (While cond: [(Sentence child:(C {(getopts)} {(DQ ("huve:d:"))} {(opt)}) terminator:)] body: (DoGroup children: [ (Case to_match: {($ VSub_Name "$opt")} arms: [ (case_arm pat_list: [{(e)}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {($ VSub_Name "$state")} right: {(DQ (decrypt))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:state) op: Equal rhs: {(DQ (encrypt))} spids: [859] ) ] spids: [859] ) ] spids: [-1 856] ) ] else_action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: the -d and -e options are mutally exclusive"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[874])] ) (C {(exit)} {(1)}) ] spids: [865 883] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_ne left: {($ VSub_Pound "$#")} right: {(3)} ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} { (DQ ( "Option -e needs and only accepts two arguments [file to encrypt] [output file]" ) ) } ] redirects: [ (Redir op_id: Redir_GreatAnd fd: -1 arg_word: {(2)} spids: [908] ) ] ) (C {(exit)} {(1)}) ] spids: [-1 899] ) ] spids: [-1 917] ) ] spids: [835 836 921 -1] ) (case_arm pat_list: [{(EscapedLiteralPart token:)}] action: [ (SimpleCommand words: [{(echo)} {(DQ ("Invalid option: -") ($ VSub_Name "$OPTARG"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[935])] ) (C {(exit)} {(1)}) ] spids: [924 925 944 -1] ) (case_arm pat_list: [{(d)}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {($ VSub_Name "$state")} right: {(DQ (encrypt))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:state) op: Equal rhs: {(DQ (decrypt))} spids: [971] ) ] spids: [971] ) ] spids: [-1 968] ) ] else_action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: the -e and -d options are mutally exclusive"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[986])] ) (C {(exit)} {(1)}) ] spids: [977 995] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_ne left: {($ VSub_Pound "$#")} right: {(3)} ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} { (DQ ( "Option -d needs and only accepts two arguments [file to decrypt] [output file]" ) ) } ] redirects: [ (Redir op_id: Redir_GreatAnd fd: -1 arg_word: {(2)} spids: [1020] ) ] ) (C {(exit)} {(1)}) ] spids: [-1 1011] ) ] spids: [-1 1029] ) ] spids: [947 948 1032 -1] ) (case_arm pat_list: [{(u)}] action: [(C {(update)}) (C {(exit)} {(0)})] spids: [1035 1036 1047 -1] ) (case_arm pat_list: [{(h)}] action: [(C {(usage)}) (C {(exit)} {(0)})] spids: [1050 1051 1062 -1] ) (case_arm pat_list: [{(v)}] action: [ (C {(echo)} {(DQ ("Version ") ($ VSub_Name "$currentVersion"))}) (C {(exit)} {(0)}) ] spids: [1065 1066 1082 -1] ) (case_arm pat_list: [{(Lit_Other ":")}] action: [ (SimpleCommand words: [ {(echo)} {(DQ ("Option -") ($ VSub_Name "$OPTARG") (" requires an argument."))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[1100])] ) (C {(exit)} {(1)}) ] spids: [1085 1086 1109 -1] ) ] spids: [828 832 1112] ) ] spids: [822 1114] ) ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id:BoolBinary_GlobDEqual left:{($ VSub_Pound "$#")} right:{(0)}) ) terminator: ) ] action: [(C {(usage)}) (C {(exit)} {(0)})] spids: [-1 1130] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$1")} right: {(DQ (update))} ) ) terminator: ) ] action: [(C {(update)}) (C {(exit)} {(0)})] spids: [1140 1154] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$1")} right: {(DQ (help))} ) ) terminator: ) ] action: [(C {(usage)}) (C {(exit)} {(0)})] spids: [1164 1178] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$state")} right: {(DQ (encrypt))} ) ) terminator: ) ] action: [ (AndOr children: [ (C {(encrypt)} {($ VSub_Number "$2")} {($ VSub_Number "$3")}) (C {(exit)} {(1)}) ] op_id: Op_DPipe ) (C {(exit)} {(0)}) ] spids: [1188 1202] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$state")} right: {(DQ (decrypt))} ) ) terminator: ) ] action: [ (AndOr children: [ (C {(decrypt)} {($ VSub_Number "$2")} {($ VSub_Number "$3")}) (C {(exit)} {(1)}) ] op_id: Op_DPipe ) (C {(exit)} {(0)}) ] spids: [1222 1236] ) ] spids: [-1 1256] ) ] )