Checking if string ends with a number throws “unexpected operator”echo string >> file does not...
Chandrayaan 2: Why is Vikram Lander's life limited to 14 Days?
Owner keeps cutting corners and poaching workers for his other company
Why would an airport be depicted with symbology for runways longer than 8,069 feet even though it is reported on the sectional as 7,200 feet?
Capacitors with same voltage, same capacitance, same temp, different diameter?
How to set any file manager in Linux to show the duration like the Length feature in Windows Explorer?
The meaning of "offing" in "an agreement in the offing"
Are personality traits, ideals, bonds, and flaws required?
What can we do about our 9-month-old putting fingers down his throat?
Methods and Feasibility of Antimatter Mining?
How can I finish my PhD?
How can I protect myself in case of attack in case like this?
The pirate treasure of Leatherback Atoll
Gap in tcolorbox after title
If every star in the universe except the Sun were destroyed, would we die?
What is the difference between a translation and a Galilean transformation?
Why does low tire pressure decrease fuel economy?
Who is the uncredited actor leading the squad in the Valerian movie?
How do I reference a custom counter that shows the section number?
What makes an ending "happy"?
Contractor cut joist hangers to make them fit
Is there a specific way to describe over-grown, old, tough vegetables?
pgfgantt: month displayed as single letter
How to find a reviewer/editor for my paper?
Is there a "right" way to interpret a novel, if not, how do we make sure our novel is interpreted correctly?
Checking if string ends with a number throws “unexpected operator”
echo string >> file does not workGrep a string with spaces from a fileRunning .sh Script With Sudo Results In Different OutputTrying to exit script with a status code but getting “unexpected end of file”bin/sh script fails with syntax error: unexpected redirectionWhile Loop over a File returning command not foundConfused why script does not exit
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm trying to check wether the file name of the script I run ends with a number or not:
#!/bin/sh
name=$(basename "$0" .sh)
[ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $numb
my shell file is named mh03.sh
and this is the output if I run it:
$ ./mh3.sh
./mh3.sh: 3: [: mh3: unexpected operator
1
can someone tell me why I get this exception and how I can fix it?
bash regular-expression
add a comment |
I'm trying to check wether the file name of the script I run ends with a number or not:
#!/bin/sh
name=$(basename "$0" .sh)
[ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $numb
my shell file is named mh03.sh
and this is the output if I run it:
$ ./mh3.sh
./mh3.sh: 3: [: mh3: unexpected operator
1
can someone tell me why I get this exception and how I can fix it?
bash regular-expression
add a comment |
I'm trying to check wether the file name of the script I run ends with a number or not:
#!/bin/sh
name=$(basename "$0" .sh)
[ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $numb
my shell file is named mh03.sh
and this is the output if I run it:
$ ./mh3.sh
./mh3.sh: 3: [: mh3: unexpected operator
1
can someone tell me why I get this exception and how I can fix it?
bash regular-expression
I'm trying to check wether the file name of the script I run ends with a number or not:
#!/bin/sh
name=$(basename "$0" .sh)
[ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $numb
my shell file is named mh03.sh
and this is the output if I run it:
$ ./mh3.sh
./mh3.sh: 3: [: mh3: unexpected operator
1
can someone tell me why I get this exception and how I can fix it?
bash regular-expression
bash regular-expression
asked 40 mins ago
Nicola UetzNicola Uetz
1155 bronze badges
1155 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The regex match operator =~
is not supported in the single square brackets. You need double square brackets for it to work.
[[ $name =~ ^.[0-9]$ ]]
Note that you don't need a regex, you can use a normal pattern:
[[ $name = *[0-9] ]]
or, if you need the name to contain something before the digit,
[[ $name = *?[0-9] ]]
Oh good to know! But now it gives me the following exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use#!/bin/bash
instead of#!/bin/sh
... I just wonder why.
– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use#!/bin/bash
.
– Stephen Harris
5 mins ago
add a comment |
[: =~: binary operator expected
the error is [$name =~ ^.[0-9]$ ]
encolse it with [],
try,
#!/bin/sh
name=$(basename "$0" .sh)
[[ $name =~ ^.[0-9]$ ]] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $num
and it works.
I don't know which one of you were first. But either way it just prints me a new exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
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/4.0/"u003ecc by-sa 4.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%2f539698%2fchecking-if-string-ends-with-a-number-throws-unexpected-operator%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
The regex match operator =~
is not supported in the single square brackets. You need double square brackets for it to work.
[[ $name =~ ^.[0-9]$ ]]
Note that you don't need a regex, you can use a normal pattern:
[[ $name = *[0-9] ]]
or, if you need the name to contain something before the digit,
[[ $name = *?[0-9] ]]
Oh good to know! But now it gives me the following exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use#!/bin/bash
instead of#!/bin/sh
... I just wonder why.
– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use#!/bin/bash
.
– Stephen Harris
5 mins ago
add a comment |
The regex match operator =~
is not supported in the single square brackets. You need double square brackets for it to work.
[[ $name =~ ^.[0-9]$ ]]
Note that you don't need a regex, you can use a normal pattern:
[[ $name = *[0-9] ]]
or, if you need the name to contain something before the digit,
[[ $name = *?[0-9] ]]
Oh good to know! But now it gives me the following exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use#!/bin/bash
instead of#!/bin/sh
... I just wonder why.
– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use#!/bin/bash
.
– Stephen Harris
5 mins ago
add a comment |
The regex match operator =~
is not supported in the single square brackets. You need double square brackets for it to work.
[[ $name =~ ^.[0-9]$ ]]
Note that you don't need a regex, you can use a normal pattern:
[[ $name = *[0-9] ]]
or, if you need the name to contain something before the digit,
[[ $name = *?[0-9] ]]
The regex match operator =~
is not supported in the single square brackets. You need double square brackets for it to work.
[[ $name =~ ^.[0-9]$ ]]
Note that you don't need a regex, you can use a normal pattern:
[[ $name = *[0-9] ]]
or, if you need the name to contain something before the digit,
[[ $name = *?[0-9] ]]
answered 31 mins ago
chorobachoroba
29.3k4 gold badges57 silver badges81 bronze badges
29.3k4 gold badges57 silver badges81 bronze badges
Oh good to know! But now it gives me the following exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use#!/bin/bash
instead of#!/bin/sh
... I just wonder why.
– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use#!/bin/bash
.
– Stephen Harris
5 mins ago
add a comment |
Oh good to know! But now it gives me the following exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use#!/bin/bash
instead of#!/bin/sh
... I just wonder why.
– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use#!/bin/bash
.
– Stephen Harris
5 mins ago
Oh good to know! But now it gives me the following exception:
./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
Oh good to know! But now it gives me the following exception:
./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
26 mins ago
I found the second issue by myself... Appearently I have to use
#!/bin/bash
instead of #!/bin/sh
... I just wonder why.– Nicola Uetz
19 mins ago
I found the second issue by myself... Appearently I have to use
#!/bin/bash
instead of #!/bin/sh
... I just wonder why.– Nicola Uetz
19 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use #!/bin/bash
.– Stephen Harris
5 mins ago
/bin/sh
is not the same as "bash". If you want to use bash semantics then use #!/bin/bash
.– Stephen Harris
5 mins ago
add a comment |
[: =~: binary operator expected
the error is [$name =~ ^.[0-9]$ ]
encolse it with [],
try,
#!/bin/sh
name=$(basename "$0" .sh)
[[ $name =~ ^.[0-9]$ ]] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $num
and it works.
I don't know which one of you were first. But either way it just prints me a new exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
add a comment |
[: =~: binary operator expected
the error is [$name =~ ^.[0-9]$ ]
encolse it with [],
try,
#!/bin/sh
name=$(basename "$0" .sh)
[[ $name =~ ^.[0-9]$ ]] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $num
and it works.
I don't know which one of you were first. But either way it just prints me a new exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
add a comment |
[: =~: binary operator expected
the error is [$name =~ ^.[0-9]$ ]
encolse it with [],
try,
#!/bin/sh
name=$(basename "$0" .sh)
[[ $name =~ ^.[0-9]$ ]] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $num
and it works.
[: =~: binary operator expected
the error is [$name =~ ^.[0-9]$ ]
encolse it with [],
try,
#!/bin/sh
name=$(basename "$0" .sh)
[[ $name =~ ^.[0-9]$ ]] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1
echo $num
and it works.
answered 30 mins ago
Cosmo ArunCosmo Arun
414 bronze badges
414 bronze badges
I don't know which one of you were first. But either way it just prints me a new exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
add a comment |
I don't know which one of you were first. But either way it just prints me a new exception:./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
I don't know which one of you were first. But either way it just prints me a new exception:
./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
I don't know which one of you were first. But either way it just prints me a new exception:
./mh3.sh: 3: ./mh3.sh: [[: not found
– Nicola Uetz
25 mins ago
1
1
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
I found the second issue by myself... Appearently I have to use #!/bin/bash instead of #!/bin/sh... I just wonder why.
– Nicola Uetz
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
i guess you /bin/bash is not really bash, try typing this and execute, /bin/bash -c "type [["
– Cosmo Arun
19 mins ago
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%2f539698%2fchecking-if-string-ends-with-a-number-throws-unexpected-operator%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