Bash 배열을 사용하여 옵션 추가

Bash 배열을 사용하여 옵션 추가

bash 스크립트를 사용하여 rsync명령을 호출하고 있습니다. 이라는 배열에 몇 가지 옵션을 수집하기로 결정했습니다 oser. 모든 공통 옵션을 배열에 넣는 대신 두 호출의 차이점을 살펴보고 배열에 넣는 것이 아이디어입니다.

이제 --backup 가능성을 추가하고 싶은데 rsync구현 방법이 혼란스럽습니다.

  oser=()
  (( filetr_dryrun == 1 )) && oser=(--dry-run)

  if (( filetr_dryrun == 1 )); then 

    rsync "${oser[@]}" -av --progress --log-file="$logfl" "$source" "$destin"

  elif (( filetr_exec == 1 )); then
      
    rsync "${oser[@]}" -av --progress --log-file="$logfl" "$source" "$destin"

  else

    rsync "${oser[@]}" -av --progress --log-file="$logfl" "$source" "$destin"

  fi

답변1

이건 어때:

# "always" options: you can put any whitespace in the array definition
oser=( 
    -av 
    --progress 
    --log-file="$logfl"
)

# note the `+=` below to _append_ to the array
(( filetr_dryrun == 1 )) && oser+=( --dry-run )

# now, `oser` contains all the options
rsync "${oser[@]}" "$source" "$destin"

이제 더 많은 옵션을 추가하려면 초기 oser=(...)정의에 추가하거나 일부 조건이 있는 경우 oser+=(...)배열에 추가를 사용하세요.

관련 정보