How can I create a Bash conditional script, based on output from a command?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












I have a Java program that gets two arguments (a video file name and an image) and outputs a boolean (0 or 1) in the first line:



java -jar myProgram video1.mp4 image.png
> 0
>some extra information...
>other extra information....going on


Now using bash script, I need to iterate through all files in a folder (not files in nested folders), run the program with the file name passed to the first argument (video name changes everytime, and image is fixed), and if the output in the first line is 0, copy the file in folder0, and if the output is 1, copy the file to folder1.



How can I achieve that in bash?







share|improve this question






















  • Are the > signs part of the output?
    – ilkkachu
    Dec 6 '17 at 22:01










  • oh no. Just to show it is output!
    – Tina J
    Dec 6 '17 at 22:04














up vote
3
down vote

favorite
1












I have a Java program that gets two arguments (a video file name and an image) and outputs a boolean (0 or 1) in the first line:



java -jar myProgram video1.mp4 image.png
> 0
>some extra information...
>other extra information....going on


Now using bash script, I need to iterate through all files in a folder (not files in nested folders), run the program with the file name passed to the first argument (video name changes everytime, and image is fixed), and if the output in the first line is 0, copy the file in folder0, and if the output is 1, copy the file to folder1.



How can I achieve that in bash?







share|improve this question






















  • Are the > signs part of the output?
    – ilkkachu
    Dec 6 '17 at 22:01










  • oh no. Just to show it is output!
    – Tina J
    Dec 6 '17 at 22:04












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I have a Java program that gets two arguments (a video file name and an image) and outputs a boolean (0 or 1) in the first line:



java -jar myProgram video1.mp4 image.png
> 0
>some extra information...
>other extra information....going on


Now using bash script, I need to iterate through all files in a folder (not files in nested folders), run the program with the file name passed to the first argument (video name changes everytime, and image is fixed), and if the output in the first line is 0, copy the file in folder0, and if the output is 1, copy the file to folder1.



How can I achieve that in bash?







share|improve this question














I have a Java program that gets two arguments (a video file name and an image) and outputs a boolean (0 or 1) in the first line:



java -jar myProgram video1.mp4 image.png
> 0
>some extra information...
>other extra information....going on


Now using bash script, I need to iterate through all files in a folder (not files in nested folders), run the program with the file name passed to the first argument (video name changes everytime, and image is fixed), and if the output in the first line is 0, copy the file in folder0, and if the output is 1, copy the file to folder1.



How can I achieve that in bash?









share|improve this question













share|improve this question




share|improve this question








edited Dec 6 '17 at 22:28









Time4Tea

866119




866119










asked Dec 6 '17 at 21:57









Tina J

1215




1215











  • Are the > signs part of the output?
    – ilkkachu
    Dec 6 '17 at 22:01










  • oh no. Just to show it is output!
    – Tina J
    Dec 6 '17 at 22:04
















  • Are the > signs part of the output?
    – ilkkachu
    Dec 6 '17 at 22:01










  • oh no. Just to show it is output!
    – Tina J
    Dec 6 '17 at 22:04















Are the > signs part of the output?
– ilkkachu
Dec 6 '17 at 22:01




Are the > signs part of the output?
– ilkkachu
Dec 6 '17 at 22:01












oh no. Just to show it is output!
– Tina J
Dec 6 '17 at 22:04




oh no. Just to show it is output!
– Tina J
Dec 6 '17 at 22:04










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You have much better control when using conditional statements:



for file in *; do
if [[ -f "$file" ]]; then
output=$(java -jar myProgram "$file" image.png | head -n 1)
[[ $output = "0" ]] && cp -- "$file" folder0
[[ $output = "1" ]] && cp -- "$file" folder1
fi
done


EDIT: if you still want to see the output of java, you can use this:



output=$(java -jar myProgram "$file" image.png | tee /dev/tty | head -n 1)





share|improve this answer






















  • I only changed the 0 and 1 to false and true, but I get error:
    – Tina J
    Dec 7 '17 at 16:25










  • runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
    – Tina J
    Dec 7 '17 at 16:25










  • Even with the original 0 and 1 id shows the same error.
    – Tina J
    Dec 7 '17 at 16:41






  • 1




    @TinaJ There has to be the space before =. So it should be $output = 1.
    – PesaThe
    Dec 7 '17 at 16:55







  • 1




    @TinaJ Please, read on filename expansion in bash, for file in *.mp4...
    – PesaThe
    Dec 7 '17 at 17:48

















up vote
2
down vote













Something like:



for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done





