Shell脚本运行通过启动不评估嵌套for循环



我有下面的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文件夹中设置了一个启动列表:

<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>

当脚本运行时,它返回的json文件只包含:

[]

我试过在第一个for循环和第二个for循环中抛出占位符输出,似乎只有第一个for循环被评估。注释掉第二个for并手动运行它,会产生与启动时相同的结果。

当我尝试使用http://obscuredclarity.blogspot.ca/2011/02/debugging-launchd-configuration-on-mac.html中描述的指令观察日志时,我从未看到任何记录。

你知道这可能是什么原因吗?我不知道下一步该做什么。

谢谢!

我可以使用

将错误输出记录到文件中
<key>StandardErrorPath</key>
<string>/Users/Shared/sillyscript/sillyscript_err.log</string>

(来源:http://erikslab.com/2011/02/04/logging-with-launchd/)

现在我知道问题是jq (http://stedolan.github.io/jq/)没有被正确地评估启动。所以至少我现在知道是哪里出了问题,只需要找出解决办法。

最新更新