2016年7月10日 星期日

Lab: 利用SCP指令來抓取其他Sensor的資料

i@f1:~$ cat scplog.sh
#!/bin/bash
for x in t01 sensor05 <宣兩個x, 可以用來抓五個sensor>
do
        echo $x: <呼叫x>
done

<結果>
pi@f1:~$ ./scplog.sh
t01:
sensor05:

pi@f1:~$ cat scplog.sh
#!/bin/bash
for x in sensor05 sensor04
do
        echo $x:
        scp -r pi@$x:~/log ~/log_$x
done


pi@f1:~$ . scplog.sh
sensor05:
2016-07-03_10-54-32_0000.log                    0%    0     0.0KB/s   --:--           2016-07-03_10-54-32_0000.log                   52%   80KB  80.0KB/s   00:00           2016-07-03_10-54-32_0000.log                  100%  151KB 151.1KB/s   00:01         
sensor04:
2016-07-03_10-54-19_0000.log                    0%    0     0.0KB/s   --:--           2016-07-03_10-54-19_0000.log                  100%  151KB 151.2KB/s   00:00   



pi@f1:~$ cat hgetxyz.sh
#!/bin/bash
echo head 30
head -30 $1 |cut -d',' -f 1,2,3,4

echo tail 50
tail -30 $1 |cut -d',' -f 1,2,3,4  <以","為依據,取3 4 欄>

<$1 is 參數filename, >

pi@f1:~$ ./hgetxyz.sh s5log
head 30
2016-07-03 14:17:18.096,0.00054785156250,0.00013671875000,-0.00119995117187
2016-07-03 14:17:18.196,-0.00180688476562,0.00102075195312,-0.00022705078125
2016-07-03 14:17:18.296,0.00166357421875,-0.00298144531250,-0.00257177734375
2016-07-03 14:17:18.396,0.00015454101563,0.00097290039062,0.00122583007813
2016-07-03 14:17:18.496,0.00027270507813,0.00037207031250,-0.00054589843750
2016-07-03 14:17:18.596,-0.00045556640625,0.00213696289062,-0.00403442382812


========================================================================
pi@f1:~$ free -mh <Check Memery free>
             total       used       free     shared    buffers     cached
Mem:          434M       107M       327M       4.4M        10M        66M
-/+ buffers/cache:        30M       403M
Swap:          99M         0B        99M
pi@f1:~$ free -mh | grep Mem:
Mem:          434M       107M       327M       4.4M        10M        66M


pi@f1:~$ free -mh | grep Mem: <只要看Memery的>
Mem:          434M       107M       327M       4.4M        10M        66M

pi@f1:~$ free -mh | grep Mem: | fmt -u <去空白>
Mem: 434M 113M 321M 4.4M 12M 66M

pi@f1:~$ free -mh | grep Mem: | fmt -u | cut -d' ' -f 3,4 <以空白為依據,取3 4 欄>
321M

<<Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=LIST        只顯示指定的位元組
  -c, --characters=LIST   只顯示指定的字元
  -d, --delimiter=DELIM   以 DELIM 字元代替 TAB 作為欄位的分隔符號
  -f, --fields=LIST       只顯示指定的欄位;同時也印出不含分隔符號的
                            每一行,除非配合 -s 選項一起使用>>

$ nano happy.sh
#!/bin/bash
echo $0
echo $1

pi@f1:~$ ./happy.sh test
./happy.sh
test
<pi@f1:~$ ./happy.sh=$0 test=$1 $2 $$4...etc>

========================================================================

pi@f1:~$ cat hget.sh
#!/bin/bash

[ "$2" == "x" ] && pos='2'
[ "$2" == "y" ] && pos='3'
[ "$2" == "z" ] && pos='4'

head -$3 $1 | cut -d',' -f $pos

hget.sh filename x, y or z 軸 看幾筆data

pi@f1:~$ ./hget.sh slog x 10
-0.00039184570312
-0.00317431640625
0.00247192382813
....



