在 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
值也使用数组。您添加到字符串中的引号不会转义空格;在未加引号的参数扩展之后,这些引号是数据的文字部分,而不是 shell 语法。
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}"
当 运行 使用 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
值也使用数组。您添加到字符串中的引号不会转义空格;在未加引号的参数扩展之后,这些引号是数据的文字部分,而不是 shell 语法。
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}"