2016年7月10日 星期日

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