1/05/2014

Bash Shell Script 기본작성

1. Bash Shell Script 기본

간단한 TEST Program을 만들거나, Service를 구동할때 자주 접하는것이 Bash Shell Script이며, 이를 이용하여 좀 더 복잡한 Program 도 작성가능하다.

이런 부분을 간략하게 정리하며, 관련된 TIP들을 정리한다.

  • Bash or Shell Script 기본 테스트 
Shell Script에 맨 위에 본인이 사용하는 Shell의 종류를 넣어주고 반드시 실행권한

$ sh -version  // 기본 Shell을 bash로 사용한다면 bash로 나옴 
// bin/sh 이 없다면, ->dash or bash Link로 기본 Shell 설정 
or
$ bash -version 

$ vi  test0.sh  // bin/sh ->dash or bash로 실행 (기본 Shell) 
#!/bin/sh
echo "Hello!! Shell Script"

$ vi  test1.sh  // /bin/bash로 실행
#!/bin/bash
echo "Hello!! Shell Script"

$ vi watch.sh  // /bin/bash로 실행 process 감시
#!/bin/bash
watch "ps aux | sort -nrk 3,3 | head -n 20"

watch
  https://linux.die.net/man/1/watch
sort
head

$ chmod +x test0.sh test1.sh  //실행권한 
$ sh ./test0.sh     // sh를 이용하여 new process 실행
$ bash ./test1.sh   // bash를 이용하여 new process 실행 
or 
$ ./test0.sh    // current process에서 실행 
$ ./test1.sh    // current process에서 실행 


Bash 시작부분
  https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/sha-bang.html


  • 고급 Bash Shell Script Guide (반드시 숙지 및 참조)
각각의 세부 Bash Shell Script Guide는 아래의사이트를 참조하자
  https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/


1.1 Bash Shell script 의 조건문 다양한 예제 

  • 일반적인 변수사용과 if문사용

$ vi iftest1.sh
#!/bin/sh
# 주석 
# bash가 지원되며 아래와 같이 맨위에 설정 
#!/bin/bash


STR1="Hello!!-ShellScript" 
STR2="Hello!!-Others"
TEST="OK"

#
# 변수의 사용법 
#
echo $STR1
echo ${STR2} 

#
## if 문 예제 STRING 
#
if [ $STR1 == $STR1 ]
then      
	echo "TEST IF ${TEST} "
elif [ $STR1 == $STR2 ]
then
	echo "TEST ELIF ${TEST}"
else
	echo "TEST ELSE ${TEST}"
fi
exit 0

  • 조건식 및 File 체크 

! EXPRESSION	The EXPRESSION is false.
-n STRING	The length of STRING is greater than zero.
-z STRING	The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2	STRING1 is equal to STRING2
STRING1 != STRING2	STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2	INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2	INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2	INTEGER1 is numerically less than INTEGER2
-d FILE	FILE exists and is a directory.
-e FILE	FILE exists.
-r FILE	FILE exists and the read permission is granted.
-s FILE	FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE	FILE exists and the write permission is granted.
-x FILE	FILE exists and the execute permission is granted.

조건문 IF 문의 EXPRESSION

[ -z ${A} ] : A 문자열의 길이가 0이면 TRUE
[ -n ${A} ] : A 문자열의 길이가 0이 아니면 TRUE
[ ${A} -eq ${B} ] : A와 B값이 같으면 TRUE
[ ${A} -ne ${B} ] : A와 B값이 다르면 TRUE
[ ${A} -gt ${B} ] : A가 B보다 크면 TRUE
[ ${A} -ge ${B} ] : A가 B보다 크거나 같으면 TRUE
[ ${A} -lt ${B} ] : A가 B보다 작으면 TRUE
[ ${A} -le ${B} ] : A가 B보다 작거나 같으면 TRUE

[ 조건식A -a 조건식B ] : 조건식 A와 B가 모두 TRUE이면 TRUE (&& 와 동일)
[ 조건식A -o 조건식B ] : 조건식 A가 TRUE거나 조건식B가 TRUE면 TRUE (|| 와 동일)

