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
bash shell shell-script bash-scripting
add a comment |
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
bash shell shell-script bash-scripting
Why do you add-orig
in the first place? How doeslist-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
add a comment |
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
bash shell shell-script bash-scripting
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
bash shell shell-script bash-scripting
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 doeslist-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
add a comment |
Why do you add-orig
in the first place? How doeslist-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
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Nov 30 at 8:20
answered Nov 30 at 8:14
Arkadiusz Drabczyk
1,523711
1,523711
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Why do you add
-orig
in the first place? How doeslist-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