首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在macOS shell脚本上转义空格

在macOS shell脚本上转义空格
EN

Stack Overflow用户
提问于 2019-08-02 20:22:32
回答 1查看 46关注 0票数 0

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

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

代码语言:javascript
复制
#!/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}

我正面临以下错误,你知道为什么会发生这个问题吗?

代码语言:javascript
复制
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]

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-02 20:42:37

也可以对*_flags值使用数组。添加到字符串中的引号不会转义空格;这些引号是数据的文字部分,而不是shell语法,在无引号的参数展开之后。

代码语言:javascript
复制
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}"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57326436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档