将bash脚本输出保存到变量

时间:2018-10-22 20:58:26

标签: bash

我有一个bash脚本,除其他外,该脚本运行命令。对于上下文,它正在运行unison配置文件以进行文件同步。

脚本采用传递给unison函数的参数,就像这样(缩短)

#!/bin/bash
unisonparameters=$1
unisonresult=$( { unison $unisonparameters local; } )
if [ $? -ne 0 ]
then
    <<send email using $unisonresult>>
fi

我希望能够以两种方式运行脚本:

  1. 作为cronjob,在这种情况下,我传递不需要用户交互的参数(-batch,并且脚本在出现问题时通过电子邮件发送给我。

  2. 作为用户启动的操作,在这种情况下,我不传递任何参数,因此unison询问我如何处理找到的文件。在这种情况下,我不管是否发送电子邮件。

我希望上面的脚本可以工作,但是正如注释中所讨论的那样,它不起作用。有办法使它起作用吗?


其他信息1

如果没有传递任何参数,我一直在尝试让脚本要求用户输入(因此$unisonparameters = '')。这是结果

工作

如果不尝试将输出保存到变量中,就像这样:

unison $unisonparameters local
if [ $? -ne 0 ]
then
    <<send email>>
fi

,所有程序均按预期运行且没有错误:

elrudi@dell:~$ ./syncnas.sh 
Mon Oct 22 22:23:17 CEST 2018
Syncing data with nas.
Found a local device at 192.168.1.100 to connect to.
running command 'unison local'...
Contacting server...
Connected [//nas//share/HDA_DATA/elrudi -> //dell//home/elrudi]
Looking for changes
  Waiting for changes from server                                       
Reconciling changes

local          nas           
new dir  ---->            syncAll/newFolder  
new file ---->            
(...)

(第一行是由于脚本中的其他部分所致。)

损坏

但是,如果我在上面的脚本中使用原始行,则尝试存储此命令的输出:

unisonresult=$( { unison $unisonparameters local; } )
if [ $? -ne 0 ]
then
    <<send email>>
fi

,它挂在unison命令上:

elrudi@dell:~$ ./syncnas.sh 
Mon Oct 22 22:01:17 CEST 2018
Syncing data with nas.
Found a local device at 192.168.1.100 to connect to.
running command 'unison local'...
Contacting server...
Connected [//nas//share/HDA_DATA/elrudi -> //dell//home/elrudi]
Looking for changes
  Waiting for changes from server                                       
Reconciling changes
Terminated!

(最后一行由我最终的ctrl-c引起。)


其他信息2

我尝试将-debug verbose添加到unison命令中以获取更多信息。在这两种情况下,输出都非常长。有趣的部分在结尾,我在下面显示。

工作

在第一种情况下,脚本最终使用unison $unisonparameters -debug verbose local要求用户输入有关哪些文件要沿哪个方向同步:

(...)

[server: update+] Archive name is //nas//share/HDA_DATA/elrudi;//nas//share/HDA_DATA/elrudi, //xps//home/elrudi;22; hashcode is 1cee08d0fa648cd9d77332dc9623b39e
[server: remote+] Sending result (id: 16)
[server: remote+] send [unlockArchive-res] '\132\149\166\190\000\000\000\001\000\000...' 21 bytes
[server: remote+] send [rsp] '\132\149\166\190\000\000\000\001\000\000...' 21 bytes
[server: remote_emit+] dump: \016\000\000\000\246\024\000\000\000\233rsp\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@&\000\000\000\018unlockArchive-res\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@
[remote_emit+] grab: \016\000\000\000\246\024\000\000\000\233rsp\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@&\000\000\000\018unlockArchive-res\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@
[remote+] Message received (id: 16)
[remote+] receive 'rsp\132\149\166\190\000\000\000...' 24 bytes
[remote+] receive 'unlockArch...' 38 bytes
[remote+] Waiting for next message

(...)

[pred] preferpartial 'syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg' = false
[pred] forcepartial 'syncFolder/Software_Config/Qnap/3_Unison/syncnas.sh' = false
[pred] preferpartial 'syncFolder/Software_Config/Qnap/3_Unison/syncnas.sh' = false
[pred] forcepartial 'syncFolder/test2.txt' = false
[pred] preferpartial 'syncFolder/test2.txt' = false
[pred] forcepartial 'syncFolder/test4txt' = false
[pred] preferpartial 'syncFolder/test4txt' = false

local          nas           
new file ---->            syncFolder/Software_Config/Linux/variety/Downloaded/Desktoppr/02126_snowytracks_2560x1600.jpg  
new file ---->            syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg  
changed  ---->            syncFolder/Software_Config/Qnap/3_Unison/syncnas.sh  
new file ---->            syncFolder/test2.txt  
new file ---->            syncFolder/test4txt  

Proceed with propagating updates? [] 

损坏

在第二种情况下,使用unisonresult=$( { unison $unisonparameters -debug verbose local; } )的脚本(即unison命令)不显示提示:

(...)

[server: update+]   Unchanged file
[server: fileinfo+] /share/HDA_DATA/elrudi/syncFolder: true 1540240561.000000 1540240561.000000
[server: update] Setting archive for //nas//share/HDA_DATA/elrudi
[server: remote+] Sending result (id: 9)
[server: remote+] send [find-res] '\132\149\166\190\000\000\000/\000\000...' 67 bytes
[server: remote+] send [rsp] '\132\149\166\190\000\000\000\001\000\000...' 21 bytes
[remote_emit+] grab: \t\000\000\000b\024\000\000\000\233rsp\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@K\000\000\000\181find-res\132\149\166\190\000\000\000/\000\000\000\000\000\000\000 \000\000\000\029\160\176'syncFolder@\160\224\160\001\001\255\001\003\255@@\145\012\000\000\128  \223\214A@\018_j\000\000\000\000\000\000\000\000\000@@
[remote+] Message received (id: 9)
[server: remote_emit+] dump: \t\000\000\000b\024\000\000\000\233rsp\132\149\166\190\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000@K\000\000\000\181find-res\132\149\166\190\000\000\000/\000\000\000\000\000\000\000 \000\000\000\029\160\176'syncFolder@\160\224\160\001\001\255\001\003\255@@\145\012\000\000\128  \223\214A@\018_j\000\000\000\000\000\000\000\000\000@@
[remote+] receive 'rsp\132\149\166\190\000\000\000...' 24 bytes
[remote+] receive 'find-res\132\149...' 75 bytes
[remote+] Waiting for next message
Reconciling changes
[recon] reconcileAll
[recon] reconcile: 3 results
[update] Marking 0 paths equal
[pred] sortlast 'syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg' = false
[pred] sortlast 'syncFolder/test.txt' = false
[pred] sortfirst 'syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg' = false
[pred] sortfirst 'syncFolder/test.txt' = false
[sort] syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg <= syncFolder/test.txt --> -1
[pred] sortlast 'syncFolder/test.txt' = false
[pred] sortlast 'syncFolder/test2.txt' = false
[pred] sortfirst 'syncFolder/test.txt' = false
[pred] sortfirst 'syncFolder/test2.txt' = false
[sort] syncFolder/test.txt <= syncFolder/test2.txt --> -1
[pred] forcepartial 'syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg' = false
[pred] preferpartial 'syncFolder/Software_Config/Linux/variety/Downloaded/Unsplash/photo-1535941863447-76a4dc94e2ba.jpg' = false
[pred] forcepartial 'syncFolder/test.txt' = false
[pred] preferpartial 'syncFolder/test.txt' = false
[pred] forcepartial 'syncFolder/test2.txt' = false
[pred] preferpartial 'syncFolder/test2.txt' = false

(挂在最后一行)

希望有帮助!

0 个答案:

没有答案