can't get specific line from bash output
up vote
0
down vote
favorite
I'm trying simple bash script:
/usr/libexec/java_home -V
output is:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Seems to be 4 lines, but if I try to get second line:
/usr/libexec/java_home -V | sed -n 2p
The output is the same. No additional line. If I try the first one - I got the second:
/usr/libexec/java_home -V | sed -n 1p
Output:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
If I assign output to array and count it's size:
array=( $(/usr/libexec/java_home -V) )
array_size=$#array[@]
echo $array_size
it shows size = 1:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
1
What am I doing wrong?
bash stdout stderr io-redirection
add a comment |
up vote
0
down vote
favorite
I'm trying simple bash script:
/usr/libexec/java_home -V
output is:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Seems to be 4 lines, but if I try to get second line:
/usr/libexec/java_home -V | sed -n 2p
The output is the same. No additional line. If I try the first one - I got the second:
/usr/libexec/java_home -V | sed -n 1p
Output:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
If I assign output to array and count it's size:
array=( $(/usr/libexec/java_home -V) )
array_size=$#array[@]
echo $array_size
it shows size = 1:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
1
What am I doing wrong?
bash stdout stderr io-redirection
3
Likely output is tostderr
instead ofstdout
, Try/usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the"Matching Java.."
is tostdout
and the rest tostderr
. (hard to tell). You can always use/usr/libexec/java_home -V 2>/dev/null
to eliminate what is output tostderr
to test. If it is all tostdout
, then there is something preventing normal word-splitting from occurring. Have you alteredIFS
in your parent shell?
– David C. Rankin
Nov 11 at 0:16
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!
– Iga
Nov 11 at 0:34
Sure, glad it helped. Yoursed
call was correct, so the only other likely cause was the script was outputting tostderr
(which isn't being piped tosed
and just appears on your screen making it look likesed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way:)
– David C. Rankin
Nov 11 at 0:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying simple bash script:
/usr/libexec/java_home -V
output is:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Seems to be 4 lines, but if I try to get second line:
/usr/libexec/java_home -V | sed -n 2p
The output is the same. No additional line. If I try the first one - I got the second:
/usr/libexec/java_home -V | sed -n 1p
Output:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
If I assign output to array and count it's size:
array=( $(/usr/libexec/java_home -V) )
array_size=$#array[@]
echo $array_size
it shows size = 1:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
1
What am I doing wrong?
bash stdout stderr io-redirection
I'm trying simple bash script:
/usr/libexec/java_home -V
output is:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Seems to be 4 lines, but if I try to get second line:
/usr/libexec/java_home -V | sed -n 2p
The output is the same. No additional line. If I try the first one - I got the second:
/usr/libexec/java_home -V | sed -n 1p
Output:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
If I assign output to array and count it's size:
array=( $(/usr/libexec/java_home -V) )
array_size=$#array[@]
echo $array_size
it shows size = 1:
Matching Java Virtual Machines (3):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
1
What am I doing wrong?
bash stdout stderr io-redirection
bash stdout stderr io-redirection
edited Nov 11 at 0:43
Cyrus
44.9k43676
44.9k43676
asked Nov 11 at 0:10
Iga
335
335
3
Likely output is tostderr
instead ofstdout
, Try/usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the"Matching Java.."
is tostdout
and the rest tostderr
. (hard to tell). You can always use/usr/libexec/java_home -V 2>/dev/null
to eliminate what is output tostderr
to test. If it is all tostdout
, then there is something preventing normal word-splitting from occurring. Have you alteredIFS
in your parent shell?
– David C. Rankin
Nov 11 at 0:16
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!
– Iga
Nov 11 at 0:34
Sure, glad it helped. Yoursed
call was correct, so the only other likely cause was the script was outputting tostderr
(which isn't being piped tosed
and just appears on your screen making it look likesed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way:)
– David C. Rankin
Nov 11 at 0:40
add a comment |
3
Likely output is tostderr
instead ofstdout
, Try/usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the"Matching Java.."
is tostdout
and the rest tostderr
. (hard to tell). You can always use/usr/libexec/java_home -V 2>/dev/null
to eliminate what is output tostderr
to test. If it is all tostdout
, then there is something preventing normal word-splitting from occurring. Have you alteredIFS
in your parent shell?
– David C. Rankin
Nov 11 at 0:16
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!
– Iga
Nov 11 at 0:34
Sure, glad it helped. Yoursed
call was correct, so the only other likely cause was the script was outputting tostderr
(which isn't being piped tosed
and just appears on your screen making it look likesed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way:)
– David C. Rankin
Nov 11 at 0:40
3
3
Likely output is to
stderr
instead of stdout
, Try /usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the "Matching Java.."
is to stdout
and the rest to stderr
. (hard to tell). You can always use /usr/libexec/java_home -V 2>/dev/null
to eliminate what is output to stderr
to test. If it is all to stdout
, then there is something preventing normal word-splitting from occurring. Have you altered IFS
in your parent shell?– David C. Rankin
Nov 11 at 0:16
Likely output is to
stderr
instead of stdout
, Try /usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the "Matching Java.."
is to stdout
and the rest to stderr
. (hard to tell). You can always use /usr/libexec/java_home -V 2>/dev/null
to eliminate what is output to stderr
to test. If it is all to stdout
, then there is something preventing normal word-splitting from occurring. Have you altered IFS
in your parent shell?– David C. Rankin
Nov 11 at 0:16
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!– Iga
Nov 11 at 0:34
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!– Iga
Nov 11 at 0:34
Sure, glad it helped. Your
sed
call was correct, so the only other likely cause was the script was outputting to stderr
(which isn't being piped to sed
and just appears on your screen making it look like sed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way :)
– David C. Rankin
Nov 11 at 0:40
Sure, glad it helped. Your
sed
call was correct, so the only other likely cause was the script was outputting to stderr
(which isn't being piped to sed
and just appears on your screen making it look like sed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way :)
– David C. Rankin
Nov 11 at 0:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
What appears to be happening is that your script, /usr/libexec/java_home -V
is outputting some, or all, of its output to stderr
instead of writing to stdout
. Since the shell pipe ('|'
) connects the stdout
of the previous command to stdin
of the command that follows, any output to stderr
by /usr/libexec/java_home -V
just gets written to your screen and is never processed by sed
making it look like sed
wasn't working.
To test and correct the problem is that is the case, just redirect stderr
from your script to stdout
and then pipe the result to sed
, making sure sed
receives all of the output from your script, e.g.
$ /usr/libexec/java_home -V 2>&1 | sed -n 1p
Glad it helped.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53244665%2fcant-get-specific-line-from-bash-output%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
up vote
1
down vote
accepted
What appears to be happening is that your script, /usr/libexec/java_home -V
is outputting some, or all, of its output to stderr
instead of writing to stdout
. Since the shell pipe ('|'
) connects the stdout
of the previous command to stdin
of the command that follows, any output to stderr
by /usr/libexec/java_home -V
just gets written to your screen and is never processed by sed
making it look like sed
wasn't working.
To test and correct the problem is that is the case, just redirect stderr
from your script to stdout
and then pipe the result to sed
, making sure sed
receives all of the output from your script, e.g.
$ /usr/libexec/java_home -V 2>&1 | sed -n 1p
Glad it helped.
add a comment |
up vote
1
down vote
accepted
What appears to be happening is that your script, /usr/libexec/java_home -V
is outputting some, or all, of its output to stderr
instead of writing to stdout
. Since the shell pipe ('|'
) connects the stdout
of the previous command to stdin
of the command that follows, any output to stderr
by /usr/libexec/java_home -V
just gets written to your screen and is never processed by sed
making it look like sed
wasn't working.
To test and correct the problem is that is the case, just redirect stderr
from your script to stdout
and then pipe the result to sed
, making sure sed
receives all of the output from your script, e.g.
$ /usr/libexec/java_home -V 2>&1 | sed -n 1p
Glad it helped.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
What appears to be happening is that your script, /usr/libexec/java_home -V
is outputting some, or all, of its output to stderr
instead of writing to stdout
. Since the shell pipe ('|'
) connects the stdout
of the previous command to stdin
of the command that follows, any output to stderr
by /usr/libexec/java_home -V
just gets written to your screen and is never processed by sed
making it look like sed
wasn't working.
To test and correct the problem is that is the case, just redirect stderr
from your script to stdout
and then pipe the result to sed
, making sure sed
receives all of the output from your script, e.g.
$ /usr/libexec/java_home -V 2>&1 | sed -n 1p
Glad it helped.
What appears to be happening is that your script, /usr/libexec/java_home -V
is outputting some, or all, of its output to stderr
instead of writing to stdout
. Since the shell pipe ('|'
) connects the stdout
of the previous command to stdin
of the command that follows, any output to stderr
by /usr/libexec/java_home -V
just gets written to your screen and is never processed by sed
making it look like sed
wasn't working.
To test and correct the problem is that is the case, just redirect stderr
from your script to stdout
and then pipe the result to sed
, making sure sed
receives all of the output from your script, e.g.
$ /usr/libexec/java_home -V 2>&1 | sed -n 1p
Glad it helped.
answered Nov 11 at 0:46
David C. Rankin
39.9k32647
39.9k32647
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f53244665%2fcant-get-specific-line-from-bash-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
3
Likely output is to
stderr
instead ofstdout
, Try/usr/libexec/java_home -V 2>&1 | sed -n 1p
Possible the"Matching Java.."
is tostdout
and the rest tostderr
. (hard to tell). You can always use/usr/libexec/java_home -V 2>/dev/null
to eliminate what is output tostderr
to test. If it is all tostdout
, then there is something preventing normal word-splitting from occurring. Have you alteredIFS
in your parent shell?– David C. Rankin
Nov 11 at 0:16
/usr/libexec/java_home -V 2>&1 | sed -n 1p
works nice! Thank you!– Iga
Nov 11 at 0:34
Sure, glad it helped. Your
sed
call was correct, so the only other likely cause was the script was outputting tostderr
(which isn't being piped tosed
and just appears on your screen making it look likesed
wasn't working). Now you know what to look for when something like that pops up -- we have all learned that lesson the same way:)
– David C. Rankin
Nov 11 at 0:40