代码之家  ›  专栏  ›  技术社区  ›  Steve

Rsync命令在java中不起作用

  •  0
  • Steve  · 技术社区  · 7 年前

    以下命令直接起作用:

    rsync -rtuc --delete-after --exclude '.git*' --filter 'protect .git/**/*' ~/some/source/ ~/some/destination/
    

    但是当通过java运行时:

    private Boolean syncFiles() {
                // Success flag default to true
                Boolean success = true;
                // Attempt sync repo
                try {
                    ProcessBuilder runtimeProcessBuilder = new ProcessBuilder().inheritIO().command(new String[]{
                        "rsync", "-rtuc","--delete-after", "--exclude", "'.git*'", "--filter", "'protect .git/**/*'", "~/some/source/", "~/some/destination/"
                    });
                    // Wait until process terminates
                    int output = runtimeProcessBuilder.start().waitFor();
                    // Determine if successful
                    if (output == 0) {
                        System.out.println("Backup of " + getSource() + " to " + getDestination()
                                + " was successful");
                    } else {
                        System.out.println("Error: rsync returned error code: " + output);
                        success = false;
                    }
                } catch (Exception ex) {
                    success = false;
                    System.out.println("Error:");
                    System.out.println(ex.getMessage());
                    Logger.getLogger(Rsync.class.getName()).log(Level.SEVERE, null, ex);
                }
                return success;
        }
    

    我得到错误:

    未知的筛选规则:`protect。git/**/*“”错误:rsync返回错误 代码:1 rsync错误:排除时出现语法或用法错误(代码1)。c(902) [客户=3.1.2]

    2 回复  |  直到 7 年前
        1
  •  2
  •   slim    7 年前

    shell在将参数传递给命令之前处理引用。

    与命令行的这一部分一起使用:

     'protect .git/**/*'
    

    shell将其解释为单个参数:

     protect .git/**/*
    

    如果一开始没有单引号,那么shell将:

    • 扩展的全局字符,如“*”

    解决方案是通过:

    "protect .git/**/"
    

    "'protect .git/**/*'" .

    你可能会有类似的问题 ~

        2
  •  0
  •   Steve    7 年前

    解决方案的答案如下:

    ProcessBuilder 对象需要初始化如下:

    ProcessBuilder runtimeProcessBuilder = new ProcessBuilder().inheritIO().command(new String[]{
         "rsync", "-rtuc","--delete-after", "--filter", "protect .git", "--exclude", "'.git*'", "~/some/source/", "~/some/destination/"
    });