Bash script to detect empty message string. Getting: too many arguments
I have a bash script where I can pipe in output from another application into the below script. But I think I have an incorrect logic for testing if the message is empty or not. If its empty I want it to do nothing, but if it detects the word "error" in the string, it should run another function.
What am I doing wrong in my logic?
I'm getting too many arguments on the 3rd to the last line, most likely due to the message being empty.
message=$( cat )
if [ -n "${message// /}" ]; then
#execute if the the variable is not empty and contains non space characters
message="``` ${message} ```"
else
#execute if the variable is empty or contains only spaces
message=""
fi
sendX() {
.....
}
if [ -z "$message" ]; then
echo "Please pipe a message to me!"
else
sendX
fi
sendAlert() {
......
}
checkword="error"
echo $message
if [ $message =~ $checkword ]; then <---- Error: too many arguments
sendY
fi
bash shell-script
add a comment |
I have a bash script where I can pipe in output from another application into the below script. But I think I have an incorrect logic for testing if the message is empty or not. If its empty I want it to do nothing, but if it detects the word "error" in the string, it should run another function.
What am I doing wrong in my logic?
I'm getting too many arguments on the 3rd to the last line, most likely due to the message being empty.
message=$( cat )
if [ -n "${message// /}" ]; then
#execute if the the variable is not empty and contains non space characters
message="``` ${message} ```"
else
#execute if the variable is empty or contains only spaces
message=""
fi
sendX() {
.....
}
if [ -z "$message" ]; then
echo "Please pipe a message to me!"
else
sendX
fi
sendAlert() {
......
}
checkword="error"
echo $message
if [ $message =~ $checkword ]; then <---- Error: too many arguments
sendY
fi
bash shell-script
add a comment |
I have a bash script where I can pipe in output from another application into the below script. But I think I have an incorrect logic for testing if the message is empty or not. If its empty I want it to do nothing, but if it detects the word "error" in the string, it should run another function.
What am I doing wrong in my logic?
I'm getting too many arguments on the 3rd to the last line, most likely due to the message being empty.
message=$( cat )
if [ -n "${message// /}" ]; then
#execute if the the variable is not empty and contains non space characters
message="``` ${message} ```"
else
#execute if the variable is empty or contains only spaces
message=""
fi
sendX() {
.....
}
if [ -z "$message" ]; then
echo "Please pipe a message to me!"
else
sendX
fi
sendAlert() {
......
}
checkword="error"
echo $message
if [ $message =~ $checkword ]; then <---- Error: too many arguments
sendY
fi
bash shell-script
I have a bash script where I can pipe in output from another application into the below script. But I think I have an incorrect logic for testing if the message is empty or not. If its empty I want it to do nothing, but if it detects the word "error" in the string, it should run another function.
What am I doing wrong in my logic?
I'm getting too many arguments on the 3rd to the last line, most likely due to the message being empty.
message=$( cat )
if [ -n "${message// /}" ]; then
#execute if the the variable is not empty and contains non space characters
message="``` ${message} ```"
else
#execute if the variable is empty or contains only spaces
message=""
fi
sendX() {
.....
}
if [ -z "$message" ]; then
echo "Please pipe a message to me!"
else
sendX
fi
sendAlert() {
......
}
checkword="error"
echo $message
if [ $message =~ $checkword ]; then <---- Error: too many arguments
sendY
fi
bash shell-script
bash shell-script
asked Feb 13 at 22:36
Patoshi パトシPatoshi パトシ
56341322
56341322
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You're getting a "too many arguments" error since [ ... ]
does not understand the =~
operator (used in bash
for regular expression matching). Since [ ... ]
does not understand the operator, it is treated as a string. You then have [ ... ]
with three strings inside it, and they don't fulfil the semantic requirements for a proper test, which is why bash
errors out at this point.
In bash
, you would use =~
inside of [[ ... ]]
.
However, what I assume you'd like to do in that test is to see whether $message
contains $checkword
as a substring. This could also be done with
[[ "$message" == *"$checkword"* ]] && sendY
or with case ... esac
:
case $message in
*"$checkword"*) sendY
esac
This way you don't have to worry about $checkword
containing characters that may be special in regular expressions.
You also need to double quote $message
in echo $message
, or you may get unexpected output if $message
contains filename globbing characters like *
.
Related:
- Bash - If Syntax confusion
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
- When is double-quoting necessary?
Why is printf better than echo? (because it would be better to useprintf '%sn' "$message"
thanecho "$message"
for user-supplied data)
https://www.shellcheck.net/ (which would have picked up these issues and possibly others, like missing a proper#!
-line)
You could also use this instead of the first operation in the script:
case $message in
*[! ]*) # contains non-space
message='``` '"$message"' ```' ;;
*) # contains nothing or only spaces
message=
esac
Using case ... esac
in both places would make your script (at least the bits that you have shown) portable to any sh
-like shell.
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%2f500510%2fbash-script-to-detect-empty-message-string-getting-too-many-arguments%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
You're getting a "too many arguments" error since [ ... ]
does not understand the =~
operator (used in bash
for regular expression matching). Since [ ... ]
does not understand the operator, it is treated as a string. You then have [ ... ]
with three strings inside it, and they don't fulfil the semantic requirements for a proper test, which is why bash
errors out at this point.
In bash
, you would use =~
inside of [[ ... ]]
.
However, what I assume you'd like to do in that test is to see whether $message
contains $checkword
as a substring. This could also be done with
[[ "$message" == *"$checkword"* ]] && sendY
or with case ... esac
:
case $message in
*"$checkword"*) sendY
esac
This way you don't have to worry about $checkword
containing characters that may be special in regular expressions.
You also need to double quote $message
in echo $message
, or you may get unexpected output if $message
contains filename globbing characters like *
.
Related:
- Bash - If Syntax confusion
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
- When is double-quoting necessary?
Why is printf better than echo? (because it would be better to useprintf '%sn' "$message"
thanecho "$message"
for user-supplied data)
https://www.shellcheck.net/ (which would have picked up these issues and possibly others, like missing a proper#!
-line)
You could also use this instead of the first operation in the script:
case $message in
*[! ]*) # contains non-space
message='``` '"$message"' ```' ;;
*) # contains nothing or only spaces
message=
esac
Using case ... esac
in both places would make your script (at least the bits that you have shown) portable to any sh
-like shell.
add a comment |
You're getting a "too many arguments" error since [ ... ]
does not understand the =~
operator (used in bash
for regular expression matching). Since [ ... ]
does not understand the operator, it is treated as a string. You then have [ ... ]
with three strings inside it, and they don't fulfil the semantic requirements for a proper test, which is why bash
errors out at this point.
In bash
, you would use =~
inside of [[ ... ]]
.
However, what I assume you'd like to do in that test is to see whether $message
contains $checkword
as a substring. This could also be done with
[[ "$message" == *"$checkword"* ]] && sendY
or with case ... esac
:
case $message in
*"$checkword"*) sendY
esac
This way you don't have to worry about $checkword
containing characters that may be special in regular expressions.
You also need to double quote $message
in echo $message
, or you may get unexpected output if $message
contains filename globbing characters like *
.
Related:
- Bash - If Syntax confusion
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
- When is double-quoting necessary?
Why is printf better than echo? (because it would be better to useprintf '%sn' "$message"
thanecho "$message"
for user-supplied data)
https://www.shellcheck.net/ (which would have picked up these issues and possibly others, like missing a proper#!
-line)
You could also use this instead of the first operation in the script:
case $message in
*[! ]*) # contains non-space
message='``` '"$message"' ```' ;;
*) # contains nothing or only spaces
message=
esac
Using case ... esac
in both places would make your script (at least the bits that you have shown) portable to any sh
-like shell.
add a comment |
You're getting a "too many arguments" error since [ ... ]
does not understand the =~
operator (used in bash
for regular expression matching). Since [ ... ]
does not understand the operator, it is treated as a string. You then have [ ... ]
with three strings inside it, and they don't fulfil the semantic requirements for a proper test, which is why bash
errors out at this point.
In bash
, you would use =~
inside of [[ ... ]]
.
However, what I assume you'd like to do in that test is to see whether $message
contains $checkword
as a substring. This could also be done with
[[ "$message" == *"$checkword"* ]] && sendY
or with case ... esac
:
case $message in
*"$checkword"*) sendY
esac
This way you don't have to worry about $checkword
containing characters that may be special in regular expressions.
You also need to double quote $message
in echo $message
, or you may get unexpected output if $message
contains filename globbing characters like *
.
Related:
- Bash - If Syntax confusion
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
- When is double-quoting necessary?
Why is printf better than echo? (because it would be better to useprintf '%sn' "$message"
thanecho "$message"
for user-supplied data)
https://www.shellcheck.net/ (which would have picked up these issues and possibly others, like missing a proper#!
-line)
You could also use this instead of the first operation in the script:
case $message in
*[! ]*) # contains non-space
message='``` '"$message"' ```' ;;
*) # contains nothing or only spaces
message=
esac
Using case ... esac
in both places would make your script (at least the bits that you have shown) portable to any sh
-like shell.
You're getting a "too many arguments" error since [ ... ]
does not understand the =~
operator (used in bash
for regular expression matching). Since [ ... ]
does not understand the operator, it is treated as a string. You then have [ ... ]
with three strings inside it, and they don't fulfil the semantic requirements for a proper test, which is why bash
errors out at this point.
In bash
, you would use =~
inside of [[ ... ]]
.
However, what I assume you'd like to do in that test is to see whether $message
contains $checkword
as a substring. This could also be done with
[[ "$message" == *"$checkword"* ]] && sendY
or with case ... esac
:
case $message in
*"$checkword"*) sendY
esac
This way you don't have to worry about $checkword
containing characters that may be special in regular expressions.
You also need to double quote $message
in echo $message
, or you may get unexpected output if $message
contains filename globbing characters like *
.
Related:
- Bash - If Syntax confusion
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
- When is double-quoting necessary?
Why is printf better than echo? (because it would be better to useprintf '%sn' "$message"
thanecho "$message"
for user-supplied data)
https://www.shellcheck.net/ (which would have picked up these issues and possibly others, like missing a proper#!
-line)
You could also use this instead of the first operation in the script:
case $message in
*[! ]*) # contains non-space
message='``` '"$message"' ```' ;;
*) # contains nothing or only spaces
message=
esac
Using case ... esac
in both places would make your script (at least the bits that you have shown) portable to any sh
-like shell.
edited Feb 14 at 10:20
answered Feb 13 at 23:13
KusalanandaKusalananda
135k17255423
135k17255423
add a comment |
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.
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%2f500510%2fbash-script-to-detect-empty-message-string-getting-too-many-arguments%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