1

I'm trying to stock the output of a script in a variable. Here is the code of my script (delete.sh) :

#!/bin/bash

echo "Suppression de $1" >> /share/MD0_DATA/remotesync/delete.log
log=$(/share/MD0_DATA/remotesync/remoteSync -rm "$1")
echo $log >> /share/MD0_DATA/remotesync/delete.log̀

When I exec this script I got that in output :

[/share/MD0_DATA/.qpkg/remotesync] # soft/delete.sh "archivesPAO/3MONTAGE BORNE OZ 275x155.psd"
drivers : ("QMYSQL3", "QMYSQL", "QSQLITE")
Table hubicobject & hubicobjectLocal sucessfully reseted
Load container Object
" ATTENTION recuperation du prefix : archivesPAO/3MONTAGE BORNE OZ 275x155.psd"
Credentials
Refresh Token
"Upload  : 100% 0.00 octets/s fin dans : 00:00:00"
"Download  : 100% 0.00 octets/s fin dans : 00:00:00"
"Download  : 100% 0.00 octets/s fin dans : 00:00:00"
"https://lb9911.hubic.ovh.net/v1/AUTH_f5cb82ec59a615a1c56053608e0c6123"
"Download  : 100% 0.00 octets/s fin dans : 00:00:00"
"Download  : 100% 0.00 octets/s fin dans : 00:00:00"
"Temps pour inserrer 10000 entree : 0 ms"
[/share/MD0_DATA/.qpkg/remotesync] # cat soft/delete.log 

Suppression de archivesPAO/3MONTAGE BORNE OZ 275x155.psd

So I don't understand why I cannot stock this output in my shell variable. Maybe it's because I work on a QNAP QTS 4.0 ? But I don't think so.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Is `remoteSync` perhaps printing to `stderr` instead of `stdout`? `$(...)` will only grab `stdout`. Also, in the source you provided you're `echo` is going to `delete.log` with an accent over the `g`, so perhaps you are just printing to the wrong file? – Eric Renouf May 31 '16 at 12:55

2 Answers2

0

Maybe remoteSync is writing also to standard error.

Give a try to this:

#!/bin/bash
printf "Suppression de %s\n" "${1}" >> /share/MD0_DATA/remotesync/delete.log
log="$(/share/MD0_DATA/remotesync/remoteSync -rm "${1}" 2>&1)"
printf "%s\n" "${log}" >> /share/MD0_DATA/remotesync/delete.log

If the log variable is only used to append the output to the log file, try this:

#!/bin/bash
printf "Suppression de %s\n" "${1}" >> /share/MD0_DATA/remotesync/delete.log
/share/MD0_DATA/remotesync/remoteSync -rm "${1}" >> /share/MD0_DATA/remotesync/delete.log 2>&1

Both standard output and standard error are appended to the delete.log file, thanks to >> ... 2>&1.

>> ... appends standard output of the command to the file.

2>&1 instructs the shell to redirect standard error (file descriptor 2) to the standard output (file descriptor 1). The standard error is appended as well.

Jay jargot
  • 2,745
  • 1
  • 11
  • 14
0

I have used the first option #!/bin/bash printf "Suppression de %s\n" "${1}" >> /share/MD0_DATA/remotesync/delete.log log="$(/share/MD0_DATA/remotesync/remoteSync -rm "${1}" 2>&1)" printf "%s\n" "${log}" >> /share/MD0_DATA/remotesync/delete.log

And it's work as well. So remoteSync was also writing to standard error.

Thanks for your quick answers.