눈바래다


코맨드

기능

동작설명

ksh -x script

에코우기능

실행되기전에 변수 대체가 상태로 스크립트의 줄을 출력시켜준다

ksh -v script

verbose옵션

실행되기전에 스크립트에 입력된 상태로 줄을 출력시켜준다

ksh -n script

noexec옵션

스크립트의 각줄을 해석은 해주지만 실행은 시키지 않는다

set -x

echo on

스크립트 실행 추적한다

set -o xtrace

set +x

echo off

추적기능을 해제 시킨다

typeset -ft

추적옵션 on

함수에서의 실행 추적을 한다

PS4

export PS4=$LINENO

PS4 프롬프트의 디폴트 값은 + 기호이다.

프롬프트를 리셋시킬수 있다. 여기서는 번호가 줄에 대해서 출력된다

trap DEBUG

trap 'print $LINENO' DEBUG

스크립트 각줄에 대한 $LINENO 값을 출력시킨다. 스크립트 코맨드마다 트랙 동작이 수행된다

trap ERR

trap 'print Bad Input' ERR

0 아닌 exit status 값이 되돌려지면 trap 실행된다

trap EXIT

trap 'print Exiting from $0' EXIT

스크립트나 함수가 종료하면 메시지가 출력된다

#!/bin/ksh

name="horany"

if [[ $name = [Hh]* ]] then

 print Hi $name

fi

num=1

while (( num < 5 ))

do

 (( num=num+1 ))

done

print The grand total is $num

#!/bin/ksh

trap 'print "num=$num on line $LINENO"' DEBUG

num=1

while (( num < 5 ))

do

 (( num = $num + 1 ))

done

print The grand total is $num

$LINENO 현재 스크립트의 줄수를 갖는 특수 Korn Shell 변수이다.

 

getopts 코맨드는 while 코맨드의 조건식으로 사용된다. 프로그램의 유효한 옵션들인 x y getopts 코맨드 다음에 리스트니ㅏ. 옵션들은 루프의 몸체에서 차례로 테스트된다. 그리고 이들 옵션등은 - 없는 상태로 options 차례로 지정된다. 처리할 인자가 이상 없으면 getopts 0 아닌 exit status 값을 가지고 종료한다. 그러면 while 루프도 같이 따라 종료된다.

#!/bin/ksh

while getopts xy options

do

 case $options in

  x) print "you entered -x as an option";;

  y) print "you entered -y as an option";;

 esac

done

$opts -x

$opts -y

$opts -xy

$opts -yx

$opts -a

$opts x


옵션 리스트 앞에 콜론이 옴으로 해서 Korn shell 유효하지 않은 옵션이 입력되더라도 에러 메시지를 내지 못하도록 한다. 하지만 욥션이 유효하지 못하면 ? 기호가 options 변수에 지정된다. 그리고 OPTARG 변수에 유효하지 않은 옵션이 지정된다

#!/bin/ksh

while getopts :xy options

do

 case $options in

  x) print "you entered -x as an option";;

  y) print "you entered -y as an option";;

  \?) print "$OPTARG is not a valid option" 1>&2 ;;

 esac

done

$opts2 -x

$opts2 -y

$opts2 -xy

$opts2 -yx

$opts2 xy

$opts2 -a

$opts2 -c


+d 유효한 옵션이다. +d + 같이 options 변수에 저장된다. print 코맨드에 -R 옵션을 주면 - print 스트링의 첫번째 문자로 사용될 있다.

#!/bin/ksh

while getopts :d options

do

 case $options in

  d) print -R "-d is the ON switch";;

  +d) print -R "+d is the OFF switch";;

  \?) print "$OPTARG is not a valid option";;

 esac

done

$opts3 -d

$opts3 +d

$opts3 -e

$opts3 e


getopts :x: 뒤에 : 반드시 인자가 와야 한다는 것을 의미한다

#!/bin/ksh

alias USAGE='print "Usage : opts4 [-x] filename" >$2'

while getopts :x: arguments

do

 case $arguments in

  x) print "$OPTARG is the name of the argument";;

  :) print "please enter an argument after the -x option" >&2

    USAGE;;

  \?) print "$OPTARG is not a valid option" >&2

    USAGE;;

 esac

print "$OPTIND"

done

$opts4 -x

$opts4 -x horany

$opts4 x

$opts4 x horany