Posted on 02-03-2020 03:55 AM
Hi all.
If $currentuser is not (user1 or user2 or user3),
then...
what would be the correct syntax for this one? I struggle hard, but cannot get this to work properly (the $currentUser variable itself works fine)
Thanks for any advise.
Best regards
Christian
Posted on 02-03-2020 03:56 AM
Aim is that script should exclude user1-3 from executing...
Posted on 02-03-2020 05:30 AM
if [ $currentuser != 'user1' -a $currentuser != 'user2' -a $currentuser != 'user3' ]; then echo Fine; fi
or
if [ $currentuser == 'user1' -o $currentuser == 'user2' -a $currentuser == 'user3' ]; then echo Sorry; fi
Posted on 02-03-2020 05:53 AM
Not 100% sure what the intent is on this. If I understood correctly, and you have something that you want to run for the currently logged in user unless it is one of a few users, this ought to work pretty well. You can use a pre-defined array of skippable users and a for loop to compare each to the current user. This scales well since you can just continually add or remove users to the array if/whenever needed. Also, you can wrap it in a function for easy portability across multiple scripts.
#!/bin/bash
#---FUNCTIONS---------------------------------------------------------------------------
# Gets the current console user
getConsoleUsername()
{
/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser
user = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
user = [user,""][user in [u"loginwindow", None, u""]]
print(user)'
}
# Skips users in a predefined array of skippable users
isSkippable()
{
local _skip_users _console_user _skip
# Define an array of all users that should be skipped
_skip_users=("user1" "user2" "user3")
# Get the current console user
_console_user="$(getConsoleUsername)"
# Loop through the array of skippable users
for _skip in "${_skip_users[@]}"; do
# Return success (0) (i.e. the user is skippable) if the console
# user matches any user in the array of skippable users
[ "$_console_user" = "$_skip" ] && return 0
done
# Return failure (1) (i.e. user is not skippable) if
# the user is not in the array of skippable users
return 1
}
#---START SCRIPT------------------------------------------------------------------------
# Check if user shold be skipped
if isSkippable; then
# If the user is skippable, report and exit
echo "User is being skipped"
else
######################################################
# If the user is not skippable, do the main thing here
######################################################
fi
exit 0