스크립트 내의 rsync 호출에 매개변수 배열을 어떻게 추가합니까?

스크립트 내의 rsync 호출에 매개변수 배열을 어떻게 추가합니까?

일부 특정 파일을 제외하고 폴더를 다른 위치로 복사하고 싶습니다.

이것은 내 현재 스크립트입니다.

#!/bin/bash

if [ -n "$2" ]
then
    source=$(readlink -f $1)
    destination=$(readlink -f $2)
else
    printf "\nProper syntax: my_copy source_folder destination_folder\n"
        exit
fi


params=(
    --exclude='.git'
    --exclude='deploy'
    --exclude='app/config/database.php'
    --exclude='app/config/config.php'
)


cd $source
rsync " -a ${params[@]} $source/* $destination"

스크립트를 실행하면 다음 오류가 발생합니다.

rsync: link_stat "-a --exclude=.git" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1070) [sender=3.0.9]

내가 뭘 잘못했나요?

답변1

무슨 일이 일어나고 있는지 확인하려면 먼저 rsync명령을 명령으로 변경하세요 echo.

$ echo "rsync \" -a ${params[@]} $source/* $destination\""

잠재적인 수정 사항

이 줄을 다음과 같이 변경하겠습니다.

$ rsync -a "${params[@]}" "$source/"* "$destination"

답변2

다음과 같이 쓰면:

rsync " -a $params $source/* $destination"

그런 다음 rsync모든 변수가 큰따옴표로 확장되므로 명령은 단일 문자열을 인수로 가져옵니다. 예를 들어, $paramsis --exclude=.git, $sourceis /somewhere, $destinationis 인 /elsewhere경우 매개변수는 다음과 같습니다.

 -a --exclude=.git /somewhere/* /elsewhere

추가적인 문제가 있습니다: "${params[@]}"배열을 별도의 매개변수로 분할하는 것입니다. 앞의 텍스트는 ${params[@]}첫 번째 배열 요소에 추가되고 다음 텍스트 ${params[@]}는 마지막 배열 요소에 추가됩니다. 따라서 rsync4개의 매개변수를 사용하여 호출하세요.

 -a --exclude=.git
--exclude=deploy
--exclude=app/config/database.php
--exclude=app/config/config.php /somewhere/* /elsewhere

각 매개변수는 큰따옴표로 묶인 별도의 문자열이어야 합니다. 공백이나 와일드카드가 포함된 경우 변수의 확장을 보호하려면 큰따옴표가 필요합니다. unwinding 이 있는 배열의 경우 ${NAME[@]}"${NAME[@]}"요소를 별도의 인수에 배치합니다. 와일드카드로 사용되는 요소와 문자를 구분하는 공백은 인용되지 않은 상태로 유지되어야 합니다.

rsync -a "${params[@]}" -- "$source"/* "$destination"

$source여기에는 와 같이 바로 아래에 있는 도트 파일은 포함되지 않습니다 . 아래의 동일한 이름의 파일 /에 파일을 복사하려면 소스 디렉터리 경로 뒤에 슬래시를 추가하기만 하면 됩니다.$source$destination

rsync -a "${params[@]}" -- "$source/" "$destination"

관련 정보