조건문 IF 문의 FILE 관련 EXPRESSION A 
[ !${A}   ] : A 파일이 디렉토리면 TRUE
[ -d ${A} ] : A 파일이 디렉토리면 TRUE
[ -e ${A} ] : A 파일이(노드, 디렉토리, 소켓 등등 모두) 존재하면 TRUE
[ -L ${A} ] : A 파일이 심볼릭 링크면 TRUE
[ -r ${A} ] : A 파일이 읽기 가능하면 TRUE
[ -s ${A} ] : A 파일의 크기가 0 보다 크면 TRUE
[ -w ${A} ] : A 파일이 쓰기 가능하면 TRUE
[ -x ${A} ] : A 파일이 실행 가능하면 TRUE
[ -c ${A} ] : A 파일이 Special character file 이면 TRUE
[ -f ${A} ] : A 파일이 디렉토리가 아닌 일반 regular 파일이면 TRUE
[ -S ${A} ] : A 파일이 소켓이면 TRUE

[ ${A} -nt ${B} ] : A 파일 B 파일보다 최신파일이면 참
[ ${A} -ot ${B} ]  : A 파일이 B 파일보다 이전파일이면 참
[ ${A} -ef ${B} ] : A 파일과 B 파일이 같은 파일이면 참

IF문 다양한예제

내부변수

특수변수


  • if 문에 and 연산과 or 연산 추가 
$ vi iftest2.sh
#!/bin/sh
# Author: Jeonghun 

ETH0_OP=`ifconfig | grep eth0 | awk '{print $1}'`
ETH1_OP=`ifconfig | grep eth1 | awk '{print $1}'`

if [ $ETH0_OP == "eth0" ] && [ $ETH1_OP == "eth1" ]
then 
	echo -e "\x1b[1;93m Both of them $ETH0_OP ,$ETH1_OP   \x1b[0m" 
elif [ $ETH0_OP == "eth0" ] || [ $ETH1_OP == "eth1" ]
then 
	echo -e "\x1b[1;93m Either of them $ETH0_OP , $ETH1_OP   \x1b[0m"
else
	echo -e "\x1b[1;93m The other $ETH0_OP , $ETH1_OP   \x1b[0m"
fi

exit 0


expr 과 문자열 조작 예제
  https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/string-manipulation.html

unary operator expected 문제 
쿼트문제 
  https://devstein.tistory.com/17


1.2 Bash 의 Function 구현 및 다양한 예제  

  • Count 증가시키는 Function 구현

$ vi test2.sh
#!/bin/bash
#   for function example 
#

count=$(( ${count}+1 ))

CNT=0

set_count()
{
     echo "CNT=$((CNT+1))"    
}

while [ $CNT -le 5 ]; do
   set_count
done


  • Sleep이용하여 Trigger Function 예제 (RANDOM사용)
$ cat sensoraging.sh 
#!/bin/bash
#
# for doing test by using GPIO,  not Senors  
# 
# $ vi ./sensorinit.sh
# echo "out" >  /sys/class/gpio/gpio388/direction
#
# $ vi /etc/rc.local 
# sudo -s /home/nvidia/deep_alpr/darknet/sensoraging.sh
#

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LOG_DIR=$HOME/deep_alpr/darknet
cd $LOG_DIR

## J21 Header 
#0 J21-PIN31 (GPIO9_MOTION) GPIO298 -> IRQ366  (FDV30IN FET, Level Shift)
#3 J21-PIN37 (GPIO26)       GPIO388 -> IRQ199  (FDV30IN FET, Level Shift) only test 

TSLEEP=0
FUNCNT=0
LOG=./log/sensoraging_$(date +%Y%m%d-%H%M-%S).log

