Reputation: 10429
I am trying to write a script where at the beginning it only runs if the user is one of the following: Peter, Paul, Simon:
I do:
if [[ "$(whoami)" != peter && "$(whoami)" != paul && "$(whoami)" != simon]]; then
echo -e "\nPlease run this script as someone who is allowed" >&2
exit 1
fi
I get:
syntax error in conditional expression: unexpected token `;'
Any tips? Or better still, a better way to do this?
Thanks
Upvotes: 0
Views: 78
Reputation: 10663
konsolebox's answer is correct about space character before ]]
.
But if you don't want to mess with extended globs you can use regexp:
if [[ $(whoami) =~ ^(peter|paul|simon)$ ]]; then
Or even
if [[ $USER =~ ^(peter|paul|simon)$ ]]; then
Upvotes: 3
Reputation: 75548
You forgot to add a space at the end:
... != simon ]]
Also if you could consider using extended globs, you could have it simpler like this:
shopt -s extglob
if [[ $(whoami) != @(peter|paul|simon) ]]; then
And a little old-fashioned but POSIX compatible way:
case $(whoami) in
peter|paul|simon)
;;
*)
echo -e "\nPlease run this script as someone who is allowed" >&2
exit 1
;;
esac
Upvotes: 6