bash 脚本列出给定用户的文件

发布于 2024-10-01 17:32:37 字数 267 浏览 0 评论 0原文

我对这个有疑问。 它不断地返回给我,不是一个目录,但肯定是

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-10-08 17:32:37

运行此命令时,您是否位于 /home 目录中?如果没有,您可能需要将其更改为:

if [ -d "/home/$DIR" ]; then

以匹配 ls 命令。假设您使用 myscript pax 之类的东西运行它来检查 /home/pax 目录,情况似乎确实如此。

如果您只想列出特定用户拥有的文件,则可以使用 awk 来仅打印第 3 列设置为所需值的文件 ($usrnm ),类似:

ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${usrnm} '$3==user{print}{}'

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}{}'
快乐很简单 2024-10-08 17:32:37

您没有测试您尝试列出的同一目录是否存在 - 也许您的意思是 -d "/home/$DIR"?或者从你的要求来看,你有两个参数吗?

user="$1"
dir="$2"

# and then examine "/home/$user/$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?

user="$1"
dir="$2"

# and then examine "/home/$user/$dir"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文