set_trigger() {
   # for LOG
   TIME=$(date "+%Y-%m-%d %H:%M:%S")  
   echo "TIME=$TIME"   >> $1
   echo "ACTIVE HIGH"  >> $1
   echo "FUNCTION: $2 CNT $3 " >> $1
   echo "TOTAL SLEEP: $TSLEEP" >> $1
   echo "" >> $1
   
   TSLEEP=0

   # Active High
   echo 1 > /sys/class/gpio/gpio388/value
   # 1~4 second (sensor dealy)
   DELAY=`echo $(( RANDOM % (3 + 1 ) + 1 ))`
   sleep $DELAY
   echo 0 > /sys/class/gpio/gpio388/value

}

short_time() {
    # 1 ~ 9 , try count 
    FUNCNT=`echo $(( RANDOM % (8 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]
       do 
          set_trigger $1 "short_time" $FUNCNT
    
          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

          if   [ $SET_SLP -eq 0 ]; then    
             # RANDOM 1~3 second
             RSLEEP=`echo $(( RANDOM % (2 + 1 ) + 1 ))`

          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 3~5
             RSLEEP=`echo $(( RANDOM % (2 + 1 ) + 3 ))`

          else
             # RANDOM 4~14
             RSLEEP=`echo $(( RANDOM % (10 + 1 ) + 4 ))`
          fi

          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))

       done

}


long_time() {
    # 1 ~ 4 , try count 
    FUNCNT=`echo $(( RANDOM % (3 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]
       do
          set_trigger $1 "long_time" $FUNCNT

          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

          if   [ $SET_SLP -eq 0 ]; then
             # RANDOM 15~135 second
             RSLEEP=`echo $(( RANDOM % (120 + 1 ) + 15 ))`

          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 120~180
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 120 ))`

          else
             # RANDOM 180~240
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 180 ))`
          fi

          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))

       done

}

verylong_time() {
    # 1 ~ 3 , try count 
    FUNCNT=`echo $(( RANDOM % (2 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]
       do
          set_trigger $1 "verylong_time" $FUNCNT

          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

          if   [ $SET_SLP -eq 0 ]; then
             # RANDOM 200~260 second
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 200 ))`

          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 600~660
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 600 ))`

          else
             # RANDOM 900~960
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 900 ))`
          fi

          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))

       done

}

# Simple 
test_modeA() {

while [ 1 ]
  do
    set_trigger $LOG "simple" 0
    # RANDOM 1~6
    RSLEEP=`echo $(( RANDOM % (5 + 1 ) + 1 ))`
    sleep $RSLEEP
    TSLEEP=$(($TSLEEP+$RSLEEP))
  done
}


# Complex 
test_modeB() {

while [ 1 ]
  do
    # 0 ~ 2 select 
    FSEL=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

    if [ $FSEL -eq 0 ]; then
       short_time $LOG 
    elif [ $FSEL -eq 1 ]; then
       long_time $LOG 
    else
       verylong_time $LOG
    fi

  done
} 

#SET 0 for ACTIVE HIGH
echo 0 > /sys/class/gpio/gpio388/value
sleep 1

#simple
test_modeA
#complex
#test_modeB


  • Interrupt 확인 후 Count 진행 

$ cat powercheck.sh
#!/bin/bash
# for checking Power Button and Power Monitor Chip 
#
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LOG_DIR=$HOME/deep_alpr/darknet
cd $LOG_DIR

check_time() {
   time=$(date "+%Y-%m-%d %H:%M:%S")
   echo $time >> $1
   echo "" >> $1   
   return 
} 

check_powerINT() {

   EX_PINT=$PINT   
   PINT=`cat /proc/irq/380/spurious | awk 'NR == 1 {print $2}'` 
   echo "- POWER_INT=$PINT" >> $1
   echo "" >> $1

   if [ $CNT -gt 1 ]; then
       if [ $PINT -ne $EX_PINT ]; then
            time=$(date "+%Y-%m-%d %H:%M:%S")
            echo " TIME = $time " >> $2
            echo " COUNT=$CNT" >> $2
            echo " - POWER_INT= $EX_PINT , $PINT" >> $2
       fi
   fi
}

CNT=0
LOG=./log/power_$(date +%Y%m%d-%H%M-%S).log
LOG_INT=./log/power_$(date +%Y%m%d-%H%M-%S)_int.log
LOG_CHG=./log/power_$(date +%Y%m%d-%H%M-%S)_chg.log

