Bash: Comparing names using xargs and if statement












0















I have a folder were if .wav files are uploaded. A progress is triggered to convert the file. This has to be done recursively.



I use inotifywait to start the process if a file is added or changed, Then I want to compare the file extension using an if statement.



However, rather than comparing the value of {} it tries to call a function with the name of the string replaced by {}. This is probably an issue of my if condition.



My pipeline is here:



inotifywait -mrq -e create -e moved_to --format '%f' ./ | xargs -I {} bash -c "if '{}' =~ *.wav ; then echo {}  ; fi"


How should I compare the extension of the string stored in {}?










share|improve this question



























    0















    I have a folder were if .wav files are uploaded. A progress is triggered to convert the file. This has to be done recursively.



    I use inotifywait to start the process if a file is added or changed, Then I want to compare the file extension using an if statement.



    However, rather than comparing the value of {} it tries to call a function with the name of the string replaced by {}. This is probably an issue of my if condition.



    My pipeline is here:



    inotifywait -mrq -e create -e moved_to --format '%f' ./ | xargs -I {} bash -c "if '{}' =~ *.wav ; then echo {}  ; fi"


    How should I compare the extension of the string stored in {}?










    share|improve this question

























      0












      0








      0








      I have a folder were if .wav files are uploaded. A progress is triggered to convert the file. This has to be done recursively.



      I use inotifywait to start the process if a file is added or changed, Then I want to compare the file extension using an if statement.



      However, rather than comparing the value of {} it tries to call a function with the name of the string replaced by {}. This is probably an issue of my if condition.



      My pipeline is here:



      inotifywait -mrq -e create -e moved_to --format '%f' ./ | xargs -I {} bash -c "if '{}' =~ *.wav ; then echo {}  ; fi"


      How should I compare the extension of the string stored in {}?










      share|improve this question














      I have a folder were if .wav files are uploaded. A progress is triggered to convert the file. This has to be done recursively.



      I use inotifywait to start the process if a file is added or changed, Then I want to compare the file extension using an if statement.



      However, rather than comparing the value of {} it tries to call a function with the name of the string replaced by {}. This is probably an issue of my if condition.



      My pipeline is here:



      inotifywait -mrq -e create -e moved_to --format '%f' ./ | xargs -I {} bash -c "if '{}' =~ *.wav ; then echo {}  ; fi"


      How should I compare the extension of the string stored in {}?







      bash xargs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 20:30









      Thomas van der meulenThomas van der meulen

      32




      32
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The two main problems are




          • You forgot the braces. if itself can only check the exit status of the following command and somefile =~ *.wav is generally not a command, but [[ ... =~ ... ]] is.


          • =~ uses extended regexes which are a bit different from globs. To match all files ending in literally .wav you have to write .*.wav$.


          inotifywait -mrq -e create -e moved_to --format '%f' ./ |
          xargs -I {} bash -c "if [[ '{}' =~ .*.wav$ ]]; then echo {}; fi"


          However this command might only work for simple file names without special symbols. For instance, it breaks if there is a single ' inside the file name. It would be safer to write



          ... | xargs -I {} bash -c 'if [[ "$1" =~ .*.wav$ ]]; then printf %s\n "$1"; fi' -- {}


          You could also use the simple and faster alternative



          ... | grep -E '.wav$'





          share|improve this answer

































            0














            If you intention is to filter by certain extension, take a look at https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific



            Of if you have more complicated logic to handle different file extensions, you can write your code as



            inotifywait -mrq -e create -e moved_to --format '%f' ./ | while read line; do
            fullname=$(basename -- $line)
            extension=${fullname##*.}
            filename=${fullname%.*}
            echo "fullname=$fullname"
            echo "filename=$filename"
            echo "extension=$extension"
            # now you can do more things with either $filename or $extension
            if [ "$extension" == "wav" ]; then
            echo "to process $fullname"
            fi
            done





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437665%2fbash-comparing-names-using-xargs-and-if-statement%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              The two main problems are




              • You forgot the braces. if itself can only check the exit status of the following command and somefile =~ *.wav is generally not a command, but [[ ... =~ ... ]] is.


              • =~ uses extended regexes which are a bit different from globs. To match all files ending in literally .wav you have to write .*.wav$.


              inotifywait -mrq -e create -e moved_to --format '%f' ./ |
              xargs -I {} bash -c "if [[ '{}' =~ .*.wav$ ]]; then echo {}; fi"


              However this command might only work for simple file names without special symbols. For instance, it breaks if there is a single ' inside the file name. It would be safer to write



              ... | xargs -I {} bash -c 'if [[ "$1" =~ .*.wav$ ]]; then printf %s\n "$1"; fi' -- {}


              You could also use the simple and faster alternative



              ... | grep -E '.wav$'





              share|improve this answer






























                0














                The two main problems are




                • You forgot the braces. if itself can only check the exit status of the following command and somefile =~ *.wav is generally not a command, but [[ ... =~ ... ]] is.


                • =~ uses extended regexes which are a bit different from globs. To match all files ending in literally .wav you have to write .*.wav$.


                inotifywait -mrq -e create -e moved_to --format '%f' ./ |
                xargs -I {} bash -c "if [[ '{}' =~ .*.wav$ ]]; then echo {}; fi"


                However this command might only work for simple file names without special symbols. For instance, it breaks if there is a single ' inside the file name. It would be safer to write



                ... | xargs -I {} bash -c 'if [[ "$1" =~ .*.wav$ ]]; then printf %s\n "$1"; fi' -- {}


                You could also use the simple and faster alternative



                ... | grep -E '.wav$'





                share|improve this answer




























                  0












                  0








                  0







                  The two main problems are




                  • You forgot the braces. if itself can only check the exit status of the following command and somefile =~ *.wav is generally not a command, but [[ ... =~ ... ]] is.


                  • =~ uses extended regexes which are a bit different from globs. To match all files ending in literally .wav you have to write .*.wav$.


                  inotifywait -mrq -e create -e moved_to --format '%f' ./ |
                  xargs -I {} bash -c "if [[ '{}' =~ .*.wav$ ]]; then echo {}; fi"


                  However this command might only work for simple file names without special symbols. For instance, it breaks if there is a single ' inside the file name. It would be safer to write



                  ... | xargs -I {} bash -c 'if [[ "$1" =~ .*.wav$ ]]; then printf %s\n "$1"; fi' -- {}


                  You could also use the simple and faster alternative



                  ... | grep -E '.wav$'





                  share|improve this answer















                  The two main problems are




                  • You forgot the braces. if itself can only check the exit status of the following command and somefile =~ *.wav is generally not a command, but [[ ... =~ ... ]] is.


                  • =~ uses extended regexes which are a bit different from globs. To match all files ending in literally .wav you have to write .*.wav$.


                  inotifywait -mrq -e create -e moved_to --format '%f' ./ |
                  xargs -I {} bash -c "if [[ '{}' =~ .*.wav$ ]]; then echo {}; fi"


                  However this command might only work for simple file names without special symbols. For instance, it breaks if there is a single ' inside the file name. It would be safer to write



                  ... | xargs -I {} bash -c 'if [[ "$1" =~ .*.wav$ ]]; then printf %s\n "$1"; fi' -- {}


                  You could also use the simple and faster alternative



                  ... | grep -E '.wav$'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 22:18

























                  answered Nov 22 '18 at 22:06









                  SocowiSocowi

                  7,1862927




                  7,1862927

























                      0














                      If you intention is to filter by certain extension, take a look at https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific



                      Of if you have more complicated logic to handle different file extensions, you can write your code as



                      inotifywait -mrq -e create -e moved_to --format '%f' ./ | while read line; do
                      fullname=$(basename -- $line)
                      extension=${fullname##*.}
                      filename=${fullname%.*}
                      echo "fullname=$fullname"
                      echo "filename=$filename"
                      echo "extension=$extension"
                      # now you can do more things with either $filename or $extension
                      if [ "$extension" == "wav" ]; then
                      echo "to process $fullname"
                      fi
                      done





                      share|improve this answer




























                        0














                        If you intention is to filter by certain extension, take a look at https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific



                        Of if you have more complicated logic to handle different file extensions, you can write your code as



                        inotifywait -mrq -e create -e moved_to --format '%f' ./ | while read line; do
                        fullname=$(basename -- $line)
                        extension=${fullname##*.}
                        filename=${fullname%.*}
                        echo "fullname=$fullname"
                        echo "filename=$filename"
                        echo "extension=$extension"
                        # now you can do more things with either $filename or $extension
                        if [ "$extension" == "wav" ]; then
                        echo "to process $fullname"
                        fi
                        done





                        share|improve this answer


























                          0












                          0








                          0







                          If you intention is to filter by certain extension, take a look at https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific



                          Of if you have more complicated logic to handle different file extensions, you can write your code as



                          inotifywait -mrq -e create -e moved_to --format '%f' ./ | while read line; do
                          fullname=$(basename -- $line)
                          extension=${fullname##*.}
                          filename=${fullname%.*}
                          echo "fullname=$fullname"
                          echo "filename=$filename"
                          echo "extension=$extension"
                          # now you can do more things with either $filename or $extension
                          if [ "$extension" == "wav" ]; then
                          echo "to process $fullname"
                          fi
                          done





                          share|improve this answer













                          If you intention is to filter by certain extension, take a look at https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific



                          Of if you have more complicated logic to handle different file extensions, you can write your code as



                          inotifywait -mrq -e create -e moved_to --format '%f' ./ | while read line; do
                          fullname=$(basename -- $line)
                          extension=${fullname##*.}
                          filename=${fullname%.*}
                          echo "fullname=$fullname"
                          echo "filename=$filename"
                          echo "extension=$extension"
                          # now you can do more things with either $filename or $extension
                          if [ "$extension" == "wav" ]; then
                          echo "to process $fullname"
                          fi
                          done






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 22:25









                          Rico ChenRico Chen

                          741511




                          741511






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437665%2fbash-comparing-names-using-xargs-and-if-statement%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings