If-condition based on standard output
Clash Royale CLAN TAG#URR8PPP
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
yesterday
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 yesterday
Kusalananda
115k15218350
115k15218350
asked yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
@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
yesterday
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
yesterday
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
yesterday
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
yesterday
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 yesterday
answered yesterday
Sparhawk
8,80863789
8,80863789
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
yesterday
add a comment |
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
yesterday
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
yesterday
Thanks @Sparhawk that is exactly what I was looking for.
– Sollosa
yesterday
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
yesterday
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
yesterday
@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
yesterday
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
yesterday
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
yesterday
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
yesterday
@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
yesterday
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
yesterday
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 yesterday
answered yesterday
Kusalananda
115k15218350
115k15218350
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
yesterday
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
yesterday
@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
yesterday
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
yesterday
add a comment |
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
yesterday
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
yesterday
@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
yesterday
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
yesterday
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
yesterday
Could c wrap (or is awk arbitrary precision)?
– ctrl-alt-delor
yesterday
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
yesterday
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
yesterday
@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
yesterday
@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
yesterday
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
yesterday
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
yesterday
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 yesterday
Alexander
5,71812043
5,71812043
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
yesterday