正确引用bash别名定义 [英] Properly quote bash alias definition
问题描述
我尝试将以下命令放入bash别名中.该命令本身可以正常工作,但是当我尝试使用别名时,出现以下错误:
I have the following command that I am trying to put into a bash alias. The command by itself works fine, but when I try to alias it, I am getting the following errors:
命令
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "$(find "{}" -type f | wc -l)" {}' \; | sort -nr
别名
alias csfiles='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "$(find "{}" -type f | wc -l)" {}' \; | sort -nr'
错误:
-sh: alias 0: not found
-sh: alias {} \; | sort nr: not found
我认为这意味着我没有正确使用引号,但是在确定正确的组合时遇到了麻烦.帮助吗?
I think this means I am not using quotes right but I am having trouble determining the correct combo. Help?
推荐答案
您的外部 find
不能完成简单的glob无法做到的任何事情.这样就消除了一层引号(以及找到的每个目录的 sh
进程).
Your outer find
doesn't do anything you couldn't do with a simple glob. This eliminates a layer of quotes (along with the sh
process for each directory found).
# Ignoring the issue of assuming no file name contains a newline
for d in ./*/; do
echo "$(find "$d" -type f | wc -l) $d"
done
只需定义一个shell函数即可消除对 alias
的参数施加的第二层.
Just define a shell function to eliminate the second layer imposed on the argument to alias
.
csfiles () {
for d in ./*/; do
echo "$(find "$d" -type f | wc -l) $d"
done
}
对 find
的其余调用也可以替换为 for
循环,从而消除了每个文件名只占一行的问题:
The remaining call(s) to find
can also be replaced with a for
loop, eliminating the problematic assumption of one line per file name:
csfiles () {
for d in ./*/; do
echo "$(for f in "$d"/*; do [ -f "$f" ] && echo; done | wc -l) $d"
done
}
如果它支持 -printf
主键,则可以保留 find
,因为您不必关心文件的实际名称,只需要一行就可以了.每个文件的输出数.
You could keep find
if it supports the -printf
primary, because you don't care about the actual names of the files, just that you get exactly one line of output per file.
csfiles () {
for d in ./*/; do
echo "$(find "$d" -type f -printf . | wc -l) $d"
done
}
这篇关于正确引用bash别名定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!