我有以下shell脚本,它评估一个充满json文件的文件夹并将它们解析为一个新文件。
#!/bin/sh
# Location of Reporter-App sync folder
REPORTER_FOLDER=/Users/pirijan/Dropbox/Apps/Reporter-App
# Location of output file on public Dropbox folder
OUTPUT=/Users/pirijan/Dropbox/public/fit-report.json
cd $REPORTER_FOLDER
rm fit-report.json
rm -report.json
rm Icon^M
# get number of json files in folder, excluding 'fit-report.json'
number_reports=$(ls -l | wc -l)
echo "number of reports: $number_reports"
LS=$(ls)
echo $LS
echo
oldest_report=$(ls -t | grep -v 'fit-report.json' | tail -n1)
echo "oldest report is: $oldest_report"
#start json objects array
echo '[' >> fit-report.json
echo ""
# evaluate each iOS reporter-app report file ↴
for (( i=2; i<=$number_reports; i++ ))
do
echo "⚡︎ Parent iteration is: $i"
# set current file to evaluate in this for loop
current_file=$(ls -t | head -n $i | sed -n $i'p')
echo "current file is: $current_file"
# get number of snapshots in the current file
current_file_snapshots=$(cat $current_file | jq '.snapshots | length')
echo "number of snapshots in current file is: $current_file_snapshots"
# parse each snapshot individually in the current file
for (( ii=0; ii<$current_file_snapshots; ii++ ))
do
echo "✈︎ nested for loop at iteration $ii"
# don't append a ',' to the oldest snapshot in the last file.
if [ $current_file == $oldest_report ] && [ $ii == $((current_file_snapshots - 1 )) ]
then
echo
echo "---------------------"
echo "✿ this is the last snapshot"
cat $current_file | jq ".snapshots[$ii] | {date, responses}" >> fit-report.json
else
cat $current_file | jq ".snapshots[$ii] | {date, responses}" >> fit-report.json
echo ',' >> fit-report.json
fi
done
echo "----"
done
# end the json array
echo ']' >> fit-report.json
# copy fit-report.json to dropbox public folder ↴
cp fit-report.json $OUTPUT
rm fit-report.json
# echo on complete
echo '♥︎︎ fit-report updated'
如果我自己手动运行,输出就是预期的。
但是,我想让这个脚本自动运行,所以我在LaunchAgents文件夹中设置了一个launchd plist:
<plist version="1.0">
<dict>
<key>Label</key>
<string>FitReport</string>
<key>Program</key>
<string>/Users/pirijan/Dropbox/projects/fit-report/report.sh</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>WorkingDirectory</key>
<string>/Users/pirijan/Dropbox/projects/fit-report/</string>
<key>LowPriorityIO</key>
<false/>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>23</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
<key>disabled</key>
<true/>
</dict>
</plist>
当脚本通过launchd运行时,它返回的json文件只包含:
[]
我尝试在第一个for循环和第二个for循环中抛出占位符输出,并且似乎只评估第一个for循环。将第二个注释掉并手动运行它会产生与launchd相同的结果。
当我尝试使用http://obscuredclarity.blogspot.ca/2011/02/debugging-launchd-configuration-on-mac.html中描述的说明观察日志时,我从未看到任何记录。
你知道这可能导致什么吗?我不知道下一步该做什么。
谢谢!
答案 0 :(得分:0)
我能够使用
将stderr输出记录到文件中<key>StandardErrorPath</key>
<string>/Users/Shared/sillyscript/sillyscript_err.log</string>
(来源:http://erikslab.com/2011/02/04/logging-with-launchd/)
现在我知道问题是launchd没有正确评估jq(http://stedolan.github.io/jq/)。所以至少我现在知道什么是错的,只需要找出解决办法......