If-condition based on standard output
up vote
0
down vote
favorite
I have few commands linked through pipes, and at the end is a conditional awk
: example below
command1 | command 2 | awk '$1 > 800'
Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?
Like,
if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing
scripting stdout
add a comment |
up vote
0
down vote
favorite
I have few commands linked through pipes, and at the end is a conditional awk
: example below
command1 | command 2 | awk '$1 > 800'
Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?
Like,
if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing
scripting stdout
You could pipe the output into awhile read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?
– Bananguin
Nov 14 at 11:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have few commands linked through pipes, and at the end is a conditional awk
: example below
command1 | command 2 | awk '$1 > 800'
Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?
Like,
if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing
scripting stdout
I have few commands linked through pipes, and at the end is a conditional awk
: example below
command1 | command 2 | awk '$1 > 800'
Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?
Like,
if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing
scripting stdout
scripting stdout
edited Nov 14 at 12:36
Kusalananda
116k15218351
116k15218351
asked Nov 14 at 11:39
Sollosa
1361315
1361315
You could pipe the output into awhile read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?
– Bananguin
Nov 14 at 11:45
add a comment |
You could pipe the output into awhile read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?
– Bananguin
Nov 14 at 11:45
You could pipe the output into a
while read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?– Bananguin
Nov 14 at 11:45
You could pipe the output into a
while read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?– Bananguin
Nov 14 at 11:45
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
I normally just use command substitution, then put it in a test, e.g.
if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi
Explanation
- This runs the command as per your question:
command1 | command 2 | awk '$1 > 800'
- The output of this is passed to the test
[ ! -z "$(…)" ]
, which will be true if it is not!
a string of zero length-z
.
Hence, if there is output to the command pipe, the then
commands will run.
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
add a comment |
up vote
1
down vote
Make the awk
script exit with the correct return code for your if
statement:
if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi
Or, if you don't actually need the output of the awk
program:
if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that thatexit
would close the pipe and potentially causecommand2
to be killed with SIGPIPE (which may or may not be desired)
– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,awk
typically useslong
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
add a comment |
up vote
0
down vote
You can let awk
print the desired exit code, like this:
echo 900 | awk '{ print !($1 > 800) }'
Then this can be wrapped in a subshell using (
and exit
, for returning the value from awk
:
echo 900 | (exit $(awk '{ print !($1 > 800) }'))
Which can then be used as part of a pipeline:
echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
I normally just use command substitution, then put it in a test, e.g.
if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi
Explanation
- This runs the command as per your question:
command1 | command 2 | awk '$1 > 800'
- The output of this is passed to the test
[ ! -z "$(…)" ]
, which will be true if it is not!
a string of zero length-z
.
Hence, if there is output to the command pipe, the then
commands will run.
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
add a comment |
up vote
3
down vote
I normally just use command substitution, then put it in a test, e.g.
if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi
Explanation
- This runs the command as per your question:
command1 | command 2 | awk '$1 > 800'
- The output of this is passed to the test
[ ! -z "$(…)" ]
, which will be true if it is not!
a string of zero length-z
.
Hence, if there is output to the command pipe, the then
commands will run.
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
add a comment |
up vote
3
down vote
up vote
3
down vote
I normally just use command substitution, then put it in a test, e.g.
if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi
Explanation
- This runs the command as per your question:
command1 | command 2 | awk '$1 > 800'
- The output of this is passed to the test
[ ! -z "$(…)" ]
, which will be true if it is not!
a string of zero length-z
.
Hence, if there is output to the command pipe, the then
commands will run.
I normally just use command substitution, then put it in a test, e.g.
if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi
Explanation
- This runs the command as per your question:
command1 | command 2 | awk '$1 > 800'
- The output of this is passed to the test
[ ! -z "$(…)" ]
, which will be true if it is not!
a string of zero length-z
.
Hence, if there is output to the command pipe, the then
commands will run.
edited Nov 14 at 12:18
answered Nov 14 at 11:51
Sparhawk
8,82763789
8,82763789
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
add a comment |
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
Nov 14 at 12:11
add a comment |
up vote
1
down vote
Make the awk
script exit with the correct return code for your if
statement:
if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi
Or, if you don't actually need the output of the awk
program:
if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that thatexit
would close the pipe and potentially causecommand2
to be killed with SIGPIPE (which may or may not be desired)
– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,awk
typically useslong
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
add a comment |
up vote
1
down vote
Make the awk
script exit with the correct return code for your if
statement:
if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi
Or, if you don't actually need the output of the awk
program:
if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that thatexit
would close the pipe and potentially causecommand2
to be killed with SIGPIPE (which may or may not be desired)
– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,awk
typically useslong
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
add a comment |
up vote
1
down vote
up vote
1
down vote
Make the awk
script exit with the correct return code for your if
statement:
if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi
Or, if you don't actually need the output of the awk
program:
if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi
Make the awk
script exit with the correct return code for your if
statement:
if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi
Or, if you don't actually need the output of the awk
program:
if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi
edited Nov 14 at 12:35
answered Nov 14 at 12:30
Kusalananda
116k15218351
116k15218351
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that thatexit
would close the pipe and potentially causecommand2
to be killed with SIGPIPE (which may or may not be desired)
– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,awk
typically useslong
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
add a comment |
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that thatexit
would close the pipe and potentially causecommand2
to be killed with SIGPIPE (which may or may not be desired)
– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,awk
typically useslong
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
Nov 14 at 13:03
Note that that
exit
would close the pipe and potentially cause command2
to be killed with SIGPIPE (which may or may not be desired)– Stéphane Chazelas
Nov 14 at 13:11
Note that that
exit
would close the pipe and potentially cause command2
to be killed with SIGPIPE (which may or may not be desired)– Stéphane Chazelas
Nov 14 at 13:11
@ctrl-alt-delor,
awk
typically uses long
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.– Stéphane Chazelas
Nov 14 at 13:14
@ctrl-alt-delor,
awk
typically uses long
C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.– Stéphane Chazelas
Nov 14 at 13:14
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
– ctrl-alt-delor
Nov 14 at 13:18
add a comment |
up vote
0
down vote
You can let awk
print the desired exit code, like this:
echo 900 | awk '{ print !($1 > 800) }'
Then this can be wrapped in a subshell using (
and exit
, for returning the value from awk
:
echo 900 | (exit $(awk '{ print !($1 > 800) }'))
Which can then be used as part of a pipeline:
echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no
add a comment |
up vote
0
down vote
You can let awk
print the desired exit code, like this:
echo 900 | awk '{ print !($1 > 800) }'
Then this can be wrapped in a subshell using (
and exit
, for returning the value from awk
:
echo 900 | (exit $(awk '{ print !($1 > 800) }'))
Which can then be used as part of a pipeline:
echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no
add a comment |
up vote
0
down vote
up vote
0
down vote
You can let awk
print the desired exit code, like this:
echo 900 | awk '{ print !($1 > 800) }'
Then this can be wrapped in a subshell using (
and exit
, for returning the value from awk
:
echo 900 | (exit $(awk '{ print !($1 > 800) }'))
Which can then be used as part of a pipeline:
echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no
You can let awk
print the desired exit code, like this:
echo 900 | awk '{ print !($1 > 800) }'
Then this can be wrapped in a subshell using (
and exit
, for returning the value from awk
:
echo 900 | (exit $(awk '{ print !($1 > 800) }'))
Which can then be used as part of a pipeline:
echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no
answered Nov 14 at 12:31
Alexander
5,72812043
5,72812043
add a comment |
add a comment |
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%2f481684%2fif-condition-based-on-standard-output%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
You could pipe the output into a
while read
construct. The while loop will iterate once for every line of output. Is that what you are looking for?– Bananguin
Nov 14 at 11:45