我想从工作目录中删除一些文件,但首先列出要删除的文件:
#!/usr/bin/env zsh
tput bold;
tput setaf 1;
echo "You are about to DELETE ALL auxillary files and Tmp files"
echo "These are the files to be deleted:"
tput sgr0;
for ext in log out aux dvi lof lot bit idx glo bbl bcf ilg toc ind out blg fdb_latexmk fls run.xml pyg pyg.out
do
find . -type f -name "*.$ext" -not -path "./.git/*" -print
done
find Tmp/ -type f
tput setaf 3;
read -s -k "?Press any key to purge!"$'n'
tput sgr0;
for ext in log out aux dvi lof lot bit idx glo bbl bcf ilg toc ind out blg fdb_latexmk fls run.xml pyg pyg.out
do
find . -type f -name "*.$ext" -not -path "./.git/*" -delete
done
find Tmp/ -type f -delete
tput bold;
tput setaf 1;
echo "DONE"
我想知道是否可以避免重复一大块代码两次。
setopt extendedglob
local -a ext=(
log out aux dvi lof lot bit idx glo bbl bcf ilg toc ind out blg
fdb_latexmk fls run.xml pyg pyg.out
)
local -a files=(
**/*.${^ext}~./.git/*(N.)
Tmp/*(N.)
)
print -nP '%B%F{1}'
{
if ! (( $#files )); then
print No files to delete!
return 1
fi
print -P You are about to DELETE ALL auxiliary files and Tmp
files in '%~'
print -P These are the files to be deleted:
} always {
print -nP '%b%f'
}
ls $files
{
print -nP '%F{3}'
if ! read -q '?Purge? [yn] '; then
print -P 'nPurge aborted.'
return 1
fi
rm -f $files
print -P 'n%B%F{1}DONE'
} always {
print -nP '%b%f'
}
extendedglob
启用各种方便的附加模式匹配器,例如用于否定的~
- 参数扩展
${…}
使数组被视为大括号扩展 - 默认情况下,Zsh中不匹配的模式会导致错误。glob限定符
(N)
导致它们被删除 (.)
仅与普通文件匹配- 通过将
-P
标志传递给print
内置命令,可以使用提示转义序列 always
子句总是在前一个块之后执行,即使在return
语句之后也是如此,但从不更改返回值read -q
读取一个字符,并且仅当该字符是y
或Y
时计算为true