pi@f1:~$ head -100 slog | tail -10 <去頭尾,抓91-100筆data>
2016-07-03 10:54:48.798,-0.00109228515625,0.00207202148438,-0.00021655273438,0.02820610687023,-0.16757251908397,-0.00180916030534
2016-07-03 10:54:48.855,-0.00082885742188,-0.00034497070312,-0.00089819335938,-0.00667938931298,0.05465648854961,0.02148091603053
2016-07-03 10:54:48.913,0.00055859375000,-0.00090698242187,0.00018261718750,0.02339694656489,0.02916030534351,-0.01624427480916
2016-07-03 10:54:48.970,-0.00262988281250,-0.00032006835937,0.00132153320312,-0.01912977099237,0.00525954198473,-0.02438167938931
2016-07-03 10:54:49.027,-0.00198242187500,0.00033471679688,0.00016479492187,0.00329770992366,0.00109923664122,-0.13952671755725
2016-07-03 10:54:49.084,0.00193701171875,-0.00085595703125,0.00149121093750,-0.02974045801527,0.00515267175572,0.00488549618321
2016-07-03 10:54:49.142,-0.00072534179688,0.00158789062500,0.00089013671875,0.03358778625954,0.05039694656488,-0.00512213740458
2016-07-03 10:54:49.199,0.00189843750000,0.00152978515625,0.00553100585937,-0.02367938931298,0.16738931297709,0.03446564885496
2016-07-03 10:54:49.256,-0.00298730468750,0.00281347656250,-0.00112719726563,0.04851908396947,0.14975572519083,0.02287786259542
2016-07-03 10:54:49.313,-0.00017529296875,-0.00107983398437,0.00490942382812,-0.07195419847328,-0.01506106870230,0.03915267175572

========================================================================<20160710>

2016-07-10 11:17:51.429,-0.00079760742188,-0.00181811523437,0.00048510742189,0.0                                                                             6098473282445,-0.04567938931297,0.01094656488551
2016-07-10 11:17:51.527,0.00232592773437,-0.00061181640625,-0.00099560546873,0.0                                                                             2479389312979,0.02551145038169,0.01803053435116
^CTraceback (most recent call last): <Pause/Break Hold住>
  File "udpserver-31500.py", line 14, in <module>
KeyboardInterrupt
pi@f2:~$ python udpserver-31500.pyc <不能再run python>
2016-07-10 11:18:13.227,-0.00114746093750,0.00103979492188,0.00010009765627,0.02514503816796,-0.02649618320610,-0.00702290076334
2016-07-10 11:18:13.327,0.00427099609375,-0.00095947265625,-0.00217041015623,0.02548854961834,-0.01490076335877,-0.01181679389312
2016-07-10 11:18:13.427,0.00395996093750,0.00142041015625,-0.00017382812498,-0.02041984732823,0.07106106870230,0.02712213740459
2016-07-10 11:18:13.527,-0.00140039062500,0.00150683593750,-0.00091845703123,-0.02818320610685,0.00194656488550,0.03390076335879
2016-07-10 11:18:13.627,0.00058569335937,-0.00225341796875,-0.00426855468748,0.02360305343513,-0.04029007633587,-0.02203053435113
2016-07-10 11:18:13.727,0.00040820312500,0.00221728515625,0.00067846679689,0.03146564885498,0.03880916030535,0.07345038167940
2016-07-10 11:18:13.827,-0.00057128906250,-0.00056274414062,0.00236718750002,-0.05786259541983,0.01812213740459,0.01145801526719
2016-07-10 11:18:13.927,-0.00266284179688,-0.00056250000000,0.00105346679689,-0.01245801526716,-0.03738931297709,-0.04892366412212
2016-07-10 11:18:14.027,-0.00137426757813,-0.00089746093750,-0.00007177734373,-0.00997709923662,0.07964885496184,0.01810687022902
2016-07-10 11:18:14.127,-0.00000244140625,0.00146606445313,0.00356005859377,-0.01176335877861,-0.07436641221373,-0.00686259541983
2016-07-10 11:18:14.241,-0.00203076171875,-0.00160766601562,-0.00125390624998,-0.01373282442746,0.01701526717558,-0.02551908396945
2016-07-10 11:18:14.327,-0.00383081054688,0.00154394531250,0.00333105468752,0.00656488549620,-0.02378625954198,0.02474045801528
2016-07-10 11:18:14.427,0.00041870117187,-0.00128198242187,-0.00051879882811,-0.02067938931296,-0.02874045801526,0.00209160305345
2016-07-10 11:18:14.527,0.00194580078125,-0.00213330078125,-0.00163452148436,-0.01911450381677,0.04043511450382,-0.00919083969464
2016-07-10 11:18:14.627,-0.00011596679688,-0.00053417968750,-0.00098754882811,0.01910687022903,-0.04928244274808,0.03093129770994
2016-07-10 11:18:14.727,-0.00079785156250,-0.00130029296875,0.00090625000002,0.04080152671758,-0.00109160305343,-0.00676335877861
2016-07-10 11:18:14.827,0.00193798828125,0.00142358398438,-0.00251708984373,-0.03312977099235,0.04287022900764,-0.01983206106869
2016-07-10 11:18:14.927,0.00292260742187,0.00003540039063,-0.00014086914061,0.00106870229009,-0.02904580152671,-0.05447328244273
2016-07-10 11:18:15.027,-0.00287597656250,0.00041064453125,-0.00179418945311,-0.00735114503815,0.01086259541985,-0.00489312977098
2016-07-10 11:18:15.127,0.00320825195312,-0.00087524414062,0.00015112304689,-0.00666412213739,0.02646564885497,-0.01343511450380
2016-07-10 11:18:15.227,-0.00009814453125,-0.00151171875000,0.00049121093752,-0.00761068702288,-0.05738167938931,0.00277862595421
^Z
[1]+  Stopped                 python udpserver-31500.pyc
pi@f2:~$ python udpserver-31500.pyc
Traceback (most recent call last):
  File "udpserver-31500.py", line 11, in <module>
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
pi@f2:~$ fg <回到前景>

