【发布时间】:2019-07-07 21:05:13
【问题描述】:
我编写了一个在 macOS 10.l0.5 上运行的 bash 脚本。我希望简化脚本。在下面的示例脚本中,第一次调用 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 $
【问题讨论】:
-
您嵌入的引号是字面意思。
-
哦,我现在明白了。 ${dirsToSkip} 中的文本未标记化。相反,它作为一个长字符串传递。
-
@historystamp "tokenized" 是错误的词;它没有被解析。或者更确切地说,它有点奇怪地被半解析了。具体来说,它被基于空格(这是一种标记化)分割成“单词”,然后任何带有通配符的单词都被扩展为匹配文件名的列表。但是引号和转义没有被解析——这是在过程的早期阶段完成的,在变量展开之前。