查找命令仅在脚本中不起作用
Find commands don't work in script only
我有一个脚本可以在特定位置搜索 .txt 文件并将结果输出到标准输出,并使用 tee 命令输出到一个文件。至少它应该是。但是,我遇到了一些奇怪的问题。这是代码:
echo -e "${HIGHLIGHT}Sensitive files:${OFF}"
echo "## Sensitive files:" >> $ofile
for file in $(cat ); do ls -lh $file 2>/dev/null; done | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Suids:${OFF}"
echo "## Suids:" >> $ofile
find / -type f \( -perm -04000 -o -perm -02000 \) -exec ls -Alh {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Owned by root only:${OFF}"
echo "## Owned by root only:" >> $ofile
find / -type f -user root \( -perm -04000 -o -perm -02000 \) -exec ls -lg {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile
# Text files
echo -e "${HIGHLIGHT}Text files:${OFF}"
echo "## Text files:" >> $ofile
find /etc -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /home -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /root -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
奇怪的是,除了底部的 find 搜索 .txt 文件外,所有命令都运行良好。 None 这些命令在脚本中有效,但如果我将它们复制并粘贴到终端并 运行 它 与它们在脚本中的完全一样 ,它们就有效正好。这怎么可能?
您需要在 -name
模式中引用或转义 *
,否则 shell 会尝试扩展它并在命令行中的位置使用扩展形式。
find /etc -type f -name '*.txt' -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
其他类似的都行
我有一个脚本可以在特定位置搜索 .txt 文件并将结果输出到标准输出,并使用 tee 命令输出到一个文件。至少它应该是。但是,我遇到了一些奇怪的问题。这是代码:
echo -e "${HIGHLIGHT}Sensitive files:${OFF}"
echo "## Sensitive files:" >> $ofile
for file in $(cat ); do ls -lh $file 2>/dev/null; done | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Suids:${OFF}"
echo "## Suids:" >> $ofile
find / -type f \( -perm -04000 -o -perm -02000 \) -exec ls -Alh {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Owned by root only:${OFF}"
echo "## Owned by root only:" >> $ofile
find / -type f -user root \( -perm -04000 -o -perm -02000 \) -exec ls -lg {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile
# Text files
echo -e "${HIGHLIGHT}Text files:${OFF}"
echo "## Text files:" >> $ofile
find /etc -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /home -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /root -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
奇怪的是,除了底部的 find 搜索 .txt 文件外,所有命令都运行良好。 None 这些命令在脚本中有效,但如果我将它们复制并粘贴到终端并 运行 它 与它们在脚本中的完全一样 ,它们就有效正好。这怎么可能?
您需要在 -name
模式中引用或转义 *
,否则 shell 会尝试扩展它并在命令行中的位置使用扩展形式。
find /etc -type f -name '*.txt' -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
其他类似的都行