2

我正在尝试创建一个脚本来处理目录中的修改/新文件(这是从远程目录镜像的,lftp但这是另一回事)。

为了跟踪被修改的文件,我使用fswatch. 然后,我将检测到的文件fswatch从 xml 转换为 json 并将它们存储在单独的目录中。为了确保一旦没有更多文件要处理(当镜像作业结束时)我可以停止此转换,我会跟踪完成后由镜像过程创建的文件。

我的脚本有效,但是由于一个奇怪的原因,我在镜像作业完成之前看不到 json 文件。就好像转换后的文件存储在内存中的某个地方,一旦“停止”条件为真,这些文件就会神奇地出现在目录中。

这是正常行为吗?如何使文件在处理后立即显示?我可以通过哪些方式优化我想要实现的目标?(我是 bash 的新手......和一般的编程。)

这是我使用的脚本:

my_convert_xml_to_json_function () {
    if [ -f "$1" ]; then
        temporary_file_name_for_json=$(echo "${1/$path_to_xml_files\/}" | base64)
        xml2json < "$1" | jq -rc '.amf' > "${path_to_json_files}/${temporary_file_name_for_json}.txt"
    fi
}
export -f my_convert_xml_to_json_function
export path_to_xml_files
export path_to_json_files

# repeat watching for files until the mirroring is over
fswatch -0 --event Updated --event Created "${path_to_xml_files}" | grep -ai 'xml$' | xargs -0 -n 1 -I {} bash -c 'my_convert_xml_to_json_function "{}"' & 

temporary_pid_of_fswatch=`jobs -p`
echo "This is PID of the last bit in the pipeline: $!; this is PID of the fswatch: ${temporary_pid_of_fswatch}"


# now check for the existence of a stopping rule
while [[ $(shopt -s nullglob; set -- "${my_temporary_files}"/xml-mirrorring-started-on-*-is-completed.txt; echo $#) -eq 0 ]]; do
    # tell the script to stop and remove the file generated by the mirror into the trashcan
        sleep 1 && temp_continue_check="running `date`"
        echo "Stop condition met (${temp_continue_check})."
done && kill -15 "${temporary_pid_of_fswatch}" && mv -v "${my_temporary_files}"/xml-mirrorring-started-on-*-is-completed.txt "$my_trashcan"

编辑:所以遵循@snorp 的评论,如果我添加sync到脚本中,那么我能够“实时”更新文件。否则,文件就在空中......如果一个进程在后台运行并且我输入sync我会得到一个似乎“冻结”的新进程(根据top输出我可以看到它正在做某事,但我没有查看写入文件夹的已处理文件,就像它们应该(最终)那样)。有什么方法可以强制 OSX 将这些文件实际写入磁盘(不包括脚本中的同步)?

4

0 回答 0