Bash脚本在bash文件中不起作用,但确实粘贴到命令行上

时间:2018-09-21 07:30:34

标签: bash

我不确定这是否是由于bash的更新或rsync的更新,但是运行了一段时间的脚本现在在运行时给我一个错误。

我想知道是否有人可以告诉我发生了什么变化,或者至少告诉我如果没有发生变化为何它不起作用。

这是我从脚本文件运行时遇到的错误:

--- Errors ---
unknown option -- -
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
       [-D [bind_address:]port] [-E log_file] [-e escape_char]
       [-F configfile] [-I pkcs11] [-i identity_file]
       [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
       [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
       [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
       [user@]hostname [command]
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(235) [Receiver=3.1.3]
--------------

这是脚本创建的行,如果我将其粘贴,则可以,但是在创建该行的文件中不起作用:

rsync -e 'ssh' --rsync-path='sudo rsync' -lrtz --delete --human-readable -go --usermap=deamon:wwwrun,xxx:yyy --groupmap=_www:www,staff:users --info=progress2 --exclude-from=exclude.list "xxx@tau:'/Users/xxx/Jobs/'" "/home/xxx/Jobs/PHP/Drupal/zzz/"

这是代码: 文件:./ sync-common.sh

function mySync() {

    local OPTIND
    local domain=''
    local dst=''
    local src=''
    local ssh_user=''
    local usermap=''
    local groupmap=''
    while getopts D:d:g:s:U:u: opt
    do
      case $opt in
        D) domain="$OPTARG"    ;; # ssh Domain
        d) dst="$OPTARG"       ;; # Destination
        g) groupmap="$OPTARG"  ;; # Group mapping
        s) src="$OPTARG"       ;; # Source
        U) ssh_user="$OPTARG"  ;; # ssh User
        u) usermap="$OPTARG"   ;; # User mapping
      esac
    done

    local cpu_nice_level=10
    local io_nice_class=3

    local exclude_list="--exclude-from=exclude.list"

    if [ "$ssh_user" == "root" ]; then
      local remote_opts="-e 'ssh'"
    else
      local remote_opts="-e 'ssh' --rsync-path='sudo rsync'"
    fi

    local tweaks='--delete --human-readable' # removed --acl for apple.
    local opts="$remote_opts -lrtz $tweaks"
    opts="$opts -go --usermap=$usermap --groupmap=$groupmap"

    if [ -z "$domain" ] || [ "$domain" = '127.0.0.1' ] || [ "$domain" = 'localhost' ]; then
      domain='localhost'
    else
      src="$ssh_user@$domain:'$src'"
    fi

    case $VERBALIZE in
      2) opts="$opts --info=progress2" ;;
      3) opts="$opts -i" ;;
      4) opts="$opts --progress" ;;
      5) opts="$opts -v --info=progress2" ;;
      6) opts="$opts -v -i" ;;
      7) opts="$opts -v --progress" ;;
    esac

    echo
    echo "Src: $src, Dst: $dst"

    echo "rsync $opts $exclude_list \"$src\" \"$dst\""
    local IFS=''  # To prevent shell word splitting which will cause this command to fail
    nice -n $cpu_nice_level \
        rsync $opts $exclude_list "$src" "$dst" 2>error.log

    if [ -s "error.log" ]; then
      echo
      echo '--- Errors ---'
      cat error.log
      echo '--------------'
      echo
    fi
}

if [ "$(whoami)" != 'root' ]; then
    echo 'Run as root user.'
    exit
fi

if [ -z "$DIR" ]; then
  DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi

VERBALIZE=2

文件:./sync-from-work-computer.sh

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
. ./sync-common.sh

# Local
base='/home/xxx'
b1="$base/Jobs/PHP/Drupal/yyy/"

# Remote
b2="/Users/yyy/Jobs/"

mySync -D 'tau' -U 'zzz' -s "$b2" -d "$b1" -u 'deamon:wwwrun,xxx:yyy' -g '_www:www,staff:users'

1 个答案:

答案 0 :(得分:0)

感谢卡米尔·库克(Kamil Cuk)。使用数组设法使直接命令行输入和bash文件中的脚本之间具有一致的行为。