48
4. Shell script & make SPARCS 13 KAIST CS 12 argon

4. Shell script & make

  • Upload
    ranit

  • View
    166

  • Download
    0

Embed Size (px)

DESCRIPTION

SPARCS 13 KAIST CS 12 argon. 4. Shell script & make . Contents – Shell script. Shell script? Shebang PATH 명령어 변수 인수전달 조건문 반복문 함수 배열 [ 실습 ] Cron 만들기. Shell Script?. Shell(Unix shell) 을 위해 쓰인 스크립트. Shell?. User 와 System 간의 대화를 중계 명령어를 해석 , Kernel 에 전달 - PowerPoint PPT Presentation

Citation preview

Page 1: 4. Shell script & make

4. Shell script & make

SPARCS 13KAIST CS 12argon

Page 2: 4. Shell script & make

Contents – Shell script Shell script? Shebang PATH 명령어 변수 인수전달 조건문 반복문 함수 배열 [ 실습 ] Cron 만들기

Page 3: 4. Shell script & make

Shell Script?

Shell(Unix shell) 을 위해 쓰인 스크립트

Page 4: 4. Shell script & make

Shell?

User 와 System 간의 대화를 중계 명령어를 해석 , Kernel 에 전달 Bash, sh, csh, ksh, zsh, tc Shell 등 다양한

shell 이 존재

Page 5: 4. Shell script & make

Script?

스크립트 언어 (Scripting language)!

응용소프트웨어를 제어 최종사용자가 응용프로그램의 동작을 사용자의 요구에 맞게 수행할 수 있도록 함

Page 6: 4. Shell script & make

Shell script!

Shell(Unix shell) 을 위해 쓰인 스크립트 !

즉 , Shell 에서 다른 응용 소프트웨어들을 다루기 위해 사용되는 언어 더 확장된 의미로는 이들 명령어들을 한 파일에 모아놓아 단일 명령으로 사용할 수 있게 만들어놓은 파일

Page 7: 4. Shell script & make

Why Shell script?

In wiki – 컴파일 단계가 없으므로 실행이 다른 언어에 비해 빠른 경우가 많다 동일한 작업의 반복 / 여러 명령어들의 입력을 통한 복잡한 작업을 한 파일에 모아 단일 명령으로 사용 !

Page 8: 4. Shell script & make

About shell script(1) - Shebang

#! : Shebang

모든 Shell script 는 Shebang line 으로 시작한다 .

Shebang line 이후의 명령들을 어떻게 해석할 것인지 지정

Page 9: 4. Shell script & make

About shell script(2) - Shebang Shebang line 의 구문 (Syntax) #!interpreter [optional-arg]

#!/bin/sh#!/bin/csh –f#!/usr/bin/perl -T

• Ex)

Page 10: 4. Shell script & make

About shell script(3) - $PATH( 환경변수 )

/usr/bin/env 는 사용자의 $PATH(환경변수 ) 상에서 첫 번째로 찾은 해당 명령을 실행 ex) #!/usr/bin/env python 환경변수 상에 기재된 파이선 PATH 중 가장 먼저 오는 것을 실행

Page 11: 4. Shell script & make

About shell script(4) – 명령어 Bash shell 기준 , 자주 쓰이는 명령어

$ echo [str]: 뒤에 전달되는 str 을 출력 $ grep [str] [file]: file 안에서 str 을 찾는다 . $ file [filename]: filename 의 filetype 을 출력 $ read var: 입력값을 변수 var 에 대입 $ tee [filename]: 표준출력을 file 에 쓰기 $ basename [file]: 디렉토리명을 제외한 파일명 $ dirname [file]: 파일이름 제외한 디렉토리명 $ pwd: 현재 경로

Page 12: 4. Shell script & make

About shell script(5) – Hello world!

$vi hello #!/bin/sh echo “hello world!”

Page 13: 4. Shell script & make

About shell script(6) – Hello world!

실행해봅시다 ./hello 안 되죠 ?

Page 14: 4. Shell script & make

About shell script(7) – Hello world!

