Shorten if statement check for matching argument
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have this bit of code which does what it's supposed to do:
first_arg="$1";
if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
exit 1;
fi
it's checking ensure that the first argument is one of (patch, major, minor, prerelease).
However, is there a shorter / less-verbose way of doing this?
bash shell-script test
add a comment |Â
up vote
0
down vote
favorite
I have this bit of code which does what it's supposed to do:
first_arg="$1";
if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
exit 1;
fi
it's checking ensure that the first argument is one of (patch, major, minor, prerelease).
However, is there a shorter / less-verbose way of doing this?
bash shell-script test
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this bit of code which does what it's supposed to do:
first_arg="$1";
if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
exit 1;
fi
it's checking ensure that the first argument is one of (patch, major, minor, prerelease).
However, is there a shorter / less-verbose way of doing this?
bash shell-script test
I have this bit of code which does what it's supposed to do:
first_arg="$1";
if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
exit 1;
fi
it's checking ensure that the first argument is one of (patch, major, minor, prerelease).
However, is there a shorter / less-verbose way of doing this?
bash shell-script test
asked Jun 14 at 6:24
Alexander Mills
1,873929
1,873929
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
You can use case
, which works in any standard shell:
case "$1" in
patch|major|minor|prerelease) ;;
*) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
esac
An alternative in Bash/ksh/zsh is to use a regular expression match:
if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
exit 1;
fi
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the[[ .... ]]
construct in bash/zsh/ksh.
â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like"$1" =~ ^regex$
won't hurt though.
â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You can use case
, which works in any standard shell:
case "$1" in
patch|major|minor|prerelease) ;;
*) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
esac
An alternative in Bash/ksh/zsh is to use a regular expression match:
if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
exit 1;
fi
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the[[ .... ]]
construct in bash/zsh/ksh.
â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like"$1" =~ ^regex$
won't hurt though.
â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
add a comment |Â
up vote
6
down vote
accepted
You can use case
, which works in any standard shell:
case "$1" in
patch|major|minor|prerelease) ;;
*) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
esac
An alternative in Bash/ksh/zsh is to use a regular expression match:
if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
exit 1;
fi
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the[[ .... ]]
construct in bash/zsh/ksh.
â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like"$1" =~ ^regex$
won't hurt though.
â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You can use case
, which works in any standard shell:
case "$1" in
patch|major|minor|prerelease) ;;
*) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
esac
An alternative in Bash/ksh/zsh is to use a regular expression match:
if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
exit 1;
fi
You can use case
, which works in any standard shell:
case "$1" in
patch|major|minor|prerelease) ;;
*) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
esac
An alternative in Bash/ksh/zsh is to use a regular expression match:
if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
exit 1;
fi
edited Jun 15 at 11:57
answered Jun 14 at 6:34
Stephen Kitt
139k22301363
139k22301363
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the[[ .... ]]
construct in bash/zsh/ksh.
â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like"$1" =~ ^regex$
won't hurt though.
â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
add a comment |Â
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the[[ .... ]]
construct in bash/zsh/ksh.
â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like"$1" =~ ^regex$
won't hurt though.
â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
that is the most bizarre syntax ever lol
â Alexander Mills
Jun 14 at 6:48
@StephenKitt: Just a side note: You don't need to quote $1 inside the
[[ .... ]]
construct in bash/zsh/ksh.â user1934428
Jun 15 at 7:17
@StephenKitt: Just a side note: You don't need to quote $1 inside the
[[ .... ]]
construct in bash/zsh/ksh.â user1934428
Jun 15 at 7:17
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
@StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
â user1934428
Jun 15 at 11:38
I assume using quotes like
"$1" =~ ^regex$
won't hurt though.â Alexander Mills
Jun 16 at 22:52
I assume using quotes like
"$1" =~ ^regex$
won't hurt though.â Alexander Mills
Jun 16 at 22:52
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
@AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
â user1934428
Jun 18 at 6:59
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449734%2fshorten-if-statement-check-for-matching-argument%23new-answer', 'question_page');
);
Post as a guest
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
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
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