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)
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.
@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.
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)}'; }