【问题标题】:bash script for rsync ssh user input [duplicate]rsync ssh用户输入的bash脚本[重复]
【发布时间】:2017-08-31 17:58:55
【问题描述】:

我正在尝试编写一个 rsync 脚本,它将用户输入的路径带到源文件并将其发送到远程计算机上的另一个用户输入的路径。

 #!/bin/bash

echo -e "What is the directory on the local machine you want to send"

read sourcedir


SOURCEDIR = :$sourcedir

echo -e "What is the directory on the GPU box you want to send these files over to?"

read destdir

DESTDIR = :$destdir

rsync -avu -e "ssh -p xxxx" $SOURCEDIR $DESTDIR user@remote.computer --progress

当我执行脚本时,我得到以下错误

-bash: DESTDIR: command not found
building file list ...
rsync: link_stat "/path/to/script/user@remote.computer" failed: No such file or directory (2)
0 files to consider
sent 29 bytes  received 20 bytes  98.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files could not be transferred (code 23) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-51/rsync/main.c(996) [sender=2.6.9]

非常感谢您对正确编写此脚本的建议/建议。

PS。我已经从输出中删除了一些敏感信息,但请放心,格式与计算机输出的一致。

【问题讨论】:

    标签: linux bash unix ssh rsync


    【解决方案1】:

    问题

    在 bash 中,赋值只能在 = 周围不带空格的情况下工作:

    variable=value      # assign "value" to variable "variable"
    
    variable = value    # call program "variable" with arguments "=" and "value"
    

    要解决您的问题,请删除 = 周围的空格。

    更好的解决方案

    请注意,bash 中的特殊变量(如 PDWRANDOM、...)按照约定都是大写的。编写脚本时,建议使用小写变量,防止意外名称冲突。

    DESTDIRdestdir 之间的区别通过变量名不清楚。 DESTDIR 只使用一次。我认为写起来会更容易更清晰

    echo ...
    read sourcedir
    echo ...
    read destdir
    rsync ... ":$sourcedir" ":$destdir" ...
    

    【讨论】:

    • 谢谢!这种工作。我只是不知道文件被发送到哪里。
    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多