How to handle a .js extension correctly in bash script?











up vote
1
down vote

favorite












I have the following bash script.



while IFS= read -r filename;
do [[ $(md5 path/to/"$filename-orig") = $(md5 path/to/"$filename") ]] || echo $filename differs;
done < path/to/list-of-files-to-compare.txt


It's supposed to compare two files (by computing their MD5 hash digest) then report if they are different. It gets the files to compare from a list.



The problem is that if the file I am trying to read is at, say,



path/to/foo-orig.js


the script will look for the file at



path/to/foo.js-orig


and, obviously, this throws an error and fails.



How do I correct this bug in my script so that I handle the .js extension correctly?



Edit



TL;DR:



Given a string foo.bar how can I get foo-orig.bar?



Edit2:



I add the -orig extension because this project takes a template that's regularly updated by a third party, then I overlay my changes to it. So, I want to keep track of what files they change so I can go back and adapt my code when necessary.



list-of-files-to-compare.txt looks like the following.



list-of-files-to-compare.txt

public/assets/images/logos/logo.svg
README.md
src/auth/Auth.js
src/auth/store/actions/login.actions.js
src/auth/store/actions/user.actions.js
src/auth/store/reducers/user.reducer.js
src/firebaseService/firebaseService.js
src/firebaseService/firebaseServiceConfig.js
src/fuse-configs/fuseNavigationConfig.js
src/fuse-configs/fuseRoutesConfig.js
src/index.js
src/main/content/components/ComponentsConfig.js
src/main/content/login/Login.js
src/store.js
src/store/actions/index.js
src/store/reducers/index.js









share|improve this question
























  • Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
    – Arkadiusz Drabczyk
    Nov 30 at 7:53










  • @ArkadiuszDrabczyk: Do the edits help answer your questions?
    – Mowzer
    Nov 30 at 8:01















up vote
1
down vote

favorite












I have the following bash script.



while IFS= read -r filename;
do [[ $(md5 path/to/"$filename-orig") = $(md5 path/to/"$filename") ]] || echo $filename differs;
done < path/to/list-of-files-to-compare.txt


It's supposed to compare two files (by computing their MD5 hash digest) then report if they are different. It gets the files to compare from a list.



The problem is that if the file I am trying to read is at, say,



path/to/foo-orig.js


the script will look for the file at



path/to/foo.js-orig


and, obviously, this throws an error and fails.



How do I correct this bug in my script so that I handle the .js extension correctly?



Edit



TL;DR:



Given a string foo.bar how can I get foo-orig.bar?



Edit2:



I add the -orig extension because this project takes a template that's regularly updated by a third party, then I overlay my changes to it. So, I want to keep track of what files they change so I can go back and adapt my code when necessary.



list-of-files-to-compare.txt looks like the following.



list-of-files-to-compare.txt

public/assets/images/logos/logo.svg
README.md
src/auth/Auth.js
src/auth/store/actions/login.actions.js
src/auth/store/actions/user.actions.js
src/auth/store/reducers/user.reducer.js
src/firebaseService/firebaseService.js
src/firebaseService/firebaseServiceConfig.js
src/fuse-configs/fuseNavigationConfig.js
src/fuse-configs/fuseRoutesConfig.js
src/index.js
src/main/content/components/ComponentsConfig.js
src/main/content/login/Login.js
src/store.js
src/store/actions/index.js
src/store/reducers/index.js









share|improve this question
























  • Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
    – Arkadiusz Drabczyk
    Nov 30 at 7:53










  • @ArkadiuszDrabczyk: Do the edits help answer your questions?
    – Mowzer
    Nov 30 at 8:01













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following bash script.



while IFS= read -r filename;
do [[ $(md5 path/to/"$filename-orig") = $(md5 path/to/"$filename") ]] || echo $filename differs;
done < path/to/list-of-files-to-compare.txt


It's supposed to compare two files (by computing their MD5 hash digest) then report if they are different. It gets the files to compare from a list.



The problem is that if the file I am trying to read is at, say,



path/to/foo-orig.js


the script will look for the file at



path/to/foo.js-orig


and, obviously, this throws an error and fails.



How do I correct this bug in my script so that I handle the .js extension correctly?



Edit



TL;DR:



Given a string foo.bar how can I get foo-orig.bar?



Edit2:



I add the -orig extension because this project takes a template that's regularly updated by a third party, then I overlay my changes to it. So, I want to keep track of what files they change so I can go back and adapt my code when necessary.



list-of-files-to-compare.txt looks like the following.



list-of-files-to-compare.txt

public/assets/images/logos/logo.svg
README.md
src/auth/Auth.js
src/auth/store/actions/login.actions.js
src/auth/store/actions/user.actions.js
src/auth/store/reducers/user.reducer.js
src/firebaseService/firebaseService.js
src/firebaseService/firebaseServiceConfig.js
src/fuse-configs/fuseNavigationConfig.js
src/fuse-configs/fuseRoutesConfig.js
src/index.js
src/main/content/components/ComponentsConfig.js
src/main/content/login/Login.js
src/store.js
src/store/actions/index.js
src/store/reducers/index.js









share|improve this question















I have the following bash script.



