bash script list files from given user
I have a problem with this one. It is constantly returning me, not a directory, but is certainly is
#!/usr/local/bin/开发者_StackOverflow中文版bash
DIR=$1
if [ -d "$DIR" ]; then
ls -1Apl /home/$DIR | grep -v /\$
else
echo "not a directory"
fi
One more thing, I need a little hint. I have to list files from a given user in a given directory, where I get both the user and directory as parameters. Just suggestions, please.
Are you in the /home
directory when you run this? If not, you may want to change it to:
if [ -d "/home/$DIR" ]; then
to match the ls
command. This is assuming you're running it with something like myscript pax
to examine the /home/pax
directory, which seems to be the case.
And if you want to only list those files in there owned by a specific user, you can use awk
to only print those with column 3 set to the desired value ($usrnm
), something like:
ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${usrnm} '$3==user{print}{}'
You're not testing for the existence of the same directory as you're trying to list - maybe you mean -d "/home/$DIR"
? Or from your requirement, do you have two parameters?
user="$1"
dir="$2"
# and then examine "/home/$user/$dir"
精彩评论