Lab: Bash Script 進階程式設計

Bash Script 進階程式設計 -1

<@的用途參數內容, #參數總數>
pi@f1:~$ more at.sh
echo $@

pi@f1:~$ ./at.sh 1 2 3 test
1 2 3 test

pi@f1:~$ ./at.sh 4 5 6 7 8 max
4 5 6 7 8 maxmu

pi@f1:~$ more at.sh
echo $@
echo $#

pi@f1:~$ ./at.sh 1 2 3 test
1 2 3 test
4
pi@f1:~$ ./at.sh 4 5 6 7 8 max
4 5 6 7 8 max
6

pi@f1:~$ more scplog.sh
#!/bin/bash
for x in sensor05 sensor04
do
        echo $x:
        scp -r pi@$x:~/log ~/log_$x
done


pi@f1:~$ cat case.sh
for x in $@
do
        case $x in
                1)echo "1"
                ;;
                2)echo "2"
                ;;
                3)echo "3"
                ;;
                4)echo "4"
                ;;
        esac
done

pi@f1:~$ ./case.sh 1
1
pi@f1:~$ ./case.sh 2
2
pi@f1:~$ ./case.sh 3
3
pi@f1:~$ ./case.sh 4
4


pi@f1:~$ ./case.sh 1 2 3 4 5 6 7 8
1
2
3
4
pi@f1:~$


pi@f1:~/led$ more lighton.sh
#!/bin/bash
python lightoff.py
for x in $@
do
  case $x in
        1) echo $x
           python n1.py
                ;;
        2) echo $x
           python n2.py
                ;;
        3) echo $x
           python n3.py
                ;;
        4) echo $x
           python n4.py
                ;;
        5) echo $x
           python n5.py
                ;;
        6) echo $x
           python n6.py
                ;;
  esac
done


pi@f1:~/led$ cat lightoff.py
#!/usr/bin/python

import RPi.GPIO as GPIO
import time

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)

# set up GPIO output channel, we set GPIO4 (Pin 7) to OUTPUT<初始化全部>
GPIO.setup(29, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)

GPIO.output(29,GPIO.LOW)<LOW關燈>
GPIO.output(31,GPIO.LOW)
GPIO.output(32,GPIO.LOW)
GPIO.output(33,GPIO.LOW)
GPIO.output(35,GPIO.LOW)
GPIO.output(37,GPIO.LOW)

