kubectl exec less 命令,输出无法重定向

kubectl exec less command and the output cannot be redirected

我在/etc/profile中定义了一些命令:

alias umcd="kubectl exec -ti -n actiontech-dmp \
$(k get pods| awk '/'umc'/{print}')  \
-c umc -- less /var/log/actiontech/umc/detail.log"

这个单独的别名命令工作正常,我可以看到详细日志。但是,每当我 运行 这个命令时:

umcd | grep "some key word" or 

umcd > detail.log. 

命令行工具永远挂了...

这是怎么回事?我对此一无所知,很遗憾 google 没有帮助。

发生这种情况是因为您正在使用 less 命令,它是 文件查看器 - it is user interface tool, it is changing terminal behaviour, while cat just reads data from file and outputs it to the terminal.

less 命令对于您的使用来说太复杂了,不建议将其用于简单的重定向或使用 grep 匹配模式,因为它可能会导致类似的问题。

在您的情况下,cat 命令是正确的工具。只需将您的别名更改为:

alias umcd="kubectl exec -ti -n actiontech-dmp \
$(k get pods| awk '/'umc'/{print}')  \
-c umc -- cat /var/log/actiontech/umc/detail.log"

它将正常工作。