5

I have a command that may have an empty string as output, I want that when I execute:

myCommand | something 'default'

It either returns the output of myCommand or default if the output was empty

I have tried myCommand |awk '{if(\$0==""){print "default"}}' but it doesn't always work.

6
  • 5
    You could assign the output to a variable, var=$(myCommand), and then use parameter expansion: echo "${var:-default}"? Commented Jun 27, 2018 at 19:01
  • Does your command produce a massive amount of data if it produces any output? What are you doing with the data later? Commented Jun 27, 2018 at 19:04
  • No, only a few lines, I am posting the output on slack Commented Jun 27, 2018 at 19:08
  • 1
    Are you trying to print default if myCommand produces no output or print default for every line that myCommand outputs that is empty or contains only white space or what? Please edit your question to include concise, testable sample input and expected output for something as right now it's very unclear what you want. Commented Jun 27, 2018 at 19:45
  • Only if there is no output, but if there is a line with output all will have. Also, I do not have a lot of experience with bash scripting or awk ;) Commented Jun 27, 2018 at 20:14

3 Answers 3

4

Since you mentioned awk, here's one way.

Note this is for empty output, i.e., print the default if myCommand outputs nothing at all. If you want to handle a program that outputs a blank line, that's something different.

myCommand | awk -v def="default" '{print} END { if(NR==0) {print def} }'

{print} passes each input line through. At the end (END{...}), NR is the number of input records, i.e., the number of lines received from myCommand. This will be 0 if no output was printed by myCommand. If so, print the value of def, assigned on the command line by -v def="whatever text you want".

Tests:

$ awk -v def="default" '{print} END {if(NR==0) {print def}}' </dev/null
default
$ awk -v def="default" '{print} END {if(NR==0) {print def}}' <<<'foo'
foo
Sign up to request clarification or add additional context in comments.

3 Comments

awk 'END{print NR?$0:"default"}'
@karakfa Nice and tight, but only works for a single line of output. printf "1\n2\n3\n"|awk 'END{print NR?$0:"default"}' -> 3
right, I assumed it returns a value but perhaps a wrong assumption if the return value may contain newlines...
4
echo foo | sed 's/^$/default/'

Output:

foo

echo | sed 's/^$/default/'

Output:

default

2 Comments

Agreed, if "empty string" in the question means "blank line", and if that's the only output in cases where the default should be triggered. sed 's/^$/default/'</dev/null produces no output, for example. I think the question could be read either way.
Try printf "%s\n" 1 "" 3 |sed 's/^$/default/', and default appears in the middle of your output.
3

You can miss empty lines with

myCommand | grep . || echo 'default'

so you might prefer the comment of @BenjaminW. :

var=$(myCommand)
echo "${var:-default}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.