Comparing Variable to Wildcard in Shell Script

johnnasset
Contributor

Pretty basic question about shell scripting. I'm writing a script to check the current logged in user and delete the dock.plist in that users folder if the current user is a generic library account. Our generic accounts all end in lib, such as:

bolib
celib
hmlib
crlib

Here is my script, although I'm always getting the else outcome:

#!/bin/bash

##This script will delete the com.apple.dock.plist on logout for generic
##library accounts.  A new default dock will be loaded on the next login.

##First, we need the currently logged in user

currentuser=`who | grep console | awk '{print $1}'`

##If the current user is a library account, delete the com.apple.dock.plist

if [ "$currentuser" == *"lib" ]; then
rm /Users/$currentuser/Library/Preferences/com.apple.dock.plist
else
echo "Dock not deleted.  Not a library account."
fi

I'm guessing it has something to do with the wildcard in the if statement but any suggestions are appreciated.

3 REPLIES 3

stevewood
Honored Contributor II
Honored Contributor II

You could use another variable and the tail command to get the last three characters:

#!/bin/bash

##This script will delete the com.apple.dock.plist on logout for generic
##library accounts.  A new default dock will be loaded on the next login.

##First, we need the currently logged in user

currentuser=`who | grep console | awk '{print $1}'`

lastThree=`echo -n $currentuser | tail -c 3`

##If the current user is a library account, delete the com.apple.dock.plist

if [ "$lastThree" == "lib" ]; then
rm /Users/$currentuser/Library/Preferences/com.apple.dock.plist
else
echo "Dock not deleted.  Not a library account."
fi

I haven't totally tested this, but it should work.

sean
Valued Contributor

Replace:

if [ "$currentuser" == *"lib" ]; then

with

if [[ "$currentuser" == *lib ]]; then

Alternatively you could add a grep at the end of your original currentuser definition:

currentuser=`who | grep console | awk '{print $1}' | grep "lib$"`

I prefer using stat to reduce the amount of pipes required:

currentuser=`stat -f%Su /dev/console | grep "lib$"`

Saying all that, if you are always removing the dock for any users called [something]lib, then you needn't bother checking who is logged in. Instead you could just run:

find /Users/*lib/Library/Preferences/com.apple.dock.plist -exec rm {} ;

tlarkin
Honored Contributor

Hey Everyone,

Just thought I would chime in here. Lots of good stuff going on in this thread. I am sure there are many ways to do this, but if we are looking to match any user with 'lib' in their name we might want to use some sort of pattern matching.

bash-3.2$ dscl . list /Users UniqueID | awk '/test/ && $2 > 500 { print $1 }'
test1
test2
test3
bash-3.2$ dscl . list /Users UniqueID | awk '/tet/ && $2 > 500 { print $1 }'
bash-3.2$ dscl . list /Users UniqueID | awk '/est/ && $2 > 500 { print $1 }'
test1
test2
test3
bash-3.2$ dscl . list /Users UniqueID | awk '/st/ && $2 > 500 { print $1 }'
test1
test2
test3

Notice when I pattern matched for 'tet' it gave me no results. The awk pipe also only prints out users that have a UID of greater than 500. This way we know they are actual human user accounts, and you can just check to see if the command returns a blank result. If it doesn't return a blank result you can then loop through those users and remove the dock items.

Maybe this will help, maybe it isn't exactly what you are looking for. Hopefully, someone will find this useful.

Thanks,
Tom