结合unix find命令选项

时间:2019-07-07 21:05:13

标签: bash variables find

我编写了一个bash脚本以在macOS 10.l0.5上运行。我希望简化脚本。在下面的示例脚本中,首次调用find命令将提供所需的输出。在原始脚本中,我有多个查找命令,但排除了同一组目录。我希望将它们放在一个变量中。第二个find命令显示我失败的尝试。

我不知道为什么这失败了。

我可以接受任何在多个查找命令之间共享通用选项的方式。不,我不想排除所有隐藏目录。

我使用外部闪存驱动器运行它。该脚本是只读的。

#! /bin/bash

sourceDir="/Volumes/DOSDISK"

echo "--------------- find works as expected ------------------------------"
find ${sourceDir} \
 ! -path "${sourceDir}/.Trashes" \
 ! -path "${sourceDir}/.Trashes/*" \
 ! -path "${sourceDir}/.Spotlight-V100" \
 ! -path "${sourceDir}/.Spotlight-V100/*" \
 ! -path "${sourceDir}/.fseventsd" \
 ! -path "${sourceDir}/.fseventsd/*" \
 -type d \
 -exec echo {} \;


echo "----------------------------- does not skip hidden directories -----------"
dirsToSkip=" ! -path \"${sourceDir}\" "
echo "dirsToSkip is -1->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Trashes\" "
echo "dirsToSkip is -2->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Trashes/*\" " 
echo "dirsToSkip is -3->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Spotlight-V100\" " 
echo "dirsToSkip is -4->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Spotlight-V100/*\" " 
echo "dirsToSkip is -5->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.fseventsd\" " 
echo "dirsToSkip is -6->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.fseventsdf/*\" " 
echo "dirsToSkip is -7->${dirsToSkip}"

echo "see in print ---> "find ${sourceDir} \
 ${dirsToSkip} \
 -type d \
 -exec echo {} \;
echo -e "\n The non-working thing. "
find ${sourceDir} \
 ${dirsToSkip} \
 -type d \
 -exec echo {} \;

输出。将调试添加到脚本中时#! / bin / bash -v -x,我注意到了!打印为“!”。

mac $  ./config/trybash.bash
--------------- find works as expected ------------------------------
/Volumes/DOSDISK
/Volumes/DOSDISK/level2
/Volumes/DOSDISK/level3
----------------------------- does not skip hidden directories -----------
dirsToSkip is -1-> ! -path "/Volumes/DOSDISK" 
dirsToSkip is -2-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" 
dirsToSkip is -3-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" 
dirsToSkip is -4-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" 
dirsToSkip is -5-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" 
dirsToSkip is -6-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd" 
dirsToSkip is -7-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd" ! -path "/Volumes/DOSDISK/.fseventsdf/*" 
see in print ---> find /Volumes/DOSDISK ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd" ! -path "/Volumes/DOSDISK/.fseventsdf/*" -type d -exec echo {} ;

 The non-working thing. 
/Volumes/DOSDISK
/Volumes/DOSDISK/.Trashes
/Volumes/DOSDISK/.Trashes/501
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100/Store-V2
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100/Store-V2/FF996064-BEDD-474E-9E76-7F8ABD688B09
/Volumes/DOSDISK/.Spotlight-V100
/Volumes/DOSDISK/.Spotlight-V100/Store-V2
/Volumes/DOSDISK/.Spotlight-V100/Store-V1
/Volumes/DOSDISK/.fseventsd
/Volumes/DOSDISK/level2
/Volumes/DOSDISK/level3
mac $ 

1 个答案:

答案 0 :(得分:0)

在此主题中获得建议后,我开发了此脚本。要运行,您需要将变量sourceDir更改为目录之一的路径。这是只能控制台打印的脚本。

#! /bin/bash
#
# Answer to my question:
#   https://stackoverflow.com/questions/56926105/combining-unix-find-command-options?noredirect=1#comment100396210_56926105
#  see Gordon Davisson's comment
#
sourceDir="/Volumes/DOSDISK"

echo "--------------- find works as expected ------------------------------"
find ${sourceDir} \
 ! -path "${sourceDir}" \
 ! -path "${sourceDir}/.Trashes" \
 ! -path "${sourceDir}/.Trashes/*" \
 ! -path "${sourceDir}/.Spotlight-V100" \
 ! -path "${sourceDir}/.Spotlight-V100/*" \
 ! -path "${sourceDir}/.fseventsd" \
 ! -path "${sourceDir}/.fseventsd/*" \
 -type d \
 -exec echo {} \;


echo "----------------------------- Another way to skip hidden directories -----------"
# You place one and only one argument in each array element.
# avoid add extraneous leading and trailing blanks.
# No need to double quote strings.
#

fileargs=()

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.Trashes")

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.Trashes/*")

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.Spotlight-V100")

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.Spotlight-V100/*")

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.fseventsd")

fileargs+=("!")
fileargs+=("-path") 
fileargs+=("${sourceDir}/.fseventsdf/*" )

echo "fileargs are -->${fileargs[@]}<--"

# This invocation is optional.  Remove comments to see 
# the arguments printed out
# you will need the associated bash file.
#echo -e "\n Print args. "
#/Users/mac/config/displayinput.bash ${sourceDir} \
#  ! -path "${sourceDir}" \
# "${fileargs[@]}" \
# -type d \
# -exec echo {} \;

echo -e "\n A second working thing. \n"
find ${sourceDir} \
  ! -path "${sourceDir}" \
 "${fileargs[@]}" \
 -type d \
 -exec echo {} \;

exit 0

这是可选的displayinput.bash脚本

#! /bin/bash

# display input
echo -e "\nin displayinput.bash"
echo -e "\nall args -->${*}<--\n"

declare -i position
position="1";

while [[ $# -gt 0 ]]
do
  key="$1"
  echo "input #${position} token ${1}"
  shift # past argument
  position="position + 1";
done