POSIX and Portability | shell scripts | grep -s, grep -qHow can I test for POSIX compliance of shell...
Multi tool use
Formal Definition of Dot Product
Why was my Canon Speedlite 600EX triggering other flashes?
Do not cross the line!
Use of さ as a filler
Do Grothendieck universes matter for an algebraic geometer?
complicated arrows in flowcharts
Adding labels and comments to a matrix
Should I communicate in my applications that I'm unemployed out of choice rather than because nobody will have me?
Is it wrong to omit object pronouns in these sentences?
How to disable Two-factor authentication for Apple ID?
Given 0s on Assignments with suspected and dismissed cheating?
Uh oh, the propeller fell off
Will casting a card from the graveyard with Flashback add a quest counter on Pyromancer Ascension?
Is Valonqar prophecy unfulfilled?
"The van's really booking"
Is this possible when it comes to the relations of P, NP, NP-Hard and NP-Complete?
Why does the headset man not get on the tractor?
Was the dragon prowess intentionally downplayed in S08E04?
Can I say: "When was your train leaving?" if the train leaves in the future?
How to not get blinded by an attack at dawn
Why is it harder to turn a motor/generator with shorted terminals?
Did galley captains put corks in the mouths of slave rowers to keep them quiet?
Is 12 minutes connection in Bristol Temple Meads long enough?
Automation Engine activity type not retrieving custom facet
POSIX and Portability | shell scripts | grep -s, grep -q
How can I test for POSIX compliance of shell scripts?POSIX test and -aCommon flag designations and standards for shell scripts and functionsSingle or double brackets and portabilityPOSIX shell scripting and performance tuningPortable POSIX shell alternative to GNU seq(1)?How to grep a program's output but also echo the output normally?Do progress reports/logging information belong on stderr or stdout?What does POSIX require for quoted here documents inside command substitution?Can I read a single character from stdin in POSIX shell?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation
, which accepts the very first argument as a string containing a question, and all of the other arguments are to be possible valid answers:
confirmation ()
{
question=$1; shift; correct_answers=$*
printf '%b' "$question\nPlease answer [ $( printf '%s' "$correct_answers" | tr ' ' / ) ] to confirm (Not <Enter>): "; read -r user_answer
# this part iterates through the list of correct answers
# and compares each as the whole word (actually as the whole line) with the user answer
for single_correct_answer in $correct_answers; do
printf '%s' "$single_correct_answer" | grep -i -x "$user_answer" > /dev/null 2>&1 && return 0
done
return 1
}
confirmation 'Do you hate me?' yes yeah kinda
As you can see, the core part is utilizing grep
, so I looked into the man page, and found this:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX .)
-s, --no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX , because it lacked -q and its -s option behaved like GNU grep's -q option. USG -style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX .)
Let's stress this part:
Portability note: unlike GNU
grep
, 7th Edition Unixgrep
did not conform to POSIX, because it lacked-q
and its-s
option behaved like GNUgrep
's-q
option. USG-stylegrep
also lacked-q
but its-s
option behaved like GNUgrep
. Portable shell scripts should avoid both-q
and-s
and should redirect standard and error output to/dev/null
instead.
Am I overdoing it already, or is the redirection to /dev/null
the only portable way?
I am not concerned about portability to an operating system version from forty years ago!
shell-script grep scripting posix portability
add a comment |
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation
, which accepts the very first argument as a string containing a question, and all of the other arguments are to be possible valid answers:
confirmation ()
{
question=$1; shift; correct_answers=$*
printf '%b' "$question\nPlease answer [ $( printf '%s' "$correct_answers" | tr ' ' / ) ] to confirm (Not <Enter>): "; read -r user_answer
# this part iterates through the list of correct answers
# and compares each as the whole word (actually as the whole line) with the user answer
for single_correct_answer in $correct_answers; do
printf '%s' "$single_correct_answer" | grep -i -x "$user_answer" > /dev/null 2>&1 && return 0
done
return 1
}
confirmation 'Do you hate me?' yes yeah kinda
As you can see, the core part is utilizing grep
, so I looked into the man page, and found this:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX .)
-s, --no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX , because it lacked -q and its -s option behaved like GNU grep's -q option. USG -style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX .)
Let's stress this part:
Portability note: unlike GNU
grep
, 7th Edition Unixgrep
did not conform to POSIX, because it lacked-q
and its-s
option behaved like GNUgrep
's-q
option. USG-stylegrep
also lacked-q
but its-s
option behaved like GNUgrep
. Portable shell scripts should avoid both-q
and-s
and should redirect standard and error output to/dev/null
instead.
Am I overdoing it already, or is the redirection to /dev/null
the only portable way?
I am not concerned about portability to an operating system version from forty years ago!
shell-script grep scripting posix portability
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
@MichaelHomer added
– Vlastimil
30 mins ago
add a comment |
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation
, which accepts the very first argument as a string containing a question, and all of the other arguments are to be possible valid answers:
confirmation ()
{
question=$1; shift; correct_answers=$*
printf '%b' "$question\nPlease answer [ $( printf '%s' "$correct_answers" | tr ' ' / ) ] to confirm (Not <Enter>): "; read -r user_answer
# this part iterates through the list of correct answers
# and compares each as the whole word (actually as the whole line) with the user answer
for single_correct_answer in $correct_answers; do
printf '%s' "$single_correct_answer" | grep -i -x "$user_answer" > /dev/null 2>&1 && return 0
done
return 1
}
confirmation 'Do you hate me?' yes yeah kinda
As you can see, the core part is utilizing grep
, so I looked into the man page, and found this:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX .)
-s, --no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX , because it lacked -q and its -s option behaved like GNU grep's -q option. USG -style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX .)
Let's stress this part:
Portability note: unlike GNU
grep
, 7th Edition Unixgrep
did not conform to POSIX, because it lacked-q
and its-s
option behaved like GNUgrep
's-q
option. USG-stylegrep
also lacked-q
but its-s
option behaved like GNUgrep
. Portable shell scripts should avoid both-q
and-s
and should redirect standard and error output to/dev/null
instead.
Am I overdoing it already, or is the redirection to /dev/null
the only portable way?
I am not concerned about portability to an operating system version from forty years ago!
shell-script grep scripting posix portability
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation
, which accepts the very first argument as a string containing a question, and all of the other arguments are to be possible valid answers:
confirmation ()
{
question=$1; shift; correct_answers=$*
printf '%b' "$question\nPlease answer [ $( printf '%s' "$correct_answers" | tr ' ' / ) ] to confirm (Not <Enter>): "; read -r user_answer
# this part iterates through the list of correct answers
# and compares each as the whole word (actually as the whole line) with the user answer
for single_correct_answer in $correct_answers; do
printf '%s' "$single_correct_answer" | grep -i -x "$user_answer" > /dev/null 2>&1 && return 0
done
return 1
}
confirmation 'Do you hate me?' yes yeah kinda
As you can see, the core part is utilizing grep
, so I looked into the man page, and found this:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX .)
-s, --no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX , because it lacked -q and its -s option behaved like GNU grep's -q option. USG -style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX .)
Let's stress this part:
Portability note: unlike GNU
grep
, 7th Edition Unixgrep
did not conform to POSIX, because it lacked-q
and its-s
option behaved like GNUgrep
's-q
option. USG-stylegrep
also lacked-q
but its-s
option behaved like GNUgrep
. Portable shell scripts should avoid both-q
and-s
and should redirect standard and error output to/dev/null
instead.
Am I overdoing it already, or is the redirection to /dev/null
the only portable way?
I am not concerned about portability to an operating system version from forty years ago!
shell-script grep scripting posix portability
shell-script grep scripting posix portability
edited 30 mins ago
Vlastimil
asked 2 hours ago
VlastimilVlastimil
8,7691768153
8,7691768153
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
@MichaelHomer added
– Vlastimil
30 mins ago
add a comment |
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
@MichaelHomer added
– Vlastimil
30 mins ago
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
@MichaelHomer added
– Vlastimil
30 mins ago
@MichaelHomer added
– Vlastimil
30 mins 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
});
}
});
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%2f518795%2fposix-and-portability-shell-scripts-grep-s-grep-q%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
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%2f518795%2fposix-and-portability-shell-scripts-grep-s-grep-q%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
jeW0Qln2hLq0t0fTjFgmomw0Cg2tTTOOoMmKJR
Are you concerned about portability to an operating system version from forty years ago? That information is necessary and sufficient to answer your question, but you haven't provided it...
– Michael Homer
1 hour ago
@MichaelHomer added
– Vlastimil
30 mins ago