while IFS= read -r filename;
do [[ $(md5 path/to/"$filename-orig") = $(md5 path/to/"$filename") ]] || echo $filename differs;
done < path/to/list-of-files-to-compare.txt


It's supposed to compare two files (by computing their MD5 hash digest) then report if they are different. It gets the files to compare from a list.



The problem is that if the file I am trying to read is at, say,



path/to/foo-orig.js


the script will look for the file at



path/to/foo.js-orig


and, obviously, this throws an error and fails.



How do I correct this bug in my script so that I handle the .js extension correctly?



Edit



TL;DR:



Given a string foo.bar how can I get foo-orig.bar?



Edit2:



I add the -orig extension because this project takes a template that's regularly updated by a third party, then I overlay my changes to it. So, I want to keep track of what files they change so I can go back and adapt my code when necessary.



list-of-files-to-compare.txt looks like the following.



list-of-files-to-compare.txt

public/assets/images/logos/logo.svg
README.md
src/auth/Auth.js
src/auth/store/actions/login.actions.js
src/auth/store/actions/user.actions.js
src/auth/store/reducers/user.reducer.js
src/firebaseService/firebaseService.js
src/firebaseService/firebaseServiceConfig.js
src/fuse-configs/fuseNavigationConfig.js
src/fuse-configs/fuseRoutesConfig.js
src/index.js
src/main/content/components/ComponentsConfig.js
src/main/content/login/Login.js
src/store.js
src/store/actions/index.js
src/store/reducers/index.js






bash shell shell-script bash-scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 at 8:01

























asked Nov 30 at 7:49









Mowzer

4623716




4623716












  • Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
    – Arkadiusz Drabczyk
    Nov 30 at 7:53










  • @ArkadiuszDrabczyk: Do the edits help answer your questions?
    – Mowzer
    Nov 30 at 8:01


















  • Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
    – Arkadiusz Drabczyk
    Nov 30 at 7:53










  • @ArkadiuszDrabczyk: Do the edits help answer your questions?
    – Mowzer
    Nov 30 at 8:01
















Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
– Arkadiusz Drabczyk
Nov 30 at 7:53




Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like?
– Arkadiusz Drabczyk
Nov 30 at 7:53












@ArkadiuszDrabczyk: Do the edits help answer your questions?
– Mowzer
Nov 30 at 8:01




@ArkadiuszDrabczyk: Do the edits help answer your questions?
– Mowzer
Nov 30 at 8:01










1 Answer
1






active

oldest

votes

















up vote
0
down vote













In Bash you could use Pattern substitution:



while IFS= read -r filename;
do
echo ${filename/.js/-orig.js}
done < list-of-files-to-compare.txt


If you want to be more generic you can first get an extension with cut and rev like that:



$ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
$ echo $ext
svg


And then replace it with sed:



$ echo public/assets/images/logos/logo.svg | sed "s,.${ext}$,-orig.${ext},"
public/assets/images/logos/logo-orig.svg


It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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',
    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%2fsuperuser.com%2fquestions%2f1379672%2fhow-to-handle-a-js-extension-correctly-in-bash-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    In Bash you could use Pattern substitution:



    while IFS= read -r filename;
    do
    echo ${filename/.js/-orig.js}
    done < list-of-files-to-compare.txt


    If you want to be more generic you can first get an extension with cut and rev like that:



    $ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
    $ echo $ext
    svg


    And then replace it with sed:



    $ echo public/assets/images/logos/logo.svg | sed "s,.${ext}$,-orig.${ext},"
    public/assets/images/logos/logo-orig.svg


    It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.






    share|improve this answer



























      up vote
      0
      down vote













      In Bash you could use Pattern substitution:



      while IFS= read -r filename;
      do
      echo ${filename/.js/-orig.js}
      done < list-of-files-to-compare.txt


      If you want to be more generic you can first get an extension with cut and rev like that:



      $ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
      $ echo $ext
      svg


      And then replace it with sed:



      $ echo public/assets/images/logos/logo.svg | sed "s,.${ext}$,-orig.${ext},"
      public/assets/images/logos/logo-orig.svg


      It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        In Bash you could use Pattern substitution:



        while IFS= read -r filename;
        do
        echo ${filename/.js/-orig.js}
        done < list-of-files-to-compare.txt


        If you want to be more generic you can first get an extension with cut and rev like that:



        $ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
        $ echo $ext
        svg


        And then replace it with sed:



        $ echo public/assets/images/logos/logo.svg | sed "s,.${ext}$,-orig.${ext},"
        public/assets/images/logos/logo-orig.svg


        It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.






        share|improve this answer














        In Bash you could use Pattern substitution:



        while IFS= read -r filename;
        do
        echo ${filename/.js/-orig.js}
        done < list-of-files-to-compare.txt


        If you want to be more generic you can first get an extension with cut and rev like that:



        $ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
        $ echo $ext
        svg


        And then replace it with sed:



        $ echo public/assets/images/logos/logo.svg | sed "s,.${ext}$,-orig.${ext},"
        public/assets/images/logos/logo-orig.svg


        It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 30 at 8:20

























        answered Nov 30 at 8:14









        Arkadiusz Drabczyk

        1,523711




        1,523711






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fsuperuser.com%2fquestions%2f1379672%2fhow-to-handle-a-js-extension-correctly-in-bash-script%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







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

             ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