Call of HandBrakeCLI within script breaks loop
Clash Royale CLAN TAG#URR8PPP
In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:
FILES="xxx*.ts
yyy.ts"
for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"
if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done
So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts
Why does the HandBrakeCLI call breaks my loop and how can I prevent it?
bash shell-script
add a comment |
In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:
FILES="xxx*.ts
yyy.ts"
for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"
if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done
So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts
Why does the HandBrakeCLI call breaks my loop and how can I prevent it?
bash shell-script
Typo:[[ ... ]] then
should be[[ ... ]]; then
. Also, you'd be better off withFILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote$f
. Consider using shellcheck.net
– Kusalananda
Jan 27 at 18:47
add a comment |
In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:
FILES="xxx*.ts
yyy.ts"
for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"
if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done
So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts
Why does the HandBrakeCLI call breaks my loop and how can I prevent it?
bash shell-script
In my bash script I have a while loop as follows. What I don't understand is, why the call of HandBrakeCLI breaks the loop.
If the 'if' condition is false and 'else' is follwed, the loop continues. Here is my script:
FILES="xxx*.ts
yyy.ts"
for f in $FILES
do
find /pathto -name $f -print | while IFS= read -r file; do
echo "file found = $file"
outputfile=$outputpath$(basename "$file")".mp4"
if [[ ! -f $outputfile ]] then
HandBrakeCLI -i "$file" -o "$outputfile" -e x264 -q 22 -r 15 -B 64 -X 480 -O
else echo "outputfile already exists: $outputfile"
fi
done
done
So I have three files, named xxx1.ts, xxx2.ts and yyy.ts.
All should be found and converted by HandBrakeCLI. But after the first conversion (of xxx1.ts) the while loop breaks and yyy.ts is processed.
If I start the script again, it finds the first file being already converted and enters the else branch. This time the while loop doesn't break and it processes xxx2.ts and then yyy.ts
Why does the HandBrakeCLI call breaks my loop and how can I prevent it?
bash shell-script
bash shell-script
edited Jan 27 at 18:48
Olaf
asked Jan 27 at 18:44
OlafOlaf
112
112
Typo:[[ ... ]] then
should be[[ ... ]]; then
. Also, you'd be better off withFILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote$f
. Consider using shellcheck.net
– Kusalananda
Jan 27 at 18:47
add a comment |
Typo:[[ ... ]] then
should be[[ ... ]]; then
. Also, you'd be better off withFILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote$f
. Consider using shellcheck.net
– Kusalananda
Jan 27 at 18:47
Typo:
[[ ... ]] then
should be [[ ... ]]; then
. Also, you'd be better off with FILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f
. Consider using shellcheck.net– Kusalananda
Jan 27 at 18:47
Typo:
[[ ... ]] then
should be [[ ... ]]; then
. Also, you'd be better off with FILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote $f
. Consider using shellcheck.net– Kusalananda
Jan 27 at 18:47
add a comment |
1 Answer
1
active
oldest
votes
while
as written is not safe to use as standard input is here shared between processes so HandBrakeCLI
is likely consuming standard input as well. This can be seen in a minimal example:
$ printf "anbncn"
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here
and thus the while
loop exits early as there is nothing else to read from standard input after sed
graffitied up the lines.
Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3
while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")
or to switch to a different programming language. (sed
will now hang, unless you feed standard input to it, somehow.)
Another option would be to close standard input to your program, e.g.
HandBrakeCLI ... <&-
as that will prevent it from seeing the standard input the while
loop needs.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f497065%2fcall-of-handbrakecli-within-script-breaks-loop%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
while
as written is not safe to use as standard input is here shared between processes so HandBrakeCLI
is likely consuming standard input as well. This can be seen in a minimal example:
$ printf "anbncn"
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here
and thus the while
loop exits early as there is nothing else to read from standard input after sed
graffitied up the lines.
Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3
while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")
or to switch to a different programming language. (sed
will now hang, unless you feed standard input to it, somehow.)
Another option would be to close standard input to your program, e.g.
HandBrakeCLI ... <&-
as that will prevent it from seeing the standard input the while
loop needs.
add a comment |
while
as written is not safe to use as standard input is here shared between processes so HandBrakeCLI
is likely consuming standard input as well. This can be seen in a minimal example:
$ printf "anbncn"
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here
and thus the while
loop exits early as there is nothing else to read from standard input after sed
graffitied up the lines.
Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3
while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")
or to switch to a different programming language. (sed
will now hang, unless you feed standard input to it, somehow.)
Another option would be to close standard input to your program, e.g.
HandBrakeCLI ... <&-
as that will prevent it from seeing the standard input the while
loop needs.
add a comment |
while
as written is not safe to use as standard input is here shared between processes so HandBrakeCLI
is likely consuming standard input as well. This can be seen in a minimal example:
$ printf "anbncn"
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here
and thus the while
loop exits early as there is nothing else to read from standard input after sed
graffitied up the lines.
Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3
while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")
or to switch to a different programming language. (sed
will now hang, unless you feed standard input to it, somehow.)
Another option would be to close standard input to your program, e.g.
HandBrakeCLI ... <&-
as that will prevent it from seeing the standard input the while
loop needs.
while
as written is not safe to use as standard input is here shared between processes so HandBrakeCLI
is likely consuming standard input as well. This can be seen in a minimal example:
$ printf "anbncn"
| while IFS= read -r x; do echo $x; sed 's/$/ sed was here/'; done
a
b sed was here
c sed was here
and thus the while
loop exits early as there is nothing else to read from standard input after sed
graffitied up the lines.
Workarounds are to hope and pray nothing else in the code is using some other file descriptor, say 3
while IFS= read -u 3 -r x; do
echo $x
sed 's/$/ sed read this line/'
done 3< <(printf "anbncn")
or to switch to a different programming language. (sed
will now hang, unless you feed standard input to it, somehow.)
Another option would be to close standard input to your program, e.g.
HandBrakeCLI ... <&-
as that will prevent it from seeing the standard input the while
loop needs.
answered Jan 27 at 19:22
thrigthrig
24.9k23157
24.9k23157
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f497065%2fcall-of-handbrakecli-within-script-breaks-loop%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
Typo:
[[ ... ]] then
should be[[ ... ]]; then
. Also, you'd be better off withFILES
as an array, or you'd be matching filenames in the current directory with those patterns. And double quote$f
. Consider using shellcheck.net– Kusalananda
Jan 27 at 18:47