Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使我的同步bash脚本可以通过一个特定的命令执行,如;mySync leftdir rightdir“;?_Bash_Unix_Ubuntu - Fatal编程技术网

如何使我的同步bash脚本可以通过一个特定的命令执行,如;mySync leftdir rightdir“;?

如何使我的同步bash脚本可以通过一个特定的命令执行,如;mySync leftdir rightdir“;?,bash,unix,ubuntu,Bash,Unix,Ubuntu,我正在Ubuntu中为Unix创建一个bash脚本,它同步两个目录 我已经编写了同步程序,但我面临的问题是我需要通过命令行调用我的脚本:“mySync-r leftdir righdir”或“mySync-i leftdir righdir”,我似乎无法让它工作 -r(递归)将覆盖所有重复文件夹 -i仅在用户同意的情况下才会覆盖重复文件夹 问题是如何通过命令使我的脚本可执行 还有,如果选择了“-i”命令,如何让脚本等待用户接受后再覆盖 提前谢谢你的帮助 代码是: #!/bin/bash

我正在Ubuntu中为Unix创建一个bash脚本,它同步两个目录

我已经编写了同步程序,但我面临的问题是我需要通过命令行调用我的脚本:“mySync-r leftdir righdir”或“mySync-i leftdir righdir”,我似乎无法让它工作

-r(递归)将覆盖所有重复文件夹

-i仅在用户同意的情况下才会覆盖重复文件夹

问题是如何通过命令使我的脚本可执行

还有,如果选择了“-i”命令,如何让脚本等待用户接受后再覆盖

提前谢谢你的帮助

代码是:

    #!/bin/bash

    echo "hello,the directory must be on the Desktop. "
    read -p "Please enter your username: " x3

    read -p "First directory name: " x1
    read -p "Second directory name: " x2

    dir1="/home/$x3/Desktop/$x1/"
    dir2="/home/$x3/Desktop/$x2/"

    if [ -d $dir2 ]; then
    cd "$dir1"

    find . -print0 | while read -d $'\0' file; do
    [ -e "$dir2/$file" ] || echo "$file"
    done

    cp -rupv $dir2* $dir1

    else
    echo Path Not found.. Check network status
    fi


    if [ -d $dir1 ]; then
    cd "$dir2"
    find . -print0 | while read -d $'\0' file; do
    [ -e "$dir1/$file" ] || echo "$file"
    done

    cp -rupv $dir1* $dir2

    else
    echo Path Not found.. Check network status

    fi

你能描述一下吗

如果您想通过命令使脚本可执行,请创建一个C包装器 像这样

在/usr/目录中创建一个文件夹(假设为XXX)并将sh文件复制到那里

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
  char string[512];
  strcpy(string, "");
  if(argc>=2){      //if argument exists
    for(int i=1; i<argc; i++){
        strcpy(string, strcat(strcat(string, argv[i]), " ") );
    }

            char cmd[512];
            system("cd /usr/XXX")
            strcpy(cmd, strcat("PROGRAM_NAME.sh ", string)); //note the SPACE
            system(cmd);
  }

  else{
    system("cd /usr/XXX")
    system("PRGRAM_NAME.sh");
  }
}
现在输入命令

如果命令不起作用,请修改它。。。如果其他人完全回答了这个问题,快乐就消失了(基本上,我现在没有足够的勇气去检查这个文件) 编辑

您需要通过键入source.bashrc来获取.bashrc文件的源代码首先,为什么不使用

如果您真的想自己做这件事,我建议您使用
getopt
getopts
进行选项解析。然后您可以根据这些选项控制执行

要使脚本可执行,可以使用chmod:


$chmod+xmysync

我想在没有rsync的情况下自己做,所以我会试试getopt,谢谢
export PATH="/usr/XXX:$PATH"