我试图通过创建一个不需要在每个术语中添加-e的别名来简化bash命令:
alias mylog='tail -f /c/logs/elog.txt | grep --line-buffered -i auctions | grep --line-buffered --color -i'
当我希望它是mylog mask face belt
时,我用mylog -e mask -e face -e belt
来称呼它
我怎样才能做到这一点?
使用函数而不是别名可以编写代码来操作参数列表:
mylog() {
# write usage when called without arguments, because why not?
(( "$#" )) || { echo "Usage: mylog arg1 [arg2 ...]" >&2; return 1; }
# logic to calculate the argument list we want to pass to grep...
local arg; local -a args=( ) # make these locals so we don't change outer scope
for arg do # this iterates over "$@" by default
args+=( -e "$arg" ) # thus, adding -e "$arg" for each argument we were called with
done
# and then the actual pipeline we're here for
tail -f /c/logs/elog.txt
| grep --line-buffered -i auctions
| grep --line-buffered --color -i "${args[@]}"
}