异常退出表单bash脚本

异常退出表单bash脚本,bash,Bash,如果要使用另一个脚本进行rsync备份,请键入Time Machine 但当完成第一个用户程序时停止 backup.sh #!/bin/bash

如果要使用另一个脚本进行rsync备份,请键入Time Machine

但当完成第一个用户程序时停止

backup.sh

#!/bin/bash                                                                                                                                                                                                                                                                                       
while read A; do
    echo $A
    ssh backupuser@host.backups "[[ -d /Volumes/MACBACKUP/desarrollo_backups/$A ]] || mkdir /Volumes/MACBACKUP/desarrollo_backups/$A; touch /Volumes/MACBACKUP/desarrollo_backups/$A/backup.marker"
    bash /home/vagrant/rsync_tmbackup.sh $A/ backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/$A/
done < cuentas.txt
account1  # comment first iteration on while
rsync_tmbackup: Previous backup found - doing incremental backup from backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-075246
rsync_tmbackup: Creating destination backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415
rsync_tmbackup: Starting backup...
rsync_tmbackup: From: castris
rsync_tmbackup: To:   backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415
rsync_tmbackup: Running command:
rsync_tmbackup: rsync  -e 'ssh -p 22 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' -D --compress --numeric-ids --links --hard-links --one-file-system --itemize-changes --times --recursive --perms --owner --group --log-file '/home/vagrant/.rsync_tmbackup/2016-11-03-080416.log' --link-dest='/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-075246' -- 'castris/' 'backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415/' | grep -E '^deleting|[^/]$'
Warning: Permanently added '192.168.1.33' (ECDSA) to the list of known hosts.
rsync_tmbackup: Backup completed without errors. # Here finish first iteration on while, and muts be with second iteration, but bash end while
运行backup.sh时

account1  # comment first iteration on while
rsync_tmbackup: Previous backup found - doing incremental backup from backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-075246
rsync_tmbackup: Creating destination backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415
rsync_tmbackup: Starting backup...
rsync_tmbackup: From: castris
rsync_tmbackup: To:   backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415
rsync_tmbackup: Running command:
rsync_tmbackup: rsync  -e 'ssh -p 22 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' -D --compress --numeric-ids --links --hard-links --one-file-system --itemize-changes --times --recursive --perms --owner --group --log-file '/home/vagrant/.rsync_tmbackup/2016-11-03-080416.log' --link-dest='/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-075246' -- 'castris/' 'backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/account1/2016-11-03-080415/' | grep -E '^deleting|[^/]$'
Warning: Permanently added '192.168.1.33' (ECDSA) to the list of known hosts.
rsync_tmbackup: Backup completed without errors. # Here finish first iteration on while, and muts be with second iteration, but bash end while
并停止,而是为下一个用户帐户进行备份2

您是否可以使用下面的“ssh-n”

#!/bin/bash                                                                                                                                                                                                                                                                                       
while read A; do
echo $A
ssh -n backupuser@host.backups "[[ -d /Volumes/MACBACKUP/desarrollo_backups/$A ]] || mkdir /Volumes/MACBACKUP/desarrollo_backups/$A; touch /Volumes/MACBACKUP/desarrollo_backups/$A/backup.marker" 
bash /home/vagrant/rsync_tmbackup.sh $A/ backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/$A/
done < cuentas.txt
#/bin/bash
边读边读;做
回音$A
ssh-nbackupuser@host.backups“[[-d/Volumes/MACBACKUP/desarrollo_backups/$A]]| | mkdir/Volumes/MACBACKUP/desarrollo_backups/$A;touch/Volumes/MACBACKUP/desarrollo_backups/$A/backup.marker”
bash/home/vagrant/rsync\u tmbackup.sh$A/backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/$A/
完成
文顺

-n从/dev/null重定向stdin(实际上,阻止从stdin读取)。在后台运行ssh时必须使用此选项。A. 常见的技巧是使用它在远程机器上运行X11程序。 例如,ssh-n shadows.cs.hut.fi emacs&将在shadows.cs.hut.fi上启动emacs,X11连接将自动断开 通过加密通道转发。ssh程序将被放入 背景。(这不起作用 如果ssh需要请求密码或密码短语,请参阅-f选项。)


我很确定@ MustafaDOGRU在基本问题上是正确的:当循环开始时,<代码> Read < /Cord>读取CuTasas.txt的第一行(如所期望的那样),但是在循环的中间部分读取文件的其余部分,因此当脚本试图<代码> Read < /Cord>下一行时…什么都没有了,所以它认为它已经完成并退出了。Mustafa的方法是试图找出循环中间的哪一部分正在读取,并让它停止,但看起来有很多事情在做,跟踪它们可能很烦人。但还有另一种方法:将文件内容传递给标准输入以外的内容:

#!/bin/bash                                                                                                                                                                                                                                                                                       
while read -u3 A; do
    echo $A
    ssh backupuser@host.backups "[[ -d /Volumes/MACBACKUP/desarrollo_backups/$A ]] || mkdir /Volumes/MACBACKUP/desarrollo_backups/$A; touch /Volumes/MACBACKUP/desarrollo_backups/$A/backup.marker"
    bash /home/vagrant/rsync_tmbackup.sh $A/ backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/$A/
done 3< cuentas.txt
#/bin/bash
读取时-u3 A;做
回音$A
sshbackupuser@host.backups“[[-d/Volumes/MACBACKUP/desarrollo_backups/$A]]| | mkdir/Volumes/MACBACKUP/desarrollo_backups/$A;touch/Volumes/MACBACKUP/desarrollo_backups/$A/backup.marker”
bash/home/vagrant/rsync\u tmbackup.sh$A/backupuser@host.backups:/Volumes/MACBACKUP/desarrollo_backups/$A/
完成3

3
将cuentas.txt指向文件描述符#3,而不是标准输入(即fd#0),而
read-u3
从fd#3读取,因此它们应该可以正常工作。由于FDα3通常未被使用,所以在中间循环的任何事情的机会都很小。问题是“在”运行bash程序rsync_tmbackup.sh之后,而不是在运行ssh命令之后。谢谢。@abkrim,您还可以在rsync_tmbackup.sh中使用ssh命令。你能不能在你的剧本里改变一下;rsync-e'ssh-n-p…为什么?我不喜欢运行ssh命令。我的问题是另一个。问题是关于在while上运行第一次迭代后失败的bash脚本。ssh这是一个程序行。猛击。。。这是bash脚本的另一行。@abkrim;ssh命令将消耗所有剩余的行,因此只运行first loop.Ok。好的。我放了另一个脚本。此脚本与此过程相同,但不运行bash/home/vagrant/rsync_tmbackup.sh。结果如何?当然,可以毫无问题地运行所有迭代。我有很多带循环的脚本,用于运行多个ProcessO远程机器,这是第一次出现这个问题。执行远程命令,创建远程目录,然后after@abkrim我不确定我是否理解正确,但是如果一些脚本在不需要fd3技巧的情况下工作,那是因为循环中没有读取标准输入的内容。一般情况下,在读取时,
;做完成