while [ 1 ]
  do 
    echo "COUNT=$CNT" >> $LOG       
    check_time  $LOG 
    check_powerINT $LOG $LOG_INT
    CNT=$(($CNT+1))
    sleep 1
  done



  • RANDOM 사용하는 예제 

$ cat sensoraging.sh
#!/bin/bash
#
# for doing test by using GPIO,  not Senors  
# 
# $RANDOM  Shell에서 RANDOM값 제공 
# if 

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LOG_DIR=$HOME/deep_alpr/darknet
cd $LOG_DIR

## J21 Header 
#0 J21-PIN31 (GPIO9_MOTION) GPIO298 -> IRQ366  (FDV30IN FET, Level Shift)
#3 J21-PIN37 (GPIO26)       GPIO388 -> IRQ199  (FDV30IN FET, Level Shift) only test 
TSLEEP=0
FUNCNT=0
LOG=./log/sensoraging_$(date +%Y%m%d-%H%M-%S).log

set_trigger() {
   # for LOG
   TIME=$(date "+%Y-%m-%d %H:%M:%S")  
   echo "TIME=$TIME"   >> $1
   echo "ACTIVE HIGH"  >> $1
   echo "FUNCTION: $2 CNT $3 " >> $1
   echo "TOTAL SLEEP: $TSLEEP" >> $1
   echo "" >> $1
   TSLEEP=0

   # Active High
   echo 1 > /sys/class/gpio/gpio388/value
   # 1~4 second (sensor dealy)

   DELAY=`echo $(( RANDOM % (3 + 1 ) + 1 ))`
   sleep $DELAY
   echo 0 > /sys/class/gpio/gpio388/value

}

short_time() {

    # 1 ~ 9 , try count 
    FUNCNT=`echo $(( RANDOM % (8 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]
       do 
          set_trigger $1 "short_time" $FUNCNT
  
          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`
          if   [ $SET_SLP -eq 0 ]; then    
             # RANDOM 1~3 second
             RSLEEP=`echo $(( RANDOM % (2 + 1 ) + 1 ))`
          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 3~5
             RSLEEP=`echo $(( RANDOM % (2 + 1 ) + 3 ))`
          else
             # RANDOM 4~14
             RSLEEP=`echo $(( RANDOM % (10 + 1 ) + 4 ))`
          fi
          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))
       done

}

