当我以交互方式运行此命令时,它会输出预期的日志名称(上次修改,按服务名称文件(。
# Gets last active service log in a /environmentsDir/serviceName/var/output/logs directory. You need to cd 1st.
ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'
当我试图为别名表达式或函数转义它时,无法正确执行,由于不同的原因总是失败。
在CLI中,为了使函数或别名正常工作,正确的转义方法是什么?
例如,如果我想跟踪日志文件,函数或别名应该允许我:
lastLog=escaped(ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}')
tail -f $(lastLog)
编辑:
添加尝试功能的输出:
21:55:28-user@host:/environmentsDir/env/serviceName/var/output/logs$ ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'
serviceName.2022-04-12-21
21:55:33-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog() {
logbash: syntax error near unexpected token `('
21:55:42-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog() {
logbash: syntax error near unexpected token `('
21:55:46-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog {
> ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'
head: invalid option -- 't'
Try 'head --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
21:56:10-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog() { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'; }
logbash: syntax error near unexpected token `('
21:56:41-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog() { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'; }^C
21:56:56-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog() {ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}' ; }
logbash: syntax error near unexpected token `('
21:57:14-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}' ; }
logbash: syntax error near unexpected token `}'
# This one doesn't blow up but still fails when executed:
21:57:35-user@host:/environmentsDir/env/serviceName/var/output/logs$ function lastLog { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}' ; }
21:57:47-user@host:/environmentsDir/env/serviceName/var/output/logs$ lastLog
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
第二次编辑:
@约翰·库格尔曼的答案是正确的,我已经试过了,但没有成功。正如@gordon davisson在下面的评论中指出的那样,添加的问题是,我添加了一个与函数同名的别名,该别名的优先级越来越高,也被破坏了。
函数的好处在于它们不需要任何特殊的转义。只需将命令完全按照编写在函数中的方式放入即可:
lastLog() {
ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'
}
您可以在交互式shell中键入多行函数定义,就像这样。但是,如果你想把它作为一行,它应该是这样的,在结束大括号之前在命令后面附加一个分号:
lastLog() { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^/]+' -o)) | head -n1 | awk '{print $(NF)}'; }
您的函数可以使用shell的参数扩展来简化:
lastLog() {
local dirname=${PWD#/environmentDir/env/} # remove the prefix
dirname=${dirname%%/*} # remove the suffix
ls -tl *"$dirname"* | head -n1
}