Skip to main content
Solved

Checking if file exist with Jamf script (ROOT)


Forum|alt.badge.img+4
  • Contributor
  • 15 replies

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?

Best answer by mm2270

~ 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:

1#!/bin/sh
2
3loggedInUser=$(stat -f%Su /dev/console)
4
5if [ -e "/Users/$loggedInUser/<rest of path here>/somefile" ]; then
6 *do something*
7else
8 *do something else*
9fi
View original
Did this topic help you find an answer to your question?

6 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • Answer
  • July 10, 2017

~ 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:

1#!/bin/sh
2
3loggedInUser=$(stat -f%Su /dev/console)
4
5if [ -e "/Users/$loggedInUser/<rest of path here>/somefile" ]; then
6 *do something*
7else
8 *do something else*
9fi

Forum|alt.badge.img+7
  • Contributor
  • 35 replies
  • July 10, 2017

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 -

1#!/bin/bash
2
3filelookingfor="/pathtofile/filename"
4
5if [ -f $filelookingfor ];
6then
7# found the file do something
8echo "found file"
9else
10# didn't find the file, do something
11echo "file not found"
12fi

donmontalvo
Forum|alt.badge.img+36
  • Hall of Fame
  • 4293 replies
  • July 10, 2017

@mm2270 posted a load of loop scripts. Can nest an if/else command into any of them for example.


donmontalvo
Forum|alt.badge.img+36
  • Hall of Fame
  • 4293 replies
  • July 10, 2017

Duplicate


donmontalvo
Forum|alt.badge.img+36
  • Hall of Fame
  • 4293 replies
  • July 10, 2017

Duplicate


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 15 replies
  • July 11, 2017

@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:

1#!/bin/bash
2#
3##############################################################
4### Function to Restart Safari from Last Session ###
5##############################################################
6function ReSafari {
7 echo "Restarting Safari to update WalkMe Extension removal"
8 osascript -e 'tell application "Safari"
9 quit
10 end tell
11 delay 2 -- Wait for Safari to close
12 tell application "Safari" to activate
13 tell application "System Events"
14 tell process "Safari"
15 click menu item "Reopen All Windows From Last Session" of menu "History" of menu bar 1
16 end tell
17 end tell'
18}
19##############################################################
20### Checking for current user :
21loggedInUser=$(stat -f%Su /dev/console)
22###
23###########################################################
24### Function to Remove Extenion ###
25###########################################################
26function UNEXTZ {
27 ### Pull the current user
28 loggedInUser=$(stat -f%Su /dev/console)
29 ### Removing Extension extz from Safari Library
30 echo "Removing WalkMe Extension from Safari library"
31 RM /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz
32 ### Checking if extension exist after removal:
33 echo "Checking if WalkMe Extension Was Removed properly"
34 if [ -e /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz ]; then
35 echo "Could Not Remove WalkMe Extension"
36 else
37 echo "WalkMe Has Been Successfully Removed"
38 ### Enable the folowing Command to Restart Safari (To Compelete the Task You must Restart Safari!):
39 ReSafari
40 fi
41}
42##############################################################
43### Script ###
44##############################################################
45### Checking if Extension is Installed:
46if [ -e /Users/$loggedInUser/Library/Safari/Extensions/Walkme Extension.safariextz ]; then
47 echo "WalkMe Extension IS Installed"
48 UNEXTZ
49else
50 echo "WalkMe Extension NOT Installed!"
51fi```

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings