help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Test if there is any output from a program, if yes, print th


From: Peng Yu
Subject: [Help-bash] Test if there is any output from a program, if yes, print the user specified string and the output from the program? (without using a tempfile)
Date: Sun, 2 Feb 2014 11:57:40 -0600

Hi,

I want print some user specified string only if a give program print
something to its stdout. If the given program prints something, I want
it print after the user specified string.

A script using a tempfile could be the following.

./prog > temp.txt
if [ -s temp.txt ]
then
  echo "user specified string"
  cat temp.txt
fi

I want to avoid the use of tempfile. So I made the following awk
script. But I'm not sure if it is robust with respect to any possible
input (e.g., binary input).

~/linux/test/awk/senario$ ./main.sh
echo xxx | awk -v str='user str'$'\n' -f main.awk
user str
xxx
echo -n | awk -v str='user str'$'\n' -f main.awk

~/linux/test/awk/senario$ cat main.awk
{
  if(printed) {
    print
  } else {
    printed=1
    printf "%s", str
    print
  }
}

~/linux/test/awk/senario$ ./main.sh
echo xxx | awk -v str='user str'$'\n' -f main.awk
user str
xxx
echo -n | awk -v str='user str'$'\n' -f main.awk

Does anyone know what might be a best solution?

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]