Passing a number to a command run thru bash
I'm running a command thru bash, and saving the output to a variable:
response=$(COMMAND $line 2>&1)
Where COMMAND $line
produces an output.
The trouble is, sometimes this command produces something like:
Found 2 possible matches:
[1] Match A
[2] Match B
Choose one:
How do I account for this in my bash code? Parsing the output doesn't seem to work:
elif [[ "$response" == *"multipleMatches"* ]]
then
echo 1
echo "$line" >> /home/fawzy/check_by_hand.txt
fi
Where $multipleMatches="possible"
How do I account for this, and pass a value to the command being run, so that my script doesn't lock up?
bash
New contributor
add a comment |
I'm running a command thru bash, and saving the output to a variable:
response=$(COMMAND $line 2>&1)
Where COMMAND $line
produces an output.
The trouble is, sometimes this command produces something like:
Found 2 possible matches:
[1] Match A
[2] Match B
Choose one:
How do I account for this in my bash code? Parsing the output doesn't seem to work:
elif [[ "$response" == *"multipleMatches"* ]]
then
echo 1
echo "$line" >> /home/fawzy/check_by_hand.txt
fi
Where $multipleMatches="possible"
How do I account for this, and pass a value to the command being run, so that my script doesn't lock up?
bash
New contributor
Your problem is that the$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do$(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.
– Stephen Harris
10 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
1
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
And if the tool is well written, you may not even need to clean the output, just drop2>&1
.
– Kamil Maciorowski
4 mins ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago
add a comment |
I'm running a command thru bash, and saving the output to a variable:
response=$(COMMAND $line 2>&1)
Where COMMAND $line
produces an output.
The trouble is, sometimes this command produces something like:
Found 2 possible matches:
[1] Match A
[2] Match B
Choose one:
How do I account for this in my bash code? Parsing the output doesn't seem to work:
elif [[ "$response" == *"multipleMatches"* ]]
then
echo 1
echo "$line" >> /home/fawzy/check_by_hand.txt
fi
Where $multipleMatches="possible"
How do I account for this, and pass a value to the command being run, so that my script doesn't lock up?
bash
New contributor
I'm running a command thru bash, and saving the output to a variable:
response=$(COMMAND $line 2>&1)
Where COMMAND $line
produces an output.
The trouble is, sometimes this command produces something like:
Found 2 possible matches:
[1] Match A
[2] Match B
Choose one:
How do I account for this in my bash code? Parsing the output doesn't seem to work:
elif [[ "$response" == *"multipleMatches"* ]]
then
echo 1
echo "$line" >> /home/fawzy/check_by_hand.txt
fi
Where $multipleMatches="possible"
How do I account for this, and pass a value to the command being run, so that my script doesn't lock up?
bash
bash
New contributor
New contributor
edited 14 mins ago
filbranden
9,56121343
9,56121343
New contributor
asked 19 mins ago
user339234user339234
1
1
New contributor
New contributor
Your problem is that the$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do$(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.
– Stephen Harris
10 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
1
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
And if the tool is well written, you may not even need to clean the output, just drop2>&1
.
– Kamil Maciorowski
4 mins ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago
add a comment |
Your problem is that the$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do$(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.
– Stephen Harris
10 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
1
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
And if the tool is well written, you may not even need to clean the output, just drop2>&1
.
– Kamil Maciorowski
4 mins ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago
Your problem is that the
$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do $(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.– Stephen Harris
10 mins ago
Your problem is that the
$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do $(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.– Stephen Harris
10 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
1
1
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
And if the tool is well written, you may not even need to clean the output, just drop
2>&1
.– Kamil Maciorowski
4 mins ago
And if the tool is well written, you may not even need to clean the output, just drop
2>&1
.– Kamil Maciorowski
4 mins ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago
add a comment |
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
user339234 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f503472%2fpassing-a-number-to-a-command-run-thru-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
user339234 is a new contributor. Be nice, and check out our Code of Conduct.
user339234 is a new contributor. Be nice, and check out our Code of Conduct.
user339234 is a new contributor. Be nice, and check out our Code of Conduct.
user339234 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f503472%2fpassing-a-number-to-a-command-run-thru-bash%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
Your problem is that the
$(...)
won't return until input is sent. So if you always want to select option 1 then you might be able to do$(echo 1 | COMMAND ....)
. If this is ignored when there's no ambiguity then you will just need to clean up the output.– Stephen Harris
10 mins ago
The issue is that input isn't always required. This is an edge case for this command.
– user339234
7 mins ago
1
If the input is ignored when it's not required, then it doesn't hurt to send it :-)
– Stephen Harris
7 mins ago
And if the tool is well written, you may not even need to clean the output, just drop
2>&1
.– Kamil Maciorowski
4 mins ago
Ok, this works to get passed the lock-up. But the other issue is now that this part of the script doesn't work properly: elif [[ "$response" == "multipleMatches" ]] then echo "$line" >> /home/fawzy/check_by_hand.txt How would I get these edge cases to write to check_by_hand.txt?
– user339234
1 min ago