在函数或别名中工作时 bash 命令会中断

发布于 2025-01-20 18:45:01 字数 3041 浏览 0 评论 0原文

当我以交互方式运行此命令时,它会输出预期的日志名称(最后修改的每个服务名称文件)。

# 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2025-01-27 18:45:01

关于功能的好处是,它们不需要任何特殊的逃脱。只需将命令完全按照函数中的写入,就可以了

lastLog() {
    ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^\/]+' -o)) | head -n1 | awk '{print $(NF)}'
}

。但是,如果您希望它作为单线仪,那就是这样,在关闭的卷发括号之前将半彩色附加到命令上:

lastLog() { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^\/]+' -o)) | head -n1 | awk '{print $(NF)}'; }

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:

lastLog() {
    ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^\/]+' -o)) | head -n1 | awk '{print $(NF)}'
}

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:

lastLog() { ls -tl | head -n20 | grep $(basename $(pwd | egrep '/environmentsDir/env/[^\/]+' -o)) | head -n1 | awk '{print $(NF)}'; }
他夏了夏天 2025-01-27 18:45:01

使用Shell的参数扩展可以简化您的功能:

lastLog() {
  local dirname=${PWD#/environmentDir/env/}     # remove the prefix
  dirname=${dirname%%/*}                        # remove the suffix

  ls -tl *"$dirname"* | head -n1
}

Your function can be simplified, using the shell's parameter expansion:

lastLog() {
  local dirname=${PWD#/environmentDir/env/}     # remove the prefix
  dirname=${dirname%%/*}                        # remove the suffix

  ls -tl *"$dirname"* | head -n1
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文