我正在尝试在变量中存储脚本的输出。 这是我的脚本代码(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̀
当我执行此脚本时,我在输出中得到了它:
[/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
所以我不明白为什么我不能在shell变量中存储这个输出。 也许是因为我在QNAP QTS 4.0上工作?但我不这么认为。
答案 0 :(得分:0)
也许remoteSync
也在写标准错误。
尝试一下:
#!/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
如果log
变量仅用于将输出附加到日志文件,请尝试以下操作:
#!/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
由于delete.log
,标准输出和标准错误都会附加到>> ... 2>&1
文件中。
>> ...
将命令的标准输出附加到文件中。
2>&1
指示shell将标准错误(文件描述符2)重定向到标准输出(文件描述符1)。标准错误也会附加。
答案 1 :(得分:0)
我使用过第一个选项 #!/斌/庆典 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
它也有效。所以remoteSync也写了标准错误。
感谢您的快速回答。