awk 에서 shell 변수 사용하기

Posted at 2009/02/13 19:56 // in Tips/Utility programs // by Daniel
awk 에서 shell 변수 사용하기 - http://devfrog.egloos.com/293560

#!/bin/sh
if [ $# -ne 4 ]
then
    echo "Usage: log_cnt <file_name> <err_code> <log_point|0> <proc_hour>"
    exit
fi
cnt=1
if [ $3 -ne 0 ]
then
cat $1 | awk 'BEGIN{FS=";"}{ if ( $10 == '"${2}"' && $7 == '"${3}"' ) print }' | grep ^$4 | wc -l
else
while [ "$cnt" -ne 5 ]
do
    echo "log point $cnt: c"
    cat $1 | awk 'BEGIN{FS=";"}{ if ( $10 == '"${2}"' && $7 == '"${cnt}"' ) print }' | grep ^$4 | wc -l
    cnt=`expr $cnt + 1`
done
fi
-----
shell script 에서 awk 표현식 안에서 shell 의 input parameter 를 쓸 경우가 있다.
이때, awk 에서 shell 변수로 인식시키기 위해서는 ' " ${변수명} "  ' 과 같은 방식으로 사용해야 한다.

크리에이티브 커먼즈 라이센스
Creative Commons License

bash wait 명령어

Posted at 2009/02/11 11:15 // in Tips/Utility programs // by Daniel

bash 스크립트에서 백그라운드로 실행시키는 스크립트가 있을 때 그 스크립트의 종료를 기다려 처리하고 싶은 일이 있을 때가 있죠

그때 쓰는 게 wait입니다.

#!/bin/sh
./sort_db.sh &
echo "1st Line"
./bkptmp.sh &
echo "2dn line"
wait
echo "Some operation will follow this"

이런식으로 쓰면 됩니다. &로 백그라운드 실행한 스크립트를 기다려줍니다.

http://unstableme.blogspot.com/2008/06/bash-wait-command.html

Task contention상황에서 QoS를 위한 연구를 하는 중인데 스크립트로 돌리려니 이게 필요해서 찾아봤습니다.

크리에이티브 커먼즈 라이센스
Creative Commons License

shc - 셸 스크립트 컴파일러

Posted at 2008/10/30 13:59 // in Tips/Utility programs // by Daniel

image

http://www.datsi.fi.upm.es/~frosal/sources/shc.html

셸 스크립트를 컴파일할 수 있습니다. 스크립트를 그냥 텍스트형태로 쓰지 않고 바이너리 파일로 만들어서 쓰게 해줍니다.

셸 코드를 보여주고 싶지 않을 때 쓸 수 있겠지요.

사용법

[code]
$ shc -f <script.sh>
[/code]

shc를 실행하면  -f로 이름을 준 스크립트 파일을 읽어서 <스크립트이름.x.c> 파일의 C 소스코드를 만들고 그걸 컴파일 해서 스트립한 바이너리를 <스크립트이름.x>를 만들어줍니다.

image

[code]
$ ls run_tests.sh*
run_tests.sh  run_tests.sh.x  run_tests.sh.x.c
$ file run_tests.sh.x
run_tests.sh.x: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, dynamically linked (uses shared libs), stripped
[/code]

크리에이티브 커먼즈 라이센스
Creative Commons License

bash에서 for문을 c언어처럼 쓰기

Posted at 2008/04/16 15:24 // in Tips // by Daniel

#!/bin/bash
for i in `seq 1 10`;
do
echo $i
done

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done


#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done


#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done



산술계산

echo $((1+1))

echo $[1+1]


복잡한 계산은 bc를 사용한다

"echo 3/4 | bc -l"

크리에이티브 커먼즈 라이센스
Creative Commons License