long_time() {

    # 1 ~ 4 , try count 
    FUNCNT=`echo $(( RANDOM % (3 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]

       do
          set_trigger $1 "long_time" $FUNCNT
          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

          if   [ $SET_SLP -eq 0 ]; then
             # RANDOM 15~135 second
             RSLEEP=`echo $(( RANDOM % (120 + 1 ) + 15 ))`
          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 120~180
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 120 ))`
          else
             # RANDOM 180~240
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 180 ))`
          fi

          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))
       done

}

verylong_time() {

    # 1 ~ 3 , try count 
    FUNCNT=`echo $(( RANDOM % (2 + 1 ) + 1 ))`

    while [ $FUNCNT -gt 0 ]
       do
          set_trigger $1 "verylong_time" $FUNCNT
          # 0 ~ 2 , try count 
          SET_SLP=`echo $(( RANDOM % (2 + 1 ) + 0 ))`

          if   [ $SET_SLP -eq 0 ]; then
             # RANDOM 200~260 second
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 200 ))`
          elif [ $SET_SLP -eq 1 ]; then
             # RANDOM 600~660
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 600 ))`
          else
             # RANDOM 900~960
             RSLEEP=`echo $(( RANDOM % (60 + 1 ) + 900 ))`
          fi
          TSLEEP=$(($TSLEEP+$RSLEEP))
          sleep $RSLEEP
          FUNCNT=$(($FUNCNT-1))
       done

}

# Simple 

test_modeA() {

while [ 1 ]
  do
    set_trigger $LOG "simple" 0
    # RANDOM 1~6
    RSLEEP=`echo $(( RANDOM % (5 + 1 ) + 1 ))`
    sleep $RSLEEP
    TSLEEP=$(($TSLEEP+$RSLEEP))
  done

}

test_modeB() {

while [ 1 ]
  do
    # 0 ~ 2 select 
    FSEL=`echo $(( RANDOM % (2 + 1 ) + 0 ))`
    if [ $FSEL -eq 0 ]; then
       short_time $LOG 
    elif [ $FSEL -eq 1 ]; then
       long_time $LOG 
    else
       verylong_time $LOG
    fi
  done

} 

#SET 0 for ACTIVE HIGH
echo 0 > /sys/class/gpio/gpio388/value
sleep 1

# select following one of two modes 
#Simple Mode
test_modeA

#Complex Mode
#test_modeB



  • awk 와 같이 사용하는 예제
awk를 이용하여 쉽게 각 proc와 sys의 정보를 얻어온 후 이 기반으로 이를 체크 
$ cat sensorcheck.sh
#!/bin/bash
#

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LOG_DIR=$HOME/deep_alpr/darknet
cd $LOG_DIR

check_sensorINT() {
   EX_SINT0=$SINT0   
   EX_SINT1=$SINT1  
   EX_SINT2=$SINT2  
   EX_SINT3=$SINT3  

## J21 Header 
#0 J21-PIN31 (GPIO9_MOTION) GPIO298 -> IRQ366  (FDV30IN FET , Level Shift)
#1 J21-PIN33 (GPIO11_AP)    GPIO389 -> IRQ200  (TXB0108 , Level Shift) 
#2 J21-PIN13 (GPIO_PE6)     GPIO397 -> IRQ208  (TXB0108 , Level Shift) 
#3 J21-PIN37 (GPIO26)       GPIO388 -> IRQ199  (FDV30IN FET, Level Shift) only test 

   SINT0=`cat /proc/irq/366/spurious | awk 'NR == 1 {print $2}'` 
   SINT1=`cat /proc/irq/200/spurious | awk 'NR == 1 {print $2}'` 
   SINT2=`cat /proc/irq/208/spurious | awk 'NR == 1 {print $2}'` 
   SINT3=`cat /proc/irq/199/spurious | awk 'NR == 1 {print $2}'` 
   PINVAL0=`cat /sys/class/gpio/gpio298/value`
   PINVAL1=`cat /sys/class/gpio/gpio389/value`
   PINVAL2=`cat /sys/class/gpio/gpio397/value`
   PINVAL3=`cat /sys/class/gpio/gpio388/value`

#   echo "- PIN31_INT=$SINT0" >> $1
#   echo "- PIN33_INT=$SINT1" >> $1
#   echo "- PIN13_INT=$SINT2" >> $1
#   echo "" >> $1

#### 
#$? 이전에 실행했던 return 값 
#$0 Shell Script name  
#$1 1st ARG
#$2 2nd ARG 
#$3 3rd ARG 
#

   if [ $CNT -gt 1 ]; then

       SINTDIFF0=$(($SINT0-$EX_SINT0))
       SINTDIFF1=$(($SINT1-$EX_SINT1))
       SINTDIFF2=$(($SINT2-$EX_SINT2))
       SINTDIFF3=$(($SINT3-$EX_SINT3))
       time=$(date "+%Y-%m-%d %H%M%S")

       if [ $SINT0 -ne $EX_SINT0 ]; then
            if [ $SINTCNT0 -gt 1 ]; then
               CNTDIFF0=$(($CNT-$EX_CNT0))  

            fi

            echo " TIME = $time " >> $2
            echo " PCNT31=$SINTCNT0  SEC_CNT=$CNT SEC_DIFF=$CNTDIFF0" >> $2
            echo " - PIN31_INT0= ($SINTDIFF0) $EX_SINT0 -> $SINT0" >> $2
            echo " - PIN31_VAL0= $PINVAL0" >> $2

            

            #J21-PIN31 active High

            if [ $PINVAL0 == "1"  ]; then

               echo " - INTERRUPT_PIN31=$PINTCNT0" >> $2
               echo " - TIME = $time " >> $3
               echo " - INTERRUPT_PIN31=$PINTCNT0" >> $3
               echo "" >> $3
               PINTCNT0=$(($PINTCNT0+1))
            fi

            echo "" >> $2
            SINTCNT0=$(($SINTCNT0+1))
            EX_CNT0=$CNT

       fi

       if [ $SINT1 -ne $EX_SINT1 ]; then

            if [ $SINTCNT1 -gt 1 ]; then
               CNTDIFF1=$(($CNT-$EX_CNT1))  
            fi
          
            echo " TIME = $time " >> $2
            echo " PCNT33=$SINTCNT1  SEC_CNT=$CNT SEC_DIFF=$CNTDIFF1 " >> $2
            echo " - PIN33_INT1= ($SINTDIFF1) $EX_SINT1 -> $SINT1" >> $2
            echo " - PIN33_VAL1= $PINVAL1" >> $2
            echo "" >> $2
            SINTCNT1=$(($SINTCNT1+1))
            EX_CNT1=$CNT

       fi

# 
# if -ne 
# 
       if [ $SINT2 -ne $EX_SINT2 ]; then
            if [ $SINTCNT2 -gt 1 ]; then
               CNTDIFF2=$(($CNT-$EX_CNT2))  
            fi
            echo " TIME = $time " >> $2
            echo " PCNT13=$SINTCNT2  SEC_CNT=$CNT SEC_DIFF=$CNTDIFF2 " >> $2
            echo " - PIN13_INT2= ($SINTDIFF2) $EX_SINT2 -> $SINT2" >> $2
            echo " - PIN13_VAL2= $PINVAL2" >> $2
            echo "" >> $2
            SINTCNT2=$(($SINTCNT2+1))
            EX_CNT2=$CNT

       fi

       if [ $SINT3 -ne $EX_SINT3 ]; then
            if [ $SINTCNT3 -gt 1 ]; then
               CNTDIFF3=$(($CNT-$EX_CNT3))
            fi
            echo " TIME = $time " >> $2
            echo " PCNT37=$SINTCNT3  SEC_CNT=$CNT SEC_DIFF=$CNTDIFF3 " >> $2
            echo " - PIN37_INT3= ($SINTDIFF3) $EX_SINT3 -> $SINT3" >> $2
            echo " - PIN37_VAL3= $PINVAL3" >> $2
            echo " - TIME = $time " >> $3
            echo " - 2ND_PIN37_VAL=$PINVAL3" >> $3
            echo "" >> $3

            if [ $PINVAL3 == "0"  ]; then
               echo " - INTERRUPT_PIN37=$PINTCNT3" >> $2
               PINTCNT3=$(($PINTCNT3+1))
            fi

            echo "" >> $2
            SINTCNT3=$(($SINTCNT3+1))
            EX_CNT3=$CNT

       fi

   fi

}

CNTDIFF0=0
CNTDIFF1=0
CNTDIFF2=0
CNTDIFF3=0
SINTCNT0=0
SINTCNT1=0
SINTCNT2=0
SINTCNT3=0
PINTCNT0=0
PINTCNT1=0
PINTCNT2=0
PINTCNT3=0
CNT=0
LOG=./log/sensor_$(date +%Y%m%d-%H%M-%S).log
LOG_INT=./log/sensor_$(date +%Y%m%d-%H%M-%S)_int.log
LOG_INT0=./log/sensor_$(date +%Y%m%d-%H%M-%S)_int0.log
time=$(date "+%Y-%m-%d %H:%M:%S")
echo $time >> $LOG

while [ 1 ]
  do 
    #echo "COUNT=$CNT" >> $LOG
    #time=$(date "+%Y-%m-%d %H:%M:%S")
    #echo $time >> $LOG
    #echo "" >> $LOG
    check_sensorINT $LOG $LOG_INT $LOG_INT0 
    CNT=$(($CNT+1))
    sleep 1
  done