2

I have a shell script that I should preferably not be editing but that I would have to use the output of. Doing ./SomeScript.sh -d displays the output I need on the shell.

However, I get an empty file if I try to redirect the output.

./SomeScript.sh -d > temp 
cat temp

After checking Why redirecting output sometimes produces an empty file?, I have also tried echo $(./SomeScript.sh -d) but the command prints nothing.

Initially I was trying to grep a line from the output, but after realising that nothing was returned, I tried to redirect the output to a file instead and realised this problem. Any idea why this might be happening?

IceTea
  • 121
  • 6

4 Answers4

1

If ./SomeScript.sh -d > temp results in an empty file (and yet you can see the output on the screen), that means that the program is maybe printing to stderr instead of stout. So, try redirecting its stderr too -- i.e., use this:

./SomeScript.sh -d >temp 2>&1

Or you could send the outputs to two separate files:

./myScript  1>outtemp 2>errtemp 
  • This might be the solution for some others so thanks for the answer, but it's not the case here. – IceTea Mar 12 '21 at 03:08
  • Have you looked inside that script to see if it's doing some funny stuff? Can you paste it someplace where we can see it? –  Mar 12 '21 at 03:13
  • it's very long and calls other functions, but one possible cause might be that the echo commands are being passed to named pipes (tbh, I'm not familiar with named pipes) instead of being directly invoked by the script – IceTea Mar 12 '21 at 04:24
0

Use

./SomeScript.sh -d >> temp

this will append each line of the script, rather than write and overwrite line by line.

Edit: try

./SomeScript.sh -d &>> temp

if you want to redirect stderr as well as stdout.

  • Unfortunately this didn't resolve the issue – IceTea Mar 11 '21 at 04:10
  • Hmmm, it worked on my machine (famous last words, I know). Could you post the contents of SomeScript.sh perhaps, if it's not too long? – Grace Thompson Mar 11 '21 at 04:14
  • I know sharing the script would really help, but unfortunately it's really long and proprietary so I won't be able to – IceTea Mar 11 '21 at 06:11
  • 2
    If it's "proprietary", then you're in the wrong place. We can't tell you why something we can't see is not working. –  Mar 13 '21 at 19:49
0

script -c "./SomeScript.sh -flags" temp

This command worked for me but I'm not too sure why either.

IceTea
  • 121
  • 6
-1

I faced the same issue and came for this workaround:

./SomeScript.sh -d | tee temp.txt

Hope it saves you time!

  • If redirecting the standar output stream does not work, it's because the data is either delivered on the standard error stream or directly to the terminal device. Piping to tee would not help. – Kusalananda Aug 29 '22 at 13:11
  • Well, that doesn't seem to be the only reason. I have the same problem, the output can be displayed on the terminal but can't be redirected to a file and I have tried the previous answers but got nothing. Using tee solved my problem. – Mostafa Wael Aug 29 '22 at 22:43