0

I have two scripts; parentScript.sh and childScript.sh.

I want to be able to call childScript.sh inside parentScript.sh and return the errors that occur within at any stage. i.e. an error found within childScript.sh looks like:

echo "ERROR: Feed file missing for $siteTag" >&2

I know how to return the error out back towards the parent shell.

But I have a feeling it is being tampered with, I can no longer printf the result to a nice looking variable. i.e.

error+="$( { ./childScript.sh | sed 's/Output/Useless/' 2>&4 1>&3; } 2>&1 )"
error+="$( { ./childScript.sh | sed 's/Output/Useless/' 2>&4 1>&3; } 2>&1 )"

Should essentially call the script twice, get errors from both scripts and store them in the variable error as I thought, which it does but it somehow gets rid of the lines both with the use of echo "$error" or printf "$error".

Does anyone know a solution here to manage to grab error output from several commands but maintain the separate calls to echo within the childScript.sh commands?

Edit: Output should be..

ERROR: Feed file missing for (..)
ERROR: Feed file missing for (..)
ERROR: Feed file missing for (..)

But is instead

ERROR: Feed file missing for (..) ERROR: Feed file missing for (..) ERROR: Feed file missing for (..)
Community
  • 1
  • 1
insidesin
  • 735
  • 2
  • 8
  • 26

1 Answers1

2

$(..) strips trailing line feeds. This is very useful most of the time, like in

echo "Welcome to $(hostname). Enjoy your stay."

However, in your case, it ruins it a bit. You can just add one back:

error+="$( { ./childScript.sh | sed 's/Output/Useless/' 2>&4 1>&3; } 2>&1 )"$'\n'
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • That doesn't solve it for multiple lines within one command, just multiple commands over multiple lines. :( Edit: Hang on... trying something. – insidesin Aug 05 '15 at 15:21
  • @insidesin Line feeds within a command are not stripped or modified. If you use `echo "$error" ` as you say (and not `echo $error` or otherwise missing quotes), it's fine – that other guy Aug 05 '15 at 15:23
  • Ugh. I think I know what was happening. When I migrated over to another script from my test one, I brought my changes with me and then proceeded to test on real world examples... ended up testing with more command calls than error echos, so thought it when I couldn't get back to one line per error, I was having issues. I think in the end it's a case of trying to fix all at once, somehow breaking one part fixing another, etc. Thank you. :) – insidesin Aug 05 '15 at 15:24