所以我对linux脚本来说相对较新,所以如果我以错误的方式解决它,请告诉我 .
我正在创建一个脚本,用于根据用户输入将来自许多不同主机的日志复制到本地计算机上 .

我写的一个功能需要使用scp . 每次在特定远程主机上使用scp命令时,都必须输入密码 . 因此,为了节省用户的时间,我想复制特定主机可能拥有的用户想要的任何文件 .

我知道我可以使用scp user @ Remoteipaddress执行此操作:'directory / file1 directory / file2'local / machine / directory

我让它运行(我感觉太多了,所以如果有更好的方式让我知道)一堆循环 .

使用scp命令的部分是我的主要问题 . 如果我引用它并回应它,代码看起来很好 . 我甚至可以复制并粘贴回显的结果,它会起作用,但如果我让脚本执行它,我会收到bash:-c:第0行:意外的EOF,同时寻找匹配的'''

编辑:$ app是在程序的另一部分创建的静态数字,添加了一些似乎缺少的东西 . 我试图从程序的多个方面拼凑而不会使它比现在更混乱

#assigns different remote host paths do array variable
until [ $scriptCounter == $app ]
do
    scpScript[$scriptCounter]="user@${ipAddress[$ipCounter]}:'"
    ((++ipCounter))
    ((++scriptCounter))
done

#$app value gets set by another function - typically 3 if that matters
scpCount=0
DayCounter=0
ipScriptCounter=0
until [ $Count == $app ]
do
    ((++scpCount))
    mkdir ~/MyDocuments/Logs/$3/app$scpCount
    echo "Creating ~/MyDocuments/Logs/${3}/app${scpCount}"

#there is one log for each day, $totalDiffDays is the total amount of days
#$DayCounter is set and gets marked up everytime it goes through loop until 
it matches total days
 until [ $DayCounter == $totalDiffDays ]
 do
    scpPath[$DayCounter]="/var/log/docker/theLog*${datePath[$DayCounter]}*"
    noSpaceSCP[$DayCounter]=${scpPath[$DayCounter]//[[:blank:]]/}
    ((++DayCounter))
 done
   fullSCPscript[$scpCount]="${scpScript[$ipScriptCounter]}${noSpaceSCP[*]}'"

   #this portion I have an issue with.
   scp ${fullSCPscript[$scpCount]} ~/MyDocuments/Logs/$3/app$scpCount

   #this ups the array counter for my ipaddress array
   ((++ipScriptCounter)) 

#How im zeroing out the $DayCounter so it will run through again for other 
nodes but with different IP address
until [ $DayCounter == "0" ]
do
    ((--DayCounter))
done
done

当我使用scp命令回显该行时,我得到的示例输出

scp user@10.10.200.100:'/var/log/docker/theLog*2018-07-26* /var/log/docker/theLog*2018-07-27*' /home/mobaxterm/MyDocuments/Logs/care3/app1

对不起,这看起来很乱,但总的来说我正在尝试构建它抓取日志的目录,如果有多天,只需添加到scp命令 . 我正在尝试这样做,而不是运行一个单独的命令来保存用户输入密码5次,如果他们需要5个文件 . 相反,他们只需要输入一次 .