Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty
-
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Is this currently working?
You're doing a loop over the items in
"$encryptedFolderPath"*.pgp
and callingfailArray=()
andsuccessArray()
but it seems like you're doing that before you actually run the loop to decrypt them? Maybe I'm looking at this incorrectly.failArray=() and successArray=() are just creating empty arrays I can put data into
-
What do you think @IRJ ?
-
I am breaking this down very slowly for myself. Not a bash master by any measure, but I do want to continue learning it. Arrays seem somewhat annoying in Bash. I will likely learn python to deal with more complex stuff I may need to do with them.
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
I am breaking this down very slowly for myself. Not a bash master by any measure, but I do want to continue learning it. Arrays seems somewhat annoying in Bash. I will likely learn python to deal with more complex stuff I may need to do with them.
I was going to suggest that. This would likely be easier in Python and more straightforward. If you have to stick to bash, don't declare your arrays in a looplike that, just declare them in the variables at the top
-
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
I am breaking this down very slowly for myself. Not a bash master by any measure, but I do want to continue learning it. Arrays seems somewhat annoying in Bash. I will likely learn python to deal with more complex stuff I may need to do with them.
I was going to suggest that. This would likely be easier in Python and more straightforward. If you have to stick to bash, don't declare your arrays in a looplike that, just declare them in the variables at the top
Alright I did that. Is there something better than ShellCheck for bash syntax checking that you know of? Do you just use bash -n script
-
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
I am breaking this down very slowly for myself. Not a bash master by any measure, but I do want to continue learning it. Arrays seems somewhat annoying in Bash. I will likely learn python to deal with more complex stuff I may need to do with them.
This would likely be easier in Python and more straightforward.
Yep
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
I am breaking this down very slowly for myself. Not a bash master by any measure, but I do want to continue learning it. Arrays seems somewhat annoying in Bash. I will likely learn python to deal with more complex stuff I may need to do with them.
I was going to suggest that. This would likely be easier in Python and more straightforward. If you have to stick to bash, don't declare your arrays in a looplike that, just declare them in the variables at the top
Alright I did that. Is there something better than ShellCheck for bash syntax checking that you know of? Do you just use bash -n script
I don't know of anything better.
-
Here's a quick setup in Python if you want to try it instead.
import os import gnupg encrypted_dir = "/tmp/encrypted_files" archive = "/tmp/archive" password = os.getenv(os.getenv("DECRYPT_PASSWORD")) gpg = gnupg.GPG(gnupghome='/home/user/.gnupg') responses = {} def decrypt_file(file: str, password: str): out_name = f'{encrypted_dir}/{file}.decrypted' stream = open(f'{encrypted_dir}/{file}', "rb") return gpg.decrypt_file(stream, passphrase=password, output=out_name) for file in os.listdir(encrypted_dir): if file.endswith(".gpg"): stat = decrypt_file(file, password) responses[file] = stat else: continue for file in responses: status = responses[file] if status.ok: os.rename(f'{encrypted_dir}/{file}', f'{archive}/{file}') print(f'File {file} decrypted and moved') else: print(f'File {file} had error, {status.stderr}')
-
@stacksofplates Thanks. That will help as a reference for later but I did want to figure out how to do this in bash as well. It's just kind of a challenge I don't want to give up on yet.
-
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
-
@stacksofplates Thanks for your help
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates Thanks for your help
No prob. I'm not a bash expert (and I find it annoying lol) so once things get past a certain point I give up with it.
-
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
-
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
Gotcha. Yeah it sounds more convenient it just going to take me more time to learn than I have with this current script
-
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
Gotcha. Yeah it sounds more convenient it just going to take me more time to learn than I have with this current script
Isn't this the one you've been working on for like a year now? I'd say that's enough time to learn a little about scripting.
-
@Obsolesce said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
Gotcha. Yeah it sounds more convenient it just going to take me more time to learn than I have with this current script
Isn't this the one you've been working on for like a year now? I'd say that's enough time to learn a little about scripting.
I built the original one awhile ago. Now I want to build more functionality into it. It's been static for a very long time.
-
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@Obsolesce said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
Gotcha. Yeah it sounds more convenient it just going to take me more time to learn than I have with this current script
Isn't this the one you've been working on for like a year now? I'd say that's enough time to learn a little about scripting.
I built the original one awhile ago. Now I want to build more functionality into it. It's been static for a very long time.
Except, bash is not the place to build more functionality.
You use bash for basic stuff, or when there is no better option available.
-
@JaredBusch said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@Obsolesce said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
@stacksofplates said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:
Rather than mess with multiple arrays, you can just have a single dictionary that holds the file and status. A single function can decrypt the file. Then just save the file name and status of the decryption in that dictionary. Then loop through the dictionary and here I just print the data, but you could email it or send to Slack or whatever.
This was a quick pass so probably can be cleaned up a bit.
My reasoning behind two arrays was to keep it organized. If I do all successes in one and then all failures in the other. So I have this now:
#!/usr/bin/env bash source "/home/user1/subdirectory1/master.sh" decryptedFolderPath="/home/user2/subdirectory2/" archiveFolderPath="/home/user1/subdirectory1/archive/in/" extension=${fileName##*\.} newFileName=${fileName%.*} fileWithoutTimestamp="$newFileName.$extension" encryptedItems=$(ls encryptedFolderPath*.pgp) statusArray=() for i in $encryptedItems do gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes --decrypt "$i" | grep -q "encrypted" > "$decryptedFolderPath"/"$fileWithoutTimestamp" outPut=$(gpg --batch --homedir /home/user1/.gnupg/ --passphrase "$PASS" --list-only --list-packets --yes "$i" | grep -q "encrypted") if [ $? != 0 ]; then echo "$i is not a pgp file" statusArray+=("failed to decrypt $i, with status code $? output from pgp: $outPut") fi if [ $? == 0 ]; then statusArray+=("Succesfully Decrypted $i") echo ${#statusArray[@]} | mail -s 'report' [email protected] v=${i%.*} encryptedFile="$v" fileName=${encryptedFile##*/} @@ -27,4 +34,4 @@ continue fi done mv "$i" "$archiveFolderPath"
I think this is what you meant, right?
Well no. I meant Python can easily work with dictionaries (hash maps) vs doing multiple arrays. You'd have to switch to a hash map in Bash vs the multiple arrays.
Gotcha. Yeah it sounds more convenient it just going to take me more time to learn than I have with this current script
Isn't this the one you've been working on for like a year now? I'd say that's enough time to learn a little about scripting.
I built the original one awhile ago. Now I want to build more functionality into it. It's been static for a very long time.
Except, bash is not the place to build more functionality.
You use bash for basic stuff, or when there is no better option available.
technically there is no other viable option because I don't have the time to learn the alternative before I need this to be done by. the plan is to remake it in python later. after I convert some stuff and feel comfortable, I'll only use python