본문 바로가기
IT일반/Linux

Command Line - 문제 해결

by 버섯도리 2022. 6. 6.
[cmd_test@btjeon-naver ~]$ vi trouble.sh
-------------------------------------------------------------------------------------------
#!/bin/bash -x

# trouble: script to demonstrate common erros

number=1

if [ $number = 1 ]; then
        echo "Number is equal to 1."
else
        echo "Number is not equal to 1."
fi
-------------------------------------------------------------------------------------------
[cmd_test@btjeon-naver ~]$ ./trouble.sh 
+ number=1
+ '[' 1 = 1 ']'
+ echo 'Number is equal to 1.'
Number is equal to 1.

bash의 -x 옵션으로 트레이싱(tracing)을 활성화할 수 있다.

+ 기호는 트레이스 출력의 기본 문자다. 그것은 PS4(프롬프트 문자열 4) 쉘 변수에 포함된다.

 

[cmd_test@btjeon-naver ~]$ export PS4='$LINENO + '
[cmd_test@btjeon-naver ~]$ ./trouble.sh 
5 + number=1
7 + '[' 1 = 1 ']'
8 + echo 'Number is equal to 1.'
Number is equal to 1.

트레이스가 실행되는 곳의 행 번호를 포함하기 위해 PS4 수정 후 실행

 

[cmd_test@btjeon-naver ~]$ vi trouble.sh
-------------------------------------------------------------------------------------------
#!/bin/bash -x

# trouble: script to demonstrate common erros

number=1

set -x # Turn on tracing
if [ $number = 1 ]; then
        echo "Number is equal to 1."
else
        echo "Number is not equal to 1."
fi
set +x # Turn off tracing
-------------------------------------------------------------------------------------------

set 명령어의 -x 옵션으로도 트레이싱(tracing)을 활성화할 수 있다.

 

 

 

 

 

 

출처 : 리눅스 커맨드라인 완벽 입문서