当我的程序之一返回非零退出代码时,我想避免重定向其输出。这是可能的,如果是,我该怎么做?
我失败的尝试:
echo foo > file
false | cat > file
Run Code Online (Sandbox Code Playgroud)
这导致file
为空。我想要的行为是仅file
在程序成功时进行调整。
我还想知道是否可以只在输出非空的情况下更新文件而不使用多个文件。
你可以这样使用它:
out=$(some_command) && echo "$out" > outfile
Run Code Online (Sandbox Code Playgroud)
echo "$out" > outfile
只有在some_command
成功时才会执行。