# blink GPIO4 (Pin 7) 50 times


pi@f1:~/led$ more n1.py
#!/usr/bin/python

import RPi.GPIO as GPIO
import time

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)

# set up GPIO output channel, we set GPIO4 (Pin 7) to OUTPUT
GPIO.setup(29, GPIO.OUT) <初始化一個>
#GPIO.setup(31, GPIO.OUT)
#GPIO.setup(32, GPIO.OUT)
#GPIO.setup(33, GPIO.OUT)
#GPIO.setup(35, GPIO.OUT)
#GPIO.setup(37, GPIO.OUT)


# blink GPIO4 (Pin 7) 50 times
GPIO.output(29,GPIO.HIGH)<亮燈>
pi@f1:~/led$

pi@f1:~/led$ cat splash.sh <閃五次...>
#!/bin/bash
python lightoff.py
python n1.py
python lightoff.py
python n1.py
python lightoff.py
python n1.py
python lightoff.py
python n1.py
python lightoff.py
python n1.py
python lightoff.py
python n2.py
python n3.py
python n4.py
python n5.py
python n6.py
pi@f1:~/led$

======================================================================
pi@f1:~$ cat rfile.sh
#!/bin/bash
exec < $1 #Import files
while read line
do
        echo $line # Print content line by line
done
<列印出檔案內容的script>

pi@f1:~$ ./rfile.sh mgetxyz.sh
#!/bin/bash
echo head56
tail -$(($4-$3+1)) $1 | head -$2 |cut -d',' -f 1,2,3,4

pi@f1:~$ a=$((3*3))
pi@f1:~$ echo $a
9
pi@f1:~$ a=$((0.3*0.3)) <原來bash script無法做小數點運算>
-bash: 0.3*0.3: syntax error: invalid arithmetic operator (error token is ".3*0.3")

pi@f1:~$ python -c "print(0.3*0.3)" <python可做小數點運算>
0.09

pi@f1:~$ b=$(python -c "print(0.3*0.3)")
pi@f1:~$ echo $b
0.09

======================================================================
pi@f1:~$ cat t1.sh
#!/bin/bash
if (( $1 > $2 )); then
        echo 'Yes, the first one is biger than secondary.'
        exit 0
fi
echo 'false'
exit 1

<leave 0 and 1 is important>

pi@f1:~$ ./t1.sh 1 2
false
pi@f1:~$ echo $?
1


pi@f1:~$ ./t1.sh 2 1
Yes, the first one is biger than secondary.
pi@f1:~$ echo $?
0

======================================================================
<如果直接從log檔判斷地震發生>

pi@f1:~$ cat ck-shake.sh
#!/bin/bash
filename=$3
flag=0
exec < $filename
while read line
do
 x=$(echo $line | cut -d',' -f 2)
 y=$(python -c "print($x>$1)")
 z=$(python -c "print($x<$2)")

 if [ "$y" == "True" ] && [ "$z" == "True" ]; then
  flag=$(( $flag + 1 ))
  echo $line | cut -d',' -f 1,2,3
  if (( $flag > 3 )); then
     echo "Yes!"
     exit 0
  fi
 fi
done
echo "No."
exit 1



pi@f1:~$ ./ck-shake.sh 0.5 0.6 s5log
No.
pi@f1:~$ echo $?
1

pi@f1:~$ ./ck-shake.sh 0.001 0.006 log07101
2016-07-03 15:42:11.096,0.00124829101563,-0.00001147460937
2016-07-03 15:42:11.695,0.00127758789063,0.00017724609375
2016-07-03 15:42:12.595,0.00115771484375,0.00029833984375
2016-07-03 15:42:13.296,0.00164184570313,-0.00118823242187
Yes!
==>>累積幾筆後,再去行亮燈...Alert Function Finish!


check_systemv1.1

 check_systemv1.1.bat 可用於電腦資產盤點 @echo off REM 後續命令使用的是:UTF-8編碼 chcp 65001 echo ***Thanks for your cooperation*** echo ***感謝你的合作*** timeout 1...