![如何从 tcsh 中的 find 命令结果中排除多个用户?](https://linux22.com/image/135891/%E5%A6%82%E4%BD%95%E4%BB%8E%20tcsh%20%E4%B8%AD%E7%9A%84%20find%20%E5%91%BD%E4%BB%A4%E7%BB%93%E6%9E%9C%E4%B8%AD%E6%8E%92%E9%99%A4%E5%A4%9A%E4%B8%AA%E7%94%A8%E6%88%B7%EF%BC%9F.png)
我想从查找结果中排除一组用户,他们不属于同一个 unix 组,这不是最好的方法,对吗?
find . -maxdepth 1 -type d -name '*_pattern_*' ! -user user1 ! -user user2 ...
我可以将用户作为字符串或数组传递吗?也许用 awk ?
答案1
在 cshell 上,您可以拼凑find
命令来执行该作业,如下所示:
#!/bin/tcsh -f
# persona non grata
set png = ( \
user1 \
user2 \
user3 \
user4 \
)
# build dynamically a portion of the `find` command
set cmd = ( `printf '! -user %s\n' $png:q` )
# now fire the generated find command
find . -maxdepth 1 -type d -name '*_pattern_*' $cmd:q
答案2
除了不喜欢未转义的感叹号这一事实之外csh
,在命令行中,您的命令看起来不错并且无法做得更好
答案3
如果您乐意从脚本运行它/bin/sh
:
#!/bin/sh
# the users to avoid
set -- user1 user2 user3 user4
# create a list of
# -o -user "username" -o -user "username" etc.
for user do
set -- "$@" -o -user "$user"
shift
done
shift # remove that first "-o"
# use the created array in the call to find
find . -maxdepth 1 -type d -name '*_pattern_*' ! '(' "$@" ')'
或者,构建与! -user "username"
您使用的相同类型的列表:
#!/bin/sh
# the users to avoid
set -- user1 user2 user3 user4
# create a list of
# ! -user "username" ! -user "username" etc.
for user do
set -- "$@" ! -user "$user"
shift
done
# use the created array in the call to find
find . -maxdepth 1 -type d -name '*_pattern_*' "$@"