share|improve this answer




















  • Where is the condition then?
    – Tina J
    Dec 7 '17 at 16:40






  • 1




    Replace source/* by current/*.mp4
    – Patrick Mevzek
    Dec 7 '17 at 17:00







  • 1




    This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
    – Patrick Mevzek
    Dec 7 '17 at 17:06






  • 1




    @TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
    – PesaThe
    Dec 7 '17 at 19:20






  • 1




    Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
    – Tina J
    Dec 7 '17 at 19:45










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409326%2fhow-can-i-create-a-bash-conditional-script-based-on-output-from-a-command%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You have much better control when using conditional statements:



for file in *; do
if [[ -f "$file" ]]; then
output=$(java -jar myProgram "$file" image.png | head -n 1)
[[ $output = "0" ]] && cp -- "$file" folder0
[[ $output = "1" ]] && cp -- "$file" folder1
fi
done


EDIT: if you still want to see the output of java, you can use this:



output=$(java -jar myProgram "$file" image.png | tee /dev/tty | head -n 1)





share|improve this answer






















  • I only changed the 0 and 1 to false and true, but I get error:
    – Tina J
    Dec 7 '17 at 16:25










  • runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
    – Tina J
    Dec 7 '17 at 16:25










  • Even with the original 0 and 1 id shows the same error.
    – Tina J
    Dec 7 '17 at 16:41






  • 1




    @TinaJ There has to be the space before =. So it should be $output = 1.
    – PesaThe
    Dec 7 '17 at 16:55







  • 1




    @TinaJ Please, read on filename expansion in bash, for file in *.mp4...
    – PesaThe
    Dec 7 '17 at 17:48














up vote
3
down vote



accepted










You have much better control when using conditional statements:



for file in *; do
if [[ -f "$file" ]]; then
output=$(java -jar myProgram "$file" image.png | head -n 1)
[[ $output = "0" ]] && cp -- "$file" folder0
[[ $output = "1" ]] && cp -- "$file" folder1
fi
done


EDIT: if you still want to see the output of java, you can use this:



output=$(java -jar myProgram "$file" image.png | tee /dev/tty | head -n 1)





share|improve this answer






















  • I only changed the 0 and 1 to false and true, but I get error:
    – Tina J
    Dec 7 '17 at 16:25










  • runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
    – Tina J
    Dec 7 '17 at 16:25










  • Even with the original 0 and 1 id shows the same error.
    – Tina J
    Dec 7 '17 at 16:41






  • 1




    @TinaJ There has to be the space before =. So it should be $output = 1.
    – PesaThe
    Dec 7 '17 at 16:55







  • 1




    @TinaJ Please, read on filename expansion in bash, for file in *.mp4...
    – PesaThe
    Dec 7 '17 at 17:48












up vote
3
down vote



accepted







up vote
3
down vote



accepted






You have much better control when using conditional statements:



for file in *; do
if [[ -f "$file" ]]; then
output=$(java -jar myProgram "$file" image.png | head -n 1)
[[ $output = "0" ]] && cp -- "$file" folder0
[[ $output = "1" ]] && cp -- "$file" folder1
fi
done


EDIT: if you still want to see the output of java, you can use this:



output=$(java -jar myProgram "$file" image.png | tee /dev/tty | head -n 1)





share|improve this answer














You have much better control when using conditional statements:



for file in *; do
if [[ -f "$file" ]]; then
output=$(java -jar myProgram "$file" image.png | head -n 1)
[[ $output = "0" ]] && cp -- "$file" folder0
[[ $output = "1" ]] && cp -- "$file" folder1
fi
done


EDIT: if you still want to see the output of java, you can use this:



output=$(java -jar myProgram "$file" image.png | tee /dev/tty | head -n 1)






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 7 '17 at 19:47

























answered Dec 6 '17 at 22:05









PesaThe

47337




47337











  • I only changed the 0 and 1 to false and true, but I get error:
    – Tina J
    Dec 7 '17 at 16:25










  • runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
    – Tina J
    Dec 7 '17 at 16:25










  • Even with the original 0 and 1 id shows the same error.
    – Tina J
    Dec 7 '17 at 16:41






  • 1




    @TinaJ There has to be the space before =. So it should be $output = 1.
    – PesaThe
    Dec 7 '17 at 16:55







  • 1




    @TinaJ Please, read on filename expansion in bash, for file in *.mp4...
    – PesaThe
    Dec 7 '17 at 17:48
















  • I only changed the 0 and 1 to false and true, but I get error:
    – Tina J
    Dec 7 '17 at 16:25










  • runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
    – Tina J
    Dec 7 '17 at 16:25










  • Even with the original 0 and 1 id shows the same error.
    – Tina J
    Dec 7 '17 at 16:41






  • 1




    @TinaJ There has to be the space before =. So it should be $output = 1.
    – PesaThe
    Dec 7 '17 at 16:55







  • 1




    @TinaJ Please, read on filename expansion in bash, for file in *.mp4...
    – PesaThe
    Dec 7 '17 at 17:48















I only changed the 0 and 1 to false and true, but I get error:
– Tina J
Dec 7 '17 at 16:25




I only changed the 0 and 1 to false and true, but I get error:
– Tina J
Dec 7 '17 at 16:25












runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
– Tina J
Dec 7 '17 at 16:25




runme.sh: line 4: conditional binary operator expected runme.sh: line 4: syntax error near "true"' runme.sh: line 4: ` [[ $output= "true" ]] && cp -- "$file" video'`
– Tina J
Dec 7 '17 at 16:25












Even with the original 0 and 1 id shows the same error.
– Tina J
Dec 7 '17 at 16:41




Even with the original 0 and 1 id shows the same error.
– Tina J
Dec 7 '17 at 16:41




1




1




@TinaJ There has to be the space before =. So it should be $output = 1.
– PesaThe
Dec 7 '17 at 16:55





@TinaJ There has to be the space before =. So it should be $output = 1.
– PesaThe
Dec 7 '17 at 16:55





1




1




@TinaJ Please, read on filename expansion in bash, for file in *.mp4...
– PesaThe
Dec 7 '17 at 17:48




@TinaJ Please, read on filename expansion in bash, for file in *.mp4...
– PesaThe
Dec 7 '17 at 17:48












up vote
2
down vote













Something like:



for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done





share|improve this answer




















  • Where is the condition then?
    – Tina J
    Dec 7 '17 at 16:40






  • 1




    Replace source/* by current/*.mp4
    – Patrick Mevzek
    Dec 7 '17 at 17:00







  • 1




    This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
    – Patrick Mevzek
    Dec 7 '17 at 17:06






  • 1




    @TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
    – PesaThe
    Dec 7 '17 at 19:20






  • 1




    Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
    – Tina J
    Dec 7 '17 at 19:45














up vote
2
down vote













Something like:



for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done





share|improve this answer




















  • Where is the condition then?
    – Tina J
    Dec 7 '17 at 16:40






  • 1




    Replace source/* by current/*.mp4
    – Patrick Mevzek
    Dec 7 '17 at 17:00







  • 1




    This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
    – Patrick Mevzek
    Dec 7 '17 at 17:06






  • 1




    @TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
    – PesaThe
    Dec 7 '17 at 19:20






  • 1




    Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
    – Tina J
    Dec 7 '17 at 19:45












up vote
2
down vote










up vote
2
down vote









Something like:



for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done





share|improve this answer












Something like:



for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 6 '17 at 22:10









Patrick Mevzek

2,0381721




2,0381721











  • Where is the condition then?
    – Tina J
    Dec 7 '17 at 16:40






  • 1




    Replace source/* by current/*.mp4
    – Patrick Mevzek
    Dec 7 '17 at 17:00







  • 1




    This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
    – Patrick Mevzek
    Dec 7 '17 at 17:06






  • 1




    @TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
    – PesaThe
    Dec 7 '17 at 19:20






  • 1




    Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
    – Tina J
    Dec 7 '17 at 19:45
















  • Where is the condition then?
    – Tina J
    Dec 7 '17 at 16:40






  • 1




    Replace source/* by current/*.mp4
    – Patrick Mevzek
    Dec 7 '17 at 17:00







  • 1




    This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
    – Patrick Mevzek
    Dec 7 '17 at 17:06






  • 1




    @TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
    – PesaThe
    Dec 7 '17 at 19:20






  • 1




    Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
    – Tina J
    Dec 7 '17 at 19:45















Where is the condition then?
– Tina J
Dec 7 '17 at 16:40




Where is the condition then?
– Tina J
Dec 7 '17 at 16:40




1




1




Replace source/* by current/*.mp4
– Patrick Mevzek
Dec 7 '17 at 17:00





Replace source/* by current/*.mp4
– Patrick Mevzek
Dec 7 '17 at 17:00





1




1




This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
– Patrick Mevzek
Dec 7 '17 at 17:06




This is expected, the script just run the cp command to copy files and the java command output is captured inside the $(...) construct, there should be no output except if problems.
– Patrick Mevzek
Dec 7 '17 at 17:06




1




1




@TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
– PesaThe
Dec 7 '17 at 19:20




@TinaJ Yes, you can achieve that with tee. You can read about tee in manual and refer to my answer for solution.
– PesaThe
Dec 7 '17 at 19:20




1




1




Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
– Tina J
Dec 7 '17 at 19:45




Thanks. Both answers are great. I have a hard time selecting the answer. The other one was 5 min faster ;)
– Tina J
Dec 7 '17 at 19:45

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409326%2fhow-can-i-create-a-bash-conditional-script-based-on-output-from-a-command%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay