Reputation: 340
I have a Bash script that calls several other scripts (syncing user lists / detecting inconsistencies). For each script that produces an output, I'd like to add a header before the actual output, but for scripts without any output, I don't want anything printed.
Currently, I simply print out the header line in any case:
#!/bin/bash
for scrpt in test1.sh test2.sh; do
echo "Script $scrpt printed out:"
./${scrpt}
done
However, what I want is the "Script $script printed out:" to appear only when ./${scrpt} really produces some output.
I could store the output in a variable and check that:
#!/bin/bash
for scrpt in test1.sh test2.sh; do
ctnt=`./${scrpt}`
if [ -n "$ctnt" ]; then
echo -e "Script returned (with temporary variable): \n$ctnt"
fi
done
However, my scripts run quite some time, so I'd like the ouput to appear as soon as it is created. And before the first output is printed, the header should appear. So while the above generates the output that I need, is not a good solution for me.
What I have in mind is something that can be called like:
#!/bin/bash
for scrpt in test1.sh test2.sh; do
./${scrpt} | print-text-before-first-output "Script $scrpt printed:"
done
Is there such a utility available?
Upvotes: 1
Views: 1355
Reputation: 88731
With GNU sed: replace
./${scrpt}
with
./${scrpt} | sed '1s/^/Header\n&/'
Upvotes: 2