Or operator between two commands fails?
I am trying to add a usage statement for users don't run this bash script with any filenames. I thought I could use the "OR" operator. But the script still failed.
#! /bin/bash
file="$1"
sort -k3,3 -k2,2 -k1 "$1" | awk '{ sum=0; for(i=4; i<=NF; i++){sum+=$i}; sum/=NF-3; print int(sum),"["$1"]",$3",",$2}' || echo "Usage: GradesAwk.sh filename"
And here is the sample data.
123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
shell-script shell
add a comment |
I am trying to add a usage statement for users don't run this bash script with any filenames. I thought I could use the "OR" operator. But the script still failed.
#! /bin/bash
file="$1"
sort -k3,3 -k2,2 -k1 "$1" | awk '{ sum=0; for(i=4; i<=NF; i++){sum+=$i}; sum/=NF-3; print int(sum),"["$1"]",$3",",$2}' || echo "Usage: GradesAwk.sh filename"
And here is the sample data.
123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
shell-script shell
4
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
1
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54
add a comment |
I am trying to add a usage statement for users don't run this bash script with any filenames. I thought I could use the "OR" operator. But the script still failed.
#! /bin/bash
file="$1"
sort -k3,3 -k2,2 -k1 "$1" | awk '{ sum=0; for(i=4; i<=NF; i++){sum+=$i}; sum/=NF-3; print int(sum),"["$1"]",$3",",$2}' || echo "Usage: GradesAwk.sh filename"
And here is the sample data.
123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
shell-script shell
I am trying to add a usage statement for users don't run this bash script with any filenames. I thought I could use the "OR" operator. But the script still failed.
#! /bin/bash
file="$1"
sort -k3,3 -k2,2 -k1 "$1" | awk '{ sum=0; for(i=4; i<=NF; i++){sum+=$i}; sum/=NF-3; print int(sum),"["$1"]",$3",",$2}' || echo "Usage: GradesAwk.sh filename"
And here is the sample data.
123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
shell-script shell
shell-script shell
edited Dec 16 at 22:07
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Sep 16 '17 at 22:54
user251505
41
41
4
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
1
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54
add a comment |
4
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
1
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54
4
4
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
1
1
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54
add a comment |
2 Answers
2
active
oldest
votes
You want to do your validation as early as possible:
#!/bin/bash
file="$1"
if [[ -z "$file" ]]; then
echo "Error: no file provided." >&2
echo "usage: $0 file" >&2
exit 1
elif [[ ! -f "$file" ]]; then
echo "Error: no such file '$file'" >&2
exit 2
fi
sort ... "$file" | awk ...
add a comment |
What you get is typical bash behavior, due to pipefail option (disabled by default).
Bash manual refers:
The return status of a pipeline is the exit status of the last
command, unless the pipefail option is enabled.If pipefail is enabled,
the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully.
In your case, the sort command fails, but the awk command does not fail;it just receives no input and this is not a failure for awk.
As a result your ||
branch is not executed since with pipefail disabled (default) the last pipeline command (awk) was successfull.
All you need is to include set -o pipefail
in your script to enable pipefail.
Check this test out:
$ ls file7*
file7
$ sort file7 |awk '{print $1}' || echo "error"
abc
four
klm
$ sort file77 |awk '{print $1}' || echo "error"
sort: cannot read: file77: No such file or directory
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
$ set -o pipefail
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
error
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying2>/dev/null
at the end of sort command, like my test.
– George Vasiliou
Sep 17 '17 at 0:10
add a comment |
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%2f392692%2for-operator-between-two-commands-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want to do your validation as early as possible:
#!/bin/bash
file="$1"
if [[ -z "$file" ]]; then
echo "Error: no file provided." >&2
echo "usage: $0 file" >&2
exit 1
elif [[ ! -f "$file" ]]; then
echo "Error: no such file '$file'" >&2
exit 2
fi
sort ... "$file" | awk ...
add a comment |
You want to do your validation as early as possible:
#!/bin/bash
file="$1"
if [[ -z "$file" ]]; then
echo "Error: no file provided." >&2
echo "usage: $0 file" >&2
exit 1
elif [[ ! -f "$file" ]]; then
echo "Error: no such file '$file'" >&2
exit 2
fi
sort ... "$file" | awk ...
add a comment |
You want to do your validation as early as possible:
#!/bin/bash
file="$1"
if [[ -z "$file" ]]; then
echo "Error: no file provided." >&2
echo "usage: $0 file" >&2
exit 1
elif [[ ! -f "$file" ]]; then
echo "Error: no such file '$file'" >&2
exit 2
fi
sort ... "$file" | awk ...
You want to do your validation as early as possible:
#!/bin/bash
file="$1"
if [[ -z "$file" ]]; then
echo "Error: no file provided." >&2
echo "usage: $0 file" >&2
exit 1
elif [[ ! -f "$file" ]]; then
echo "Error: no such file '$file'" >&2
exit 2
fi
sort ... "$file" | awk ...
answered Sep 17 '17 at 0:27
glenn jackman
50.2k570107
50.2k570107
add a comment |
add a comment |
What you get is typical bash behavior, due to pipefail option (disabled by default).
Bash manual refers:
The return status of a pipeline is the exit status of the last
command, unless the pipefail option is enabled.If pipefail is enabled,
the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully.
In your case, the sort command fails, but the awk command does not fail;it just receives no input and this is not a failure for awk.
As a result your ||
branch is not executed since with pipefail disabled (default) the last pipeline command (awk) was successfull.
All you need is to include set -o pipefail
in your script to enable pipefail.
Check this test out:
$ ls file7*
file7
$ sort file7 |awk '{print $1}' || echo "error"
abc
four
klm
$ sort file77 |awk '{print $1}' || echo "error"
sort: cannot read: file77: No such file or directory
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
$ set -o pipefail
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
error
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying2>/dev/null
at the end of sort command, like my test.
– George Vasiliou
Sep 17 '17 at 0:10
add a comment |
What you get is typical bash behavior, due to pipefail option (disabled by default).
Bash manual refers:
The return status of a pipeline is the exit status of the last
command, unless the pipefail option is enabled.If pipefail is enabled,
the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully.
In your case, the sort command fails, but the awk command does not fail;it just receives no input and this is not a failure for awk.
As a result your ||
branch is not executed since with pipefail disabled (default) the last pipeline command (awk) was successfull.
All you need is to include set -o pipefail
in your script to enable pipefail.
Check this test out:
$ ls file7*
file7
$ sort file7 |awk '{print $1}' || echo "error"
abc
four
klm
$ sort file77 |awk '{print $1}' || echo "error"
sort: cannot read: file77: No such file or directory
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
$ set -o pipefail
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
error
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying2>/dev/null
at the end of sort command, like my test.
– George Vasiliou
Sep 17 '17 at 0:10
add a comment |
What you get is typical bash behavior, due to pipefail option (disabled by default).
Bash manual refers:
The return status of a pipeline is the exit status of the last
command, unless the pipefail option is enabled.If pipefail is enabled,
the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully.
In your case, the sort command fails, but the awk command does not fail;it just receives no input and this is not a failure for awk.
As a result your ||
branch is not executed since with pipefail disabled (default) the last pipeline command (awk) was successfull.
All you need is to include set -o pipefail
in your script to enable pipefail.
Check this test out:
$ ls file7*
file7
$ sort file7 |awk '{print $1}' || echo "error"
abc
four
klm
$ sort file77 |awk '{print $1}' || echo "error"
sort: cannot read: file77: No such file or directory
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
$ set -o pipefail
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
error
What you get is typical bash behavior, due to pipefail option (disabled by default).
Bash manual refers:
The return status of a pipeline is the exit status of the last
command, unless the pipefail option is enabled.If pipefail is enabled,
the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully.
In your case, the sort command fails, but the awk command does not fail;it just receives no input and this is not a failure for awk.
As a result your ||
branch is not executed since with pipefail disabled (default) the last pipeline command (awk) was successfull.
All you need is to include set -o pipefail
in your script to enable pipefail.
Check this test out:
$ ls file7*
file7
$ sort file7 |awk '{print $1}' || echo "error"
abc
four
klm
$ sort file77 |awk '{print $1}' || echo "error"
sort: cannot read: file77: No such file or directory
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
$ set -o pipefail
$ sort file77 2>/dev/null |awk '{print $1}' || echo "error"
error
edited Sep 16 '17 at 23:27
answered Sep 16 '17 at 23:22
George Vasiliou
5,59531028
5,59531028
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying2>/dev/null
at the end of sort command, like my test.
– George Vasiliou
Sep 17 '17 at 0:10
add a comment |
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying2>/dev/null
at the end of sort command, like my test.
– George Vasiliou
Sep 17 '17 at 0:10
1
1
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Or better yet, test whether a (exactly one) parameter is specified, and whether it exists (is a file), and whether it is readable, before trying to run a command with the parameter as input.
– Scott
Sep 16 '17 at 23:56
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
Thanks, George! And does the "sort fail warning" will always show up even my script is now runnable and gives output to the echo command?
– user251505
Sep 16 '17 at 23:58
@user251505 you can hide the sort failure message by applying
2>/dev/null
at the end of sort command, like my test.– George Vasiliou
Sep 17 '17 at 0:10
@user251505 you can hide the sort failure message by applying
2>/dev/null
at the end of sort command, like my test.– George Vasiliou
Sep 17 '17 at 0:10
add a comment |
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.
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%2funix.stackexchange.com%2fquestions%2f392692%2for-operator-between-two-commands-fails%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
4
Do you seriously think it is a good idea to offer a screen shot instead of pasting the code into your question?
– Hauke Laging
Sep 16 '17 at 23:08
1
Please paste the code in a a code syntax, not a screenshot.
– JohnDoea
Sep 16 '17 at 23:26
Well, that’s a good start, but your question doesn't really make much sense unless you show (or explain) that it doesn’t give the error message you want it to give when it’s invoked with no arguments. (In this case that’s even more important than the input data, but, as long as you’ve got them in the question, you might as well leave them in.)
– Scott
Sep 16 '17 at 23:54