What does the slash do in 'case $1/$2' in shell script (/bin/sh)
Found this example on the arch wiki
#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac
and can't make out if it's a logical operator or something else.
shell scripting slash
add a comment |
Found this example on the arch wiki
#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac
and can't make out if it's a logical operator or something else.
shell scripting slash
it matches the/
inpre/*
andpost/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04
add a comment |
Found this example on the arch wiki
#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac
and can't make out if it's a logical operator or something else.
shell scripting slash
Found this example on the arch wiki
#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac
and can't make out if it's a logical operator or something else.
shell scripting slash
shell scripting slash
edited Dec 22 '18 at 17:45
ctrl-alt-delor
11.8k42260
11.8k42260
asked Dec 22 '18 at 16:22
James LanghamJames Langham
182
182
it matches the/
inpre/*
andpost/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04
add a comment |
it matches the/
inpre/*
andpost/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04
it matches the
/
in pre/*
and post/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
it matches the
/
in pre/*
and post/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04
add a comment |
1 Answer
1
active
oldest
votes
There is nothing special here. The syntax for case
is
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
In the example in the question, word
is built by combining $1
, /
and $2
. /
doesn't have a special meaning at all, it's just a character.
PS: Practically the usage is a bit strange, as the $2
part afterwards gets matched against *
so the value of $2
doesn't really matter. One could, in the context described in the Wiki, also write
#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac
But there might be cases where suspend
, hibernate
or hybrid
(the possible values for $2
) are relevant, so it's just a general pattern here.
3
maybe$1
ispre/heat
and$2
isoven
in which case your code would not match, but the original would ;-)
– mosvy
Dec 22 '18 at 17:19
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where$1
ispre/something
.
– Stéphane Chazelas
Dec 22 '18 at 17:58
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%2f490491%2fwhat-does-the-slash-do-in-case-1-2-in-shell-script-bin-sh%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
There is nothing special here. The syntax for case
is
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
In the example in the question, word
is built by combining $1
, /
and $2
. /
doesn't have a special meaning at all, it's just a character.
PS: Practically the usage is a bit strange, as the $2
part afterwards gets matched against *
so the value of $2
doesn't really matter. One could, in the context described in the Wiki, also write
#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac
But there might be cases where suspend
, hibernate
or hybrid
(the possible values for $2
) are relevant, so it's just a general pattern here.
3
maybe$1
ispre/heat
and$2
isoven
in which case your code would not match, but the original would ;-)
– mosvy
Dec 22 '18 at 17:19
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where$1
ispre/something
.
– Stéphane Chazelas
Dec 22 '18 at 17:58
add a comment |
There is nothing special here. The syntax for case
is
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
In the example in the question, word
is built by combining $1
, /
and $2
. /
doesn't have a special meaning at all, it's just a character.
PS: Practically the usage is a bit strange, as the $2
part afterwards gets matched against *
so the value of $2
doesn't really matter. One could, in the context described in the Wiki, also write
#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac
But there might be cases where suspend
, hibernate
or hybrid
(the possible values for $2
) are relevant, so it's just a general pattern here.
3
maybe$1
ispre/heat
and$2
isoven
in which case your code would not match, but the original would ;-)
– mosvy
Dec 22 '18 at 17:19
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where$1
ispre/something
.
– Stéphane Chazelas
Dec 22 '18 at 17:58
add a comment |
There is nothing special here. The syntax for case
is
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
In the example in the question, word
is built by combining $1
, /
and $2
. /
doesn't have a special meaning at all, it's just a character.
PS: Practically the usage is a bit strange, as the $2
part afterwards gets matched against *
so the value of $2
doesn't really matter. One could, in the context described in the Wiki, also write
#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac
But there might be cases where suspend
, hibernate
or hybrid
(the possible values for $2
) are relevant, so it's just a general pattern here.
There is nothing special here. The syntax for case
is
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
In the example in the question, word
is built by combining $1
, /
and $2
. /
doesn't have a special meaning at all, it's just a character.
PS: Practically the usage is a bit strange, as the $2
part afterwards gets matched against *
so the value of $2
doesn't really matter. One could, in the context described in the Wiki, also write
#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac
But there might be cases where suspend
, hibernate
or hybrid
(the possible values for $2
) are relevant, so it's just a general pattern here.
edited Dec 22 '18 at 17:41
answered Dec 22 '18 at 16:27
nohillsidenohillside
2,3901019
2,3901019
3
maybe$1
ispre/heat
and$2
isoven
in which case your code would not match, but the original would ;-)
– mosvy
Dec 22 '18 at 17:19
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where$1
ispre/something
.
– Stéphane Chazelas
Dec 22 '18 at 17:58
add a comment |
3
maybe$1
ispre/heat
and$2
isoven
in which case your code would not match, but the original would ;-)
– mosvy
Dec 22 '18 at 17:19
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where$1
ispre/something
.
– Stéphane Chazelas
Dec 22 '18 at 17:58
3
3
maybe
$1
is pre/heat
and $2
is oven
in which case your code would not match, but the original would ;-)– mosvy
Dec 22 '18 at 17:19
maybe
$1
is pre/heat
and $2
is oven
in which case your code would not match, but the original would ;-)– mosvy
Dec 22 '18 at 17:19
1
1
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.
– nohillside
Dec 22 '18 at 17:23
They would not be equivalent in cases where
$1
is pre/something
.– Stéphane Chazelas
Dec 22 '18 at 17:58
They would not be equivalent in cases where
$1
is pre/something
.– Stéphane Chazelas
Dec 22 '18 at 17:58
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%2f490491%2fwhat-does-the-slash-do-in-case-1-2-in-shell-script-bin-sh%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
it matches the
/
inpre/*
andpost/*
– ctrl-alt-delor
Dec 22 '18 at 17:46
It is a trick to test $1 and $2 at the same time.
– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04