Bash: Comparing names using xargs and if statement
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
add a comment |
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
add a comment |
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
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
bash xargs
asked Nov 22 '18 at 20:30
Thomas van der meulenThomas van der meulen
32
32
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The two main problems are
- You forgot the braces.
ifitself can only check the exit status of the following command andsomefile =~ *.wavis generally not a command, but[[ ... =~ ... ]]is.
=~uses extended regexes which are a bit different from globs. To match all files ending in literally.wavyou 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$'
add a comment |
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
add a comment |
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
});
}
});
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%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
The two main problems are
- You forgot the braces.
ifitself can only check the exit status of the following command andsomefile =~ *.wavis generally not a command, but[[ ... =~ ... ]]is.
=~uses extended regexes which are a bit different from globs. To match all files ending in literally.wavyou 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$'
add a comment |
The two main problems are
- You forgot the braces.
ifitself can only check the exit status of the following command andsomefile =~ *.wavis generally not a command, but[[ ... =~ ... ]]is.
=~uses extended regexes which are a bit different from globs. To match all files ending in literally.wavyou 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$'
add a comment |
The two main problems are
- You forgot the braces.
ifitself can only check the exit status of the following command andsomefile =~ *.wavis generally not a command, but[[ ... =~ ... ]]is.
=~uses extended regexes which are a bit different from globs. To match all files ending in literally.wavyou 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$'
The two main problems are
- You forgot the braces.
ifitself can only check the exit status of the following command andsomefile =~ *.wavis generally not a command, but[[ ... =~ ... ]]is.
=~uses extended regexes which are a bit different from globs. To match all files ending in literally.wavyou 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$'
edited Nov 22 '18 at 22:18
answered Nov 22 '18 at 22:06
SocowiSocowi
7,1862927
7,1862927
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 22:25
Rico ChenRico Chen
741511
741511
add a comment |
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f53437665%2fbash-comparing-names-using-xargs-and-if-statement%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