我有以下挑战。我正在尝试使用 WebIF 从 Datalogger 读取一些数据,该脚本基本上按预期运行。但我想使用 while 循环添加检查。现在我被困在“for i in & while”组合中......
#!/bin/bash
PASSWD=blah
OUTFILE=$(date +%d).csv
OUTPATH=$(date +%Y/%m/)
LOGPATH="./test/log/"
BASEPATH="./test/"
DEVICENAME=wc-s0
NETWORK=somewhere.lan
CURLPARA="-silent --connect-timeout 5"
TRYS=3
for i in {0..5};
do
TIME="$(date +"%Y-%m-%d %H:%M:%S")";
while [[ $TRYS -ne 0 ]] ; do
VALUE=$(curl $CURLPARA "http://"$DEVICENAME"."$NETWORK"/counter?PW="$PASSWD"&"|grep counter|cut -sd ";" -f $(( i+2 ))|sed 's#kWh##g'|sed 's#kVARh##g'|sed 's#,#.#g');
rc=$?
if [[ $rc -eq 0 ]] ; then
TRYS=1
fi
((TRYS = TRYS - 1))
done
if [[ $rc -eq 0 ]] ; then
echo "Read $DEVICENAME input"$i"."
else
echo "Timeout while reading $DEVICENAME input"$i"."
fi
echo "$VALUE"
echo -n "$DEVICENAME input"$i"; "
echo "$TIME; "
done
输出如下:
Read wc-s0 input0.
wc-s0 input0; 2012-09-15 16:58:34;
Read wc-s0 input1.
wc-s0 input1; 2012-09-15 16:58:38;
Read wc-s0 input2.
wc-s0 input2; 2012-09-15 16:58:38;
Read wc-s0 input3.
wc-s0 input3; 2012-09-15 16:58:38;
Read wc-s0 input4.
wc-s0 input4; 2012-09-15 16:58:38;
Read wc-s0 input5.
wc-s0 input5; 2012-09-15 16:58:38;
“0.560”是一个 $VALUE 但我希望它是这样的:
Read wc-s0 input0.
wc-s0 input0; 2012-09-15 15:58:36; 0.560
Read wc-s0 input1.
wc-s0 input1; 2012-09-15 15:58:37; "$VALUE2"
但我不能让它以这种方式打印数据......
希望你们中的一个有提示!