How can I create a Bash conditional script, based on output from a command?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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?
bash shell-script ubuntu files for
add a comment |Â
up vote
3
down vote
favorite
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?
bash shell-script ubuntu files for
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
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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?
bash shell-script ubuntu files for
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?
bash shell-script ubuntu files for
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
add a comment |Â
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
add a comment |Â
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)
I only changed the0
and1
tofalse
andtrue
, 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 inbash
,for file in *.mp4
...
â PesaThe
Dec 7 '17 at 17:48
 |Â
show 3 more comments
up vote
2
down vote
Something like:
for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done
Where is the condition then?
â Tina J
Dec 7 '17 at 16:40
1
Replacesource/*
bycurrent/*.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 withtee
. You can read abouttee
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
 |Â
show 4 more comments
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)
I only changed the0
and1
tofalse
andtrue
, 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 inbash
,for file in *.mp4
...
â PesaThe
Dec 7 '17 at 17:48
 |Â
show 3 more comments
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)
I only changed the0
and1
tofalse
andtrue
, 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 inbash
,for file in *.mp4
...
â PesaThe
Dec 7 '17 at 17:48
 |Â
show 3 more comments
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)
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)
edited Dec 7 '17 at 19:47
answered Dec 6 '17 at 22:05
PesaThe
47337
47337
I only changed the0
and1
tofalse
andtrue
, 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 inbash
,for file in *.mp4
...
â PesaThe
Dec 7 '17 at 17:48
 |Â
show 3 more comments
I only changed the0
and1
tofalse
andtrue
, 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 inbash
,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
 |Â
show 3 more comments
up vote
2
down vote
Something like:
for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done
Where is the condition then?
â Tina J
Dec 7 '17 at 16:40
1
Replacesource/*
bycurrent/*.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 withtee
. You can read abouttee
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
 |Â
show 4 more comments
up vote
2
down vote
Something like:
for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done
Where is the condition then?
â Tina J
Dec 7 '17 at 16:40
1
Replacesource/*
bycurrent/*.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 withtee
. You can read abouttee
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
 |Â
show 4 more comments
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
Something like:
for f in source/*
do
cp "$f" folder$(java -jar myProgram "$f" image.png | head -1)
done
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
Replacesource/*
bycurrent/*.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 withtee
. You can read abouttee
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
 |Â
show 4 more comments
Where is the condition then?
â Tina J
Dec 7 '17 at 16:40
1
Replacesource/*
bycurrent/*.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 withtee
. You can read abouttee
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
 |Â
show 4 more comments
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
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
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
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
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
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