Posted on 07-10-2017 07:11 AM
Hey guys.
I tried everything in Jamf nation and other sites.
I couldn't find any solution for my problem.
I'm trying to write a script that test the file existence and if its true he should do the A if not he should do B.
The problem is that nothing works when it comes to Current User location ( ~ ).
Not the $3 or anything else worked..
Oh and it should work with Altiris or any other deployment solution.
You just cant find a reason to love Mac :(
Can someone help me with this problem I'm stuck with?
Solved! Go to Solution.
Posted on 07-10-2017 07:26 AM
~ always resolves to the home path of the account running the command. When scripts are run as root, ~
will resolve to /private/var/root/
If you're trying to test for something against the currently logged in user, you need to get the logged in user name first, and in some cases the home path, though the latter isn't necessary if you're certain the home folder will always be in /Users/ If they are, you just plug the current user name into a path like /Users/username/path/to/somefile
Example below:
#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console)
if [ -e "/Users/$loggedInUser/<rest of path here>/somefile" ]; then
*do something*
else
*do something else*
fi
Posted on 07-10-2017 07:26 AM
~ always resolves to the home path of the account running the command. When scripts are run as root, ~
will resolve to /private/var/root/
If you're trying to test for something against the currently logged in user, you need to get the logged in user name first, and in some cases the home path, though the latter isn't necessary if you're certain the home folder will always be in /Users/ If they are, you just plug the current user name into a path like /Users/username/path/to/somefile
Example below:
#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console)
if [ -e "/Users/$loggedInUser/<rest of path here>/somefile" ]; then
*do something*
else
*do something else*
fi
Posted on 07-10-2017 07:26 AM
Hi Yarin,
I am a little unclear what you mean by "Current User location ( ~ )", but in bash, for instance, what you are looking for would be something like this -
#!/bin/bash
filelookingfor="/pathtofile/filename"
if [ -f $filelookingfor ];
then
# found the file do something
echo "found file"
else
# didn't find the file, do something
echo "file not found"
fi
Posted on 07-10-2017 08:31 AM
@mm2270 posted a load of loop scripts. Can nest an if/else command into any of them for example.
Posted on 07-10-2017 08:31 AM
Duplicate
Posted on 07-10-2017 08:32 AM
Duplicate
Posted on 07-11-2017 06:42 AM
@mm2270
I wrote a similar code and I don't know why but what you wrote works ! thanks !!!
I tested it on sierra and now I'll try to run it on other MacOS to be sure.
I'm using it to test if our Safari Extension product exist and if not remove it (and safari must be restarted)
The script will be used by costumers IT teams to deploy.
I'm sharing The code with you:
#!/bin/bash
#
##############################################################
### Function to Restart Safari from Last Session ###
##############################################################
function ReSafari {
echo "Restarting Safari to update WalkMe Extension removal"
osascript -e 'tell application "Safari"
quit
end tell
delay 2 -- Wait for Safari to close
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Reopen All Windows From Last Session" of menu "History" of menu bar 1
end tell
end tell'
}
##############################################################
### Checking for current user :
loggedInUser=$(stat -f%Su /dev/console)
###
###########################################################
### Function to Remove Extenion ###
###########################################################
function UNEXTZ {
### Pull the current user
loggedInUser=$(stat -f%Su /dev/console)
### Removing Extension extz from Safari Library
echo "Removing WalkMe Extension from Safari library"
RM /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz
### Checking if extension exist after removal:
echo "Checking if WalkMe Extension Was Removed properly"
if [ -e /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz ]; then
echo "Could Not Remove WalkMe Extension"
else
echo "WalkMe Has Been Successfully Removed"
### Enable the folowing Command to Restart Safari (To Compelete the Task You must Restart Safari!):
ReSafari
fi
}
##############################################################
### Script ###
##############################################################
### Checking if Extension is Installed:
if [ -e /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz ]; then
echo "WalkMe Extension IS Installed"
UNEXTZ
else
echo "WalkMe Extension NOT Installed!"
fi```