ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty

    IT Discussion
    6
    30
    1.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ObsolesceO
      Obsolesce
      last edited by

      What do you think @IRJ ?

      1 Reply Last reply Reply Quote -1
      • wirestyle22W
        wirestyle22
        last edited by wirestyle22

        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.

        stacksofplatesS 1 Reply Last reply Reply Quote 0
        • stacksofplatesS
          stacksofplates @wirestyle22
          last edited by

          @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

          wirestyle22W IRJI 2 Replies Last reply Reply Quote 1
          • wirestyle22W
            wirestyle22 @stacksofplates
            last edited by wirestyle22

            @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

            stacksofplatesS 1 Reply Last reply Reply Quote 0
            • IRJI
              IRJ @stacksofplates
              last edited by

              @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

              1 Reply Last reply Reply Quote 0
              • stacksofplatesS
                stacksofplates @wirestyle22
                last edited by

                @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.

                1 Reply Last reply Reply Quote 1
                • stacksofplatesS
                  stacksofplates
                  last edited by stacksofplates

                  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}')
                  
                  wirestyle22W 1 Reply Last reply Reply Quote 1
                  • wirestyle22W
                    wirestyle22 @stacksofplates
                    last edited by

                    @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.

                    1 Reply Last reply Reply Quote 0
                    • stacksofplatesS
                      stacksofplates
                      last edited by

                      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.

                      wirestyle22W 1 Reply Last reply Reply Quote 1
                      • wirestyle22W
                        wirestyle22 @stacksofplates
                        last edited by

                        @stacksofplates Thanks for your help

                        stacksofplatesS 1 Reply Last reply Reply Quote 0
                        • stacksofplatesS
                          stacksofplates @wirestyle22
                          last edited by

                          @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.

                          wirestyle22W 1 Reply Last reply Reply Quote 0
                          • wirestyle22W
                            wirestyle22 @stacksofplates
                            last edited by wirestyle22

                            @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?

                            stacksofplatesS 1 Reply Last reply Reply Quote 0
                            • stacksofplatesS
                              stacksofplates @wirestyle22
                              last edited by stacksofplates

                              @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.

                              wirestyle22W 1 Reply Last reply Reply Quote 0
                              • wirestyle22W
                                wirestyle22 @stacksofplates
                                last edited by wirestyle22

                                @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

                                ObsolesceO 1 Reply Last reply Reply Quote 0
                                • wirestyle22W
                                  wirestyle22
                                  last edited by wirestyle22

                                  https://twitter.com/YellsOld/status/1370004797798092804?s=07&fbclid=IwAR2IVL6gcZT7MS7xnjEQODIY6HUuSeKTlbf2OMFp0pYU3euVHFRqu6cFHT8

                                  Relevant

                                  1 Reply Last reply Reply Quote 0
                                  • ObsolesceO
                                    Obsolesce @wirestyle22
                                    last edited by

                                    @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.

                                    wirestyle22W 1 Reply Last reply Reply Quote 0
                                    • wirestyle22W
                                      wirestyle22 @Obsolesce
                                      last edited by wirestyle22

                                      @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.

                                      JaredBuschJ 1 Reply Last reply Reply Quote 0
                                      • JaredBuschJ
                                        JaredBusch @wirestyle22
                                        last edited by

                                        @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.

                                        wirestyle22W 1 Reply Last reply Reply Quote 2
                                        • wirestyle22W
                                          wirestyle22 @JaredBusch
                                          last edited by

                                          @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

                                          IRJI JaredBuschJ 2 Replies Last reply Reply Quote 0
                                          • IRJI
                                            IRJ @wirestyle22
                                            last edited by

                                            @wirestyle22 said in Return Values in Bash Script and generate e-mail which shows successes, errors and if the directory is empty:

                                            @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

                                            I learned the basics of python in a day or two. I bought udemy course and built a few python apps. I was able to find tutorials to build security tools like scrapers and scanners.

                                            I've since added to these scripts and combined some of them. I'm not a python master, but it's very easy to pick up. There's also so many resources out there.

                                            travisdh1T wirestyle22W 2 Replies Last reply Reply Quote 1
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post