如果命令 returns 什么都没有,那么 print/echo 在 csh shell 中向 STDOUT 声明
If command returns nothing, then print/echo a statement to STDOUT in csh shell
我在文件中查找 word/pattern,如果文件中不存在这样的模式,那么我想 print/echo“-”。我们可以在命令行中使用单行命令执行此操作吗?
grep word <file> | <command to return "-" if "word" not present in file>
类似地,如果脚本 returns 什么都没有,回显“-”
python script.py | <command to return "-" if script returns nothing/NULL
这可以通过以下命令完成:
grep word file.txt || echo "-"
如果文件中存在 word,那么它将打印文件中的行,否则将打印 -
。
如果命令是脚本则:
ret=`python script.py`; if [[ "$ret" ]];then echo "$ret"; else echo "-"; fi
如果你想检查一个命令是否没有输出而不管它的返回码,那么你应该检查没有输出,一种方法是:
awk '{print} END {if (NR == 0) print "-"}'
例如:
$ grep - /dev/null | awk '{print} END {if (NR == 0) print "-"}'
-
$ echo some output | awk '{print} END {if (NR == 0) print "-"}'
some output
我在文件中查找 word/pattern,如果文件中不存在这样的模式,那么我想 print/echo“-”。我们可以在命令行中使用单行命令执行此操作吗?
grep word <file> | <command to return "-" if "word" not present in file>
类似地,如果脚本 returns 什么都没有,回显“-”
python script.py | <command to return "-" if script returns nothing/NULL
这可以通过以下命令完成:
grep word file.txt || echo "-"
如果文件中存在 word,那么它将打印文件中的行,否则将打印 -
。
如果命令是脚本则:
ret=`python script.py`; if [[ "$ret" ]];then echo "$ret"; else echo "-"; fi
如果你想检查一个命令是否没有输出而不管它的返回码,那么你应该检查没有输出,一种方法是:
awk '{print} END {if (NR == 0) print "-"}'
例如:
$ grep - /dev/null | awk '{print} END {if (NR == 0) print "-"}'
-
$ echo some output | awk '{print} END {if (NR == 0) print "-"}'
some output