小编pef*_*cio的帖子

如何在 Bash 脚本中同时获取命令的结果及其返回码?

我正在玩 Bash 脚本。我想将命令的结果分配给一个变量,并将其返回代码分配给另一个变量。

前任。:

line_count=$(cat "$filename" | wc -l) #If the file does not exist, an error code is returned
cmd_return_code=$? #I want to assign the previous error code to a variable
Run Code Online (Sandbox Code Playgroud)

我尝试如上所示使用$?,但它捕获了前一个赋值本身的返回码,即 0。

我怎样才能做到这一点?

谢谢

bash bash-scripting

2
推荐指数
1
解决办法
2420
查看次数

标签 统计

bash ×1

bash-scripting ×1