我正在编写一个小脚本,它将帮助我调试一些权限问题。我正在传递我想要检查的父文件夹,并且能够指定我想要忽略的任何子文件夹。
我在传递构造的参数字符串时遇到问题,find
因为它的某些部分(正在转义)。我似乎无法弄清楚如何以find
正确接受它的方式将通配符提供到命令中。通配符到位,路径字符串的该部分使用单引号进行限定,单引号使用转义'\''
并让我感到困惑(因为我无法弄清楚如何控制转换)和find
(这基本上忽略了我的排除)
我一直在阅读有关单引号和双引号以及转义字符的所有内容,但我还没有找到与我的类似的示例。
#!/bin/bash -f
# output permissions and ownership with path relative to specified parent.
Usage="$0 <parent path> <excluded child folder> ...."
if [ $# -lt 1 ]
then
(>&2 echo -e $Usage)
exit 1
else
parent=$1
shift
if [ $# -gt 0 ]
then
excludes=" ("
for folder in $@
do
thisLine=" ! -path $parent$folder ! -path '$parent$folder/*'" <=== the '*' wildcard is causing the problem I think.
excludes=$excludes$thisLine
done
excludes=$excludes" )"
fi
(>&2 echo "=> find $parent $excludes -ls | awk '{print "'$3"|"$5"|"$6"|"$11}'"'")
(>&2 echo )
set -vx
find $parent $excludes -ls | awk '{print $3"|"$5"|"$6"|"$11}'
fi
我正在使用的树的分支/home/user/catkin_ws/src/clfsm
有三个子文件夹,我希望排除其中两个;cmake
& include
.下面的输出分为两部分:顶部是当前输出,它不会过滤我希望排除的文件夹。底部部分是正确的,使用上面代码中的回显命令行。
调用上述脚本的命令是:
~/myScripts/show_permissions.sh /media/nap/U14041/home/nap/catkin_ws/src/clfsm /cmake /include
。请注意,Stephen 的解决方案要求指定要排除的子文件夹,且不带前导/
.
user@rMBP-Ubuntu:[12:29]:/home/user/catkin_ws/src/clfsm$ ~/myScripts/show_permissions.sh /home/user/catkin_ws/src/clfsm /cmake /include
=> find /home/user/catkin_ws/src/clfsm ( ! -path /home/user/catkin_ws/src/clfsm/cmake ! -path '/home/user/catkin_ws/src/clfsm/cmake/*' ! -path /home/user/catkin_ws/src/clfsm/include ! -path '/home/user/catkin_ws/src/clfsm/include/*' ) -ls | awk '{print $3"|"$5"|"$6"|"$11}'
++ find /home/user/catkin_ws/src/clfsm '(' '!' -path /home/user/catkin_ws/src/clfsm/cmake '!' -path ''\''/home/user/catkin_ws/src/clfsm/cmake/*'\''' '!' -path /home/user/catkin_ws/src/clfsm/include '!' -path ''\''/home/user/catkin_ws/src/clfsm/include/*'\''' ')' -ls
++ awk '{print $3"|"$5"|"$6"|"$11}'
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.layout
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/src
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitorsupport.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_machine.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitors.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_main.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_cc.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/cmake/FindLibDispatch.cmake
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/CMakeLists.txt
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_vector_factory.h
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/FSMControlStatus.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/FSM_Control.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/wb_fsm_control_status.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/wb_fsm_state_status.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/CLActionAction.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBQueryPredicate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMSuspensibleMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBSubMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/gu_util.h~
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/CLTransitionExpression.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBContext.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMState.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMAction.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMExpression.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_cc.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMTransition.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_visitorsupport.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/stringConstants.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_factory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMFactory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMachineVector.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMActivity.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_visitors.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_cc_delegate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_machine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/gu_util.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBPredicate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_wb_vector_factory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSM.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/package.xml
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.cbp
user@rMBP-Ubuntu:[12:34]:/home/user/catkin_ws/src/clfsm$ find /home/user/catkin_ws/src/clfsm \( ! -path /home/user/catkin_ws/src/clfsm/cmake ! -path '/home/user/catkin_ws/src/clfsm/cmake/*' ! -path /home/user/catkin_ws/src/clfsm/include ! -path '/home/user/catkin_ws/src/clfsm/include/*' \) -ls | awk '{print $3"|"$5"|"$6"|"$11}'
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.layout
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/src
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitorsupport.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_machine.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitors.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_main.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_cc.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/CMakeLists.txt
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/package.xml
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.cbp
user@rMBP-Ubuntu:[12:35]:/home/user/catkin_ws/src/clfsm$
答案1
如果我正确理解了这个要求,你应该使用-path ... -prune
停止下降到树上。
就像是:
#!/bin/bash -f
# output permissions and ownership with path relative to specified parent.
Usage="$0 <parent path> <excluded child folder> ...."
if [ $# -lt 1 ]
then
(>&2 echo -e $Usage)
exit 1
else
parent=$1
shift
if [ $# -gt 0 ]
then
for folder in $@
do
thisLine=" ( -path $parent/$folder -prune ) -o"
excludes=$excludes$thisLine
done
fi
set -vx
find $parent $excludes -ls | awk '{print $3"|"$5"|"$6"|"$11}'
fi
这个想法是构建一个类似于
find /tmp/A \( -path /tmp/A/skip1 -prune \) -o -ls