bash 脚本列出给定用户的文件
我对这个有疑问。 它不断地返回给我,不是一个目录,但肯定是
#!/usr/local/bin/bash
DIR=$1
if [ -d "$DIR" ]; then
ls -1Apl /home/$DIR | grep -v /\$
else
echo "not a directory"
fi
另一件事,我需要一点提示。我必须列出给定目录中给定用户的文件,其中我将用户和目录作为参数。 只是建议,请。
I have a problem with this one.
It is constantly returning me, not a directory, but is certainly is
#!/usr/local/bin/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行此命令时,您是否位于
/home
目录中?如果没有,您可能需要将其更改为:以匹配 ls 命令。假设您使用
myscript pax
之类的东西运行它来检查/home/pax
目录,情况似乎确实如此。如果您只想列出特定用户拥有的文件,则可以使用 awk 来仅打印第 3 列设置为所需值的文件 (
$usrnm
),类似:Are you in the
/home
directory when you run this? If not, you may want to change it to:to match the
ls
command. This is assuming you're running it with something likemyscript 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:您没有测试您尝试列出的同一目录是否存在 - 也许您的意思是
-d "/home/$DIR"
?或者从你的要求来看,你有两个参数吗?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?