繁体   English   中英

在macOS Shell脚本上转义空格

[英]Escaping whitespace on a macOS shell script

使用macOS内置的rsync版本(rsync版本2.6.9协议版本29)在macOS上运行以下脚本时,我遇到问题。

该脚本将一些文件和文件夹备份到我的保管箱中的特定文件夹中,并由plist macOS启动守护程序在特定时间运行。

#!/bin/bash

# Halt the script on any errors.
set -e

target_path="/Users/alex/Dropbox/backup"

# Create the target path if it doesn't exist.
mkdir -p "${target_path}"

# A list of absolute paths to backup.
things3="${HOME}/Library/Containers/com.culturedcode.ThingsMac/Data/Library/Application Support/Cultured Code/Things/Things.sqlite3"

include_paths=(
  "${HOME}/.ssh"
  "$things3"
  # [...]
)

# A list of folder names and files to exclude.
exclude_paths=(
  # [...]
)

# rsync allows you to exclude certain paths.
for item in "${exclude_paths[@]}"
do
  exclude_flags="${exclude_flags} --exclude='"${item}"'"
done

# rsync allows you to pass in a list of paths to copy.
for item in "${include_paths[@]}"
do
  include_args="${include_args} --include='"${item}"'"
done


# Finally, we just run rsync
rsync -avR --dry-run ${exclude_flags} ${include_args} ${target_path}

我遇到以下错误,为什么会出现此问题?

building file list ... rsync: link_stat "/Users/alex/Dropbox/bin/" failed: No such file or directory (2) rsync: 
link_stat "/Users/alex/bin/ --include='/Users/alex/.ssh' --include='/Users/alex/Library/Containers/com.culturedcode.ThingsMac/Data/Library/Application Support/Cultured Code/Things/Things.sqlite3'" failed: No such file or directory (2) done

sent 29 bytes  received 20 bytes  98.00 bytes/sec total size is 0  speedup is 0.00 rsync error: some files could not be transferred (code 23) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/main.c(996) [sender=2.6.9]

谢谢。

也将数组用于*_flags值。 添加到字符串中的引号不会使空格转义; 这些引号是未引号的参数扩展之后的数据的文字部分,而不是外壳语法。

for item in "${exclude_paths[@]}"; do
  exclude_flags+=(--exclude "$item")
done

for item in "${include_paths[@]}"; do
  include_flags+=(--include "$item")
done

rsync -avR --dry-run "${exclude_flags[@]}" "${include_args[@]}" "${target_path}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM