0

I've made a bash script which is executed as root and must verify the existence of a file in the Desktop of an user. Here is the code:

if [ -e /Users/*/Desktop/file ]; then
....

The fact is if there are multiple users in /Users/, and that the file is not present in the first user (by alphabetical order) but in one of the others, it will fail.

So to resolve the issue, I thought this was good:

if [ -e ~/Desktop/file ]; then
....

but there, as the script is executed as root, ~/Desktop/ is /private/var/root/Desktop instead of /Users/my_user/Desktop .

I'm not very at ease for the moment with bash so I hope you will help me to resolve this issue ! Thanks !

b1onic
  • 239
  • 2
  • 14

3 Answers3

0

I don't get it? Why don't you specify home dir of that specific user ~youruser/Desktop?

I assume that you wanted that. So the line will look like:

if [ -e ~youruser/Desktop/file ]; the
...

Did I understood question correctly? Just search Google on 'linux tilde expansion'

And watch out NOT to have quotes around tilde. On how to use quotes, read discussion: https://stackoverflow.com/a/3984130/1579985

Community
  • 1
  • 1
Michael Field
  • 2,092
  • 1
  • 17
  • 12
0

You could use a for loop in bash to test for each user's home directory in turn, e.g.:

for d in /Users/*
do
    if [ -e "$d/Desktop/file" ]
    then
        echo "The file existed under $d"
    else
        echo "The file was not found under $d"
    fi
done

The /Users/* is expanded into /Users/alice /Users/bob /Users/charles (or whatever) and then $d is given the value of each of those in turn inside the loop. Obviously you can replace the echo statments with whatever you want to do in either case.

The bash manual's section on looping constructs, including for loops, is here - that might not be the best place to start, but you can find plenty of examples with Google.

One thing to note about this answer is that if you're being strict, a user's home directory doesn't have to be called /Users/username - strictly speaking you need to look in /etc/passwd to find all users and their corresponding home directories. However, for what you want to do, this might be enough.


Update 1: It sounds from the comment below as if you want to take an action if no file matching /Users/*/Desktop/file exists for any user. This sounds surprising to me, but if you really want to do that, you could use one of the recipes from this other Stack Overflow question, for example:

if [ -z "$(shopt -s nullglob; echo /Users/*/Desktop/file)" ]
then
    echo "No one had a file in ~/Desktop/file"
fi

Update 2: Your comments on Dennis Williamson's answer indicate that this script is expected to be run by non-root users on this machine. If that's the case then I think his suggestion in the comments is the right one - you just want to test:

if [ -e "$HOME/Desktop/file" ]
then
    echo "Found the file"
else
    echo "Failed to find the file"
fi

... and then you just just need to make sure you test it as a non-root user.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Thanks for your help ! Currently in 'else' the script kills a processus. So each time the file is not found, it kills the processus. How to make the processus killed only when the file has not been find at all ? – b1onic Mar 05 '11 at 15:00
  • @b1onic: so you want to kill a process only if the file `~/Desktop/file` doesn't exist for any user? – Mark Longair Mar 05 '11 at 16:26
0

In the title of your question you say "specific user". Why not name the specific user?

if [ -e /Users/username/Desktop/file ]

if you're trying to find such a file, use find:

find /Users -path '/Users/*/Desktop/*' -name 'file'
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439