• Linux 에서 기본적으로 파일은 실행권한이 없음 실행권한 부여 // $chmod +x hello $./hello

• 혹은 sh hello 로 실행

Page 15: 4. Shell script & make

About shell script(8) – 변수선언 변수 선언

name=argon age=19 = 양쪽으로 공백이 있으면 안 된다 .

변수 호출 변수를 호출할 때는 $ 를 사용

Page 16: 4. Shell script & make

About shell script(9) – 변수입력 변수 입력 받기

read [ 변수명 ]

Page 17: 4. Shell script & make

About shell script(10) – 특수 변수 특수한 변수

$$ : Shell 자신의 PID $! : Shell 이 마지막에 실행한 background

process PID $* : 인수 전체의 list 하나의 달라붙어있는 형태 $@ : 인수 전체의 list, 각각 “”로 묶여서 전개 # : Shell 에 부여된 인수의 개수 $0 : Shell 자신의 file 명 $1 ~ $n : Shell 에 부여된 인수의 값 ( 순서대로 ) $? : 이전에 수행한 명령이 성공했는가 ? ( 성공 0)

Page 18: 4. Shell script & make

About shell script(11) – 인수 전달 Shell 에 인수 전달하기

Page 19: 4. Shell script & make

About shell script(12) – 연산자 C 언어와 마찬가지로 사칙연산이 가능

+-/\*

%

Page 20: 4. Shell script & make

About shell script(13) – 조건문 if

if [condition1]then

commandselif [condition2]then

commandselse

commands fi //if 문의 끝

Page 21: 4. Shell script & make

About shell script(14) – 조건문 if

if [condition1]; thencommands

elif [condition2]; thencommands

elsecommands

fi //if 문의 끝

Page 22: 4. Shell script & make

About shell script(15) – casecase $ 변수 in

0) commands;;1) commands;;

esac //case 문의 종결

Page 23: 4. Shell script & make

About shell script(16) – 제어문 &&: 앞의 조건식이 참일 때 실행 ||: 앞의 조건식이 거짓일 때 실행

Page 24: 4. Shell script & make

About shell script(17) – 조건식 조건식은 [ ] 로 묶임 파일식 : 파일의 속성 검사

[ -f ”file” ]: file 이 파일인지를 테스트 [ -x “/usr/games/sl” ]: /usr/games/sl 이 실행파일인지 아닌지 테스트

문자열식 : 문자열을 검사 [ -z “$var” ]: $var 의 문자열의 길이기 0 인지 테스트 [ -n “$var” ]: $var 의 문자열 길이가 0 인지 아닌지 테스트 [ “$a” = “$b” ]: $a 와 $b 가 같은가 테스트

숫자 비교 연산자 : -eq: == -ne: != -ge: >= -gt: > -le: <= -lt: <

Page 25: 4. Shell script & make

About shell script(18) – 반복문

until [Condition]do

commandsdone

while [Condition]do

commandsdone

for 변수 in [list]do

commandsdone

for ((i=0; I < 10; i++))do

commandsdone

Page 26: 4. Shell script & make

About shell script(19) – 함수

호출은 functionname

functionname(){commands}

Page 27: 4. Shell script & make

About shell script(20) – 배열 declare –a array ( 크기 지정 없이 배열을 선언 ) array[1]=k 와 같이 접근 ,

호출은 변수와 마찬가지로 ${array[1]}

Page 28: 4. Shell script & make

실전 ! – Cron 만들기Cron?정해진 시간마다 정해진 작업을 하는 Unix 의 background process 의 명칭

Page 29: 4. Shell script & make

실전 ! – Cron 만들기바로 이렇게 !

Page 30: 4. Shell script & make

실전 ! – Cron 만들기 일단 , 아래와 같이 시작합니다 .

Page 31: 4. Shell script & make

실전 ! – Cron 만들기 전달받은 인수의 개수가 올바른지 확인 올바르지 않은 경우 Usage 를 출력한 뒤

exit

Page 32: 4. Shell script & make

실전 ! – Cron 만들기 서버로부터 시간 정보 가져오기

Page 33: 4. Shell script & make

실전 ! – Cron 만들기 이제부터 시작 ! Hint: 서버로부터 가져온 시간과 현재 시간이 일치하는가 확인 아직 시간이 남았다면 sleep 을 사용합시다 .

실행에는 . ${ 변수명 } 을 사용

Page 34: 4. Shell script & make

Contents – make make? Makefile syntax Example Parallel build Clock skew dependency

Page 35: 4. Shell script & make

make?

linux 에서 제공하는 하나의 유틸리티 현재 디렉토리에 있는 Makefile 또는 makefile 이란 일정 규칙하에 만들어진 file 의 내용을 읽어 목표 file 을 생성

Page 36: 4. Shell script & make

Makefile Syntax

Makefile 도 결국 Shell Script 와 유사한 syntax

Command 전에는 항상 TAB # 으로 시작하는 행은 주석 이외의 자세한 문법은

http://www.gnu.org/software/make/manual/make.html

Page 37: 4. Shell script & make

make & Makefile

make 의 목표 가장 적은 단계를 거쳐 파일 생성

• Makefile 에 파일 생성을 위해 필요한 파일들에 대해 기록해둔다 .

파일 생성을 위해 필요한 파일들 중 변경된 파일이 있는지 Timestamp 를 가지고 추적 .

Page 38: 4. Shell script & make

Makefile Example

GcdLcm: main: main 함수를 포함 GcdLcm.c: GCD, LCM 함수 정의 GcdLcm.h: GCD, LCM 함수 선언

Page 39: 4. Shell script & make

Makefile Example

GcdLcm.h • GcdLcm.c

Page 40: 4. Shell script & make

Makefile Example

main.c

Page 41: 4. Shell script & make

Makefile Example

Makefile

Page 42: 4. Shell script & make

Makefile Example make

한 번 더 ?

Page 43: 4. Shell script & make

Parallel build

일반적으로 make 는 한 번에 한 file 을 생성 -j 혹은 – jobs 옵션을 통해 동시에 여러 파일을 생성하도록 할 수 있음 . Ex) –j4 : 4 개의 프로세스를 생성하여 make

( 즉 동시에 4 개의 작업을 수행 )

Page 44: 4. Shell script & make

Clock skew

Make: warning: Clock skew detected. Your bui ld may be incomlete

시스템의 시간과 컴파일 하고자 하는 소스 파일의 시간이 맞지 않음 Ex) 시스템의 시간보다 미래에 저장된 소스파일

해결방법 소스 파일들의 access 와 modification 시간을 현재시간으로 업데이트

Page 45: 4. Shell script & make

dependency

main.cGcdL-cm.c

GcdL-cm.h

main.oGcdL-cm.o

GCDLCM

어떠한 차례로 컴파일 해야 할 것인가 ? 병렬 컴파일이 가능한 파일들은 무엇인가 ? 어떤 파일이 변경된다면 얼마만큼의 재 컴파일이 필요한가 ? 순환참조가 있는가 ?

Page 46: 4. Shell script & make

References http://ingorae.tistory.com http://smeffect.tistory.com• http://www.gnu.org/software/make/manual/make.html• http://ko.wikipedia.org /wiki/%EC%8A%A4%ED%81%AC%EB

%A6%BD%ED%8A%B8_%EC%96%B8%EC%96%B4• http://kukuta.tistory.com• http://kyagami.blog.me• http://cafe.naver.com/daousw/489• http://ingorae.tistory.com• http://www.gnu.org/software/make/manual/html_node/Paral-

lel.htmlhttp://blog.naver.com/juner84?Redirect=Log&logNo=100129369041

• http://jacking75.cafe24.com/Boost/graph/file_dependency_example.html

Page 47: 4. Shell script & make

Citation

•  2011 Wheel Seminar - 08. GCC and Shell Script 정창제 선배님

• 2012 Wheel Seminar - 08. 쉘 스크립트 박지민 회원

Page 48: 4. Shell script & make

수고하셨습니다 .