2016년 5월 26일 목요일

C shell program example

Following example shows looping


#!/bin/csh

set j = $1
while ( $j <= 100 )
    echo "Welcome $j times"
    @ j++     
## 또는 @ j += 3
end



Example Shell Code :  Sleep for XX minutes


#!/bin/csh

##
## How to use
##  example: sleep_minute.sh 10 (minutes)
##

set ma_value = $1

echo "I'm going to sleep for ${ma_value} minutes "

set j = 1
while ( ${j} <= ${ma_value} )
    date
    echo "${j} minutes are passed"
    @ j++
    sleep 60
end


Example : Kill process which name is "my_proc"

kill -KILL `ps -ef | grep mamm | grep -v grep | awk -F' ' '{ print $2}'`



Example : Count file number in a folder


#!/bin/csh 
set grouplist = "ABC HSS IBC IMGC SLF MRF MSS TAS"
#set CUR_DATE=`date -d "-2 min" +%Y-%m-%d-%H:%M:00`
set STD_DATE=`date  +$1-00:00:00`
set INC_DATE=`date  -d "+1 days" +$STD_DATE`

echo "$STD_DATE, $INC_DATE"

if ($#argv < 2) then
    echo "./xxx.sh day, stat_type(1MIN/5MIN/1HOUR)"
    echo "ex) ./xxx.sh 2016-03-22 5MIN"
    exit
endif

set ARG_DAY = $1
set ARG_TYPE = $2

echo "$ARG_DAY, $ARG_TYPE"

foreach gname ( $grouplist )
    printf "\n[ $gname ]\n"
    set syslist = `grep $gname /home/ina/etc/ini/ASE/svc_system.ini | grep "=" |  grep -v "#" | awk  -F "=" '{print $2}'|  awk -F "+" '{ for(i=1; i<=NF; i++) print $i }' ` ;
    foreach sname ( $syslist )
        printf "$sname  :   "
        #set count_result = `ls -al $gname/$sname/5MIN/*$CUR_DATE* | awk '{print $8}' | awk -F\. '{print $3}' | awk -F\- '{print $1"-"$2"-"$3}' | sort | uniq -c`
        set count_result = `ls -al $gname/$sname/$ARG_TYPE/*$ARG_DAY* | wc -l`
        printf "$count_result\n"

        if ($ARG_TYPE == "1MIN") then
            if ($count_result != "1440") then
                echo "1M : [$sname] : $count_result"
            endif
        else if ($ARG_TYPE == "5MIN") then
            if ($count_result != "288") then
                echo "5M : [$sname] : $count_result"
                ## for(i=0; i<288; i++) 
                set STAT_DATE=`date -d "+5 min" +$1-%H:%M:00`
                set FILE_CHK = ls -al $gname/$sname/$ARG_TYPE/*$ARG_DAY* | awk '{print $8}' | awk -F\. '{print $4}' | grep $STAT_DATE
                if($FILE_CHK == NULL) then
                echo "Not Exist : $sname.5MIN.$STAT_DATE"
            endif
        else if ($ARG_TYPE == "1HOUR") then
            if ($count_result != "24") then
                echo "1H : [$sname] : $count_result"
            endif
        endif
### 1M = 1440, 5M = 288, 1H = 24 ###

    end
end

endif




-------------------------------------------------------

if (0) then
  echo "false case"
  exit
endif
  echo "true case"