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.
    • 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
            • travisdh1T
              travisdh1 @IRJ
              last edited by

              @IRJ 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:

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

              I've used python for interfacing with sensors connected to a rPi. It's easier to picup than BASH imo.

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

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

                the plan is to remake it in python later. after I convert some stuff and feel comfortable, I'll only use python

                I personally hate python because of the space indentation defining code blocks. even if I always space indent my code.

                But the language is simple and straightforward.

                stacksofplatesS 1 Reply Last reply Reply Quote 0
                • stacksofplatesS
                  stacksofplates @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:

                  the plan is to remake it in python later. after I convert some stuff and feel comfortable, I'll only use python

                  I personally hate python because of the space indentation defining code blocks. even if I always space indent my code.

                  But the language is simple and straightforward.

                  I always hated it for that, but PyCharm and other IDEs make it pretty easy. PyCharm is probably the best. I'm a fan of the JetBrains tools.

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

                    On the Python note, I wrote a microservice for a PoC we are doing. I wrote it in Go and it takes a JWT and some information about a secret stored in Vault and authenticates to Vault with the JWT and retrieves the secret. It was around 500-600 lines of code. I rewrote it in Python today and it was less than 40 lines of code.

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

                      @IRJ 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:

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

                      I purchased "Learn Python the Hard way". Going through it now and then experimenting.

                      JaredBuschJ 1 Reply Last reply Reply Quote 1
                      • 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:

                        I purchased "Learn Python the Hard way". Going through it now and then experimenting.

                        While a good thing, if you have a working bash script just Google each line “in python”

                        Declare array in python
                        For next in python

                        1 Reply Last reply Reply Quote 2
                        • 1
                        • 2
                        • 2 / 2
                        • First post
                          Last post