help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Test if there is any output from a program, if yes, prin


From: Peggy Russell
Subject: Re: [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 20:26:52 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

> 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.

There are multiple ways of doing this. Here's an option that uses process
substitution. For this example I used the output of the find command.

while IFS= read -re line; do 
  printf -- '%s\n%s\n' 'user specified string' "${line}" 
done < <(find . -maxdepth 1 -type f -name '[^.]*')

# Variation of the above.
IFS= readarray -t cmdOutput < <(find . -maxdepth 1 -type f -name '[^.]*')
if [[ address@hidden -ne 0 ]]; then
  printf -- '%s\n' 'user specified string' 
  printf -- '%s\n' "address@hidden"     
fi

Also look into command substitution and redirection. 
There are FAQ's at this link that you will find helpful:
http://mywiki.wooledge.org/BashFAQ

Peg Russell
 






reply via email to

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