在函数或别名中工作时 bash 命令会中断
当我以交互方式运行此命令时,它会输出预期的日志名称(最后修改的每个服务名称文件)。
# 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.
第二次编辑:
@john-kugelman的答案是正确的,我已经尝试过了,不成功。附加的问题是,正如下面 @gordon-davisson 的评论所指出的,我添加了一个与函数同名的别名,该别名获得了优先级,但也被破坏了。
When I run this command interactively it outputs the expected log name (last modified, per Service name file).
# 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)}'
When I try to escape it for an alias expression or a function, can't get it right, always fails for different reasons.
What would be the right way of escaping it, in CLI, to get a function or alias working?
For instance if I'd like to tail that log file the function or alias should allow me to:
lastLog=escaped(ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^\/]+' -o)) | head -n1 | awk '{print $(NF)}')
tail -f $(lastLog)
Edit:
Adding outputs of trying a function:
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.
2nd Edit:
@john-kugelman 's answer was correct, and I had already tried it, unsuccessfully. The added problem was, as pointed out by @gordon-davisson 's comment below, I had added an alias with same name as function, which was gaining precedence and was also broken.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于功能的好处是,它们不需要任何特殊的逃脱。只需将命令完全按照函数中的写入,就可以了
。但是,如果您希望它作为单线仪,那就是这样,在关闭的卷发括号之前将半彩色附加到命令上:
The nice thing about functions is that they don't require any special escaping. Just put the command exactly as written inside a function and you're good to go:
You can type a multi-line function definition at an interactive shell just like that. But if you want it as a one-liner, it would be this, with a semi-colon appended to the command before the closing curly brace:
使用Shell的参数扩展可以简化您的功能:
Your function can be simplified, using the shell's parameter expansion: