shell 脚本错误日志记录
我正在尝试在我的 shell 脚本中设置一个简单的日志框架。为此,我想定义一个可调用的“日志”函数,其中
log "LEVEL" $message
消息是一个变量,我之前已将执行命令的输出重定向到该变量。我的问题是,我收到以下错误:
{message=command 2>&3 1>&3 3>&-} >&3
log "INFO" $message
有问题,不是吗?
TIA
I'm trying to setup a simple logging framework in my shell scripts. For this I'd like to define a "log" function callable as
log "LEVEL" $message
Where the message is a variable to which I have previously redirected the outputs of executed commands. My trouble is that I get errors with the following
{message=command 2>&3 1>&3 3>&-} >&3
log "INFO" $message
There's something wrong isn't there?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我为我的 shell 脚本开发了一个简单的多级记录器,你可以看看使用、灵感或任何你想要的东西。
I've developed a simple multi-level logger for my shell scripts, you can take a look for use, inspiration, or whatever you want.
也许你想要:
Perhaps you want:
尚不完全清楚您的问题与日志记录有关(看起来您的麻烦在于捕获要记录的文本,glenn jackman 已经巧妙地提出了解决方案),但为了以防万一,您可能希望了解
logger 命令,它为 shell 脚本提供了写入系统日志的能力。它有不同的级别和优先级以及一切。非常方便。
It's not entirely clear that your problem is related to logging (it looks like your trouble is capturing the text to log, which glenn jackman has ably suggested a solution for), but just in case, you might wish to be aware of the
logger
command found on most Unix systems—it provides shell scripts the ability to write the system log. It has various levels and priorities and everything. Very handy.除了
$()
之外,您还需要在大括号中使用空格和分号(如果您甚至需要它们):In addition to the
$()
, you need to use spaces and a semicolon with your curly braces (if you even need them):