-1

I have a script that checks if the users home dir is there here is the bit I'm stuck with

j=`su $p -c df -h | grep $p`

can anyone help me here? df -h won't work but df will work...

user
  • 29,910
John R
  • 3
  • 1
    su $p -c "df -h" | grep $p – nKn Sep 07 '15 at 11:56
  • This is going through a while look so no luch with this – John R Sep 07 '15 at 11:58
  • #!/bin/bash filename='users'

    while read p; do

    j=su $p -c df -h | grep $p

    echo $j

    done < $filename

    – John R Sep 07 '15 at 11:58
  • j=su $p -c "df -h" | grep $p – John R Sep 07 '15 at 12:00
  • 1
    It doesn't matter if it's inside a loop, the problem is that if you don't use the double quotes, bash will think the -h flag should be added to su instead of the df command, so simply wrap the df -h command within double quotes and it should work – nKn Sep 07 '15 at 12:00
  • Note that sudo -u $p df -h will work without quotes: after processing its options sudo treats the rest of its run string as the command to be executed (there is no -c option). – AFH Sep 07 '15 at 12:17

1 Answers1

0

You are overcomplicating this.

I'm assuming here that $p holds the user name for the user you are currently interested in (because you are passing it to su), and that you are merely interested in a yes/no answer as to whether the home directory exists.

We can take advantage of the fact that in bash, ~username expands to the path to the home directory for username. This is done in this answer (the question has other answers pointing to other potential solutions as well):

eval echo ~$USER

We can then take advantage of the fact that test -d tests whether a file system name "exists and is a directory", and use subprocess substitution together with the eval to feed the expanded value for ~username to it:

test -d $( eval echo ~"$p" ) && echo yes

This will execute the echo yes if and only if (because it follows the &&, which requires the preceding command to return a success status) (1) the home directory for the user named by $p exists (note that this isn't dependent on it having any particular location in the file system), and (2) it is a directory. The quoting ensures that this won't break should the username contain whitespace (which is valid but uncommon on Unix-like systems).

Simply replace the echo yes with whatever you want. I get the feeling that you just want $j to be non-emtpy if the home directory exists, in which case you could do something much like (this snippet would form the body of the loop):

unset j
test -d $( eval echo ~"$p" ) && j=1
if test -n "$j"; then
    echo Home directory found for "$p"
else
    echo Home directory NOT found for "$p"
fi

(the last line would obviously be replaced by whatever specific logic you have in mind)

If you truly need the output of df, it shouldn't be terribly difficult to adapt the script above to provide it instead. For the time being, I'm leaving that as an exercise to the reader.

Oh, and try to use more descriptive variable names. It will help immensely the next time you need to make any adjustments to that script. $p would be much better as $user, for example, because $user actually describes what the value of that variable means.

user
  • 29,910