下面是我的设置的细节。我有以下文件:
config.json
{
"paths": ["/Users/First Folder", "/Users/Second Folder"]
}
test.sh
#!/bin/zsh
monitored_paths=$(jq -r '.paths[]' config.json)
fswatch --verbose "${monitored_paths[@]}"
JSON数组中的paths
键需要由jq
处理,然后扩展为参数。然而,当执行test.sh
时,我遇到了以下输出:
输出量:
start_monitor: Adding path: /Users/First Folder
/Users/Second Folder
我的期望是:
start_monitor: Adding path: /Users/First Folder
start_monitor: Adding path: /Users/Second Folder
总之,我的目标是监视两个文件,但似乎只监视了一个文件。如何解决此问题?
编辑:
在注解中按请求使用精确输出。
2条答案
按热度按时间kx5bkwkv1#
**注:**以下答案基于最初归因于问题的
bash
标签;标签已更改为zsh
;我不使用zsh
,但从OP的评论:while/read
循环在zsh
中工作mapfile
在zsh
中不工作正如注解中提到的,当前
monitored_paths
赋值使用单个(2行)字符串填充变量:请注意两个路径之间的嵌入式换行符(
\n
)。这整个两行结构作为一条路径被馈送到
fswatch
。将
monitored_paths
填充为数组的几个选项:while/read循环:
Map文件:
这两种方法都可以创建/填充数组:
从这里开始,OP的当前代码应该按预期运行:
mrfwxfqh2#
在shell语法从shebang你的例子