1 / 48

4. Shell script & make

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 에 전달

ranit
Télécharger la présentation

4. Shell script & make

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Shell Script? • Shell(Unix shell)을 위해 쓰인 스크립트

  4. Shell? • User와 System간의 대화를 중계 • 명령어를 해석, Kernel에 전달 • Bash, sh, csh, ksh, zsh, tc Shell 등 다양한 shell이 존재

  5. Script? • 스크립트 언어(Scripting language)! • 응용소프트웨어를 제어 • 최종사용자가 응용프로그램의 동작을 사용자의 요구에 맞게 수행할 수 있도록 함

  6. Shell script! • Shell(Unix shell)을 위해 쓰인 스크립트! • 즉, Shell에서 다른 응용 소프트웨어들을 다루기 위해 사용되는 언어 • 더 확장된 의미로는 이들 명령어들을 한 파일에 모아놓아 단일 명령으로 사용할 수 있게 만들어놓은 파일

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

  8. About shell script(1) - Shebang • #! : Shebang • 모든 Shell script는 Shebang line으로 시작한다. • Shebang line 이후의 명령들을 어떻게 해석할 것인지 지정

  9. About shell script(2) - Shebang • Shebang line의 구문(Syntax) • #!interpreter [optional-arg] • Ex) #!/bin/sh #!/bin/csh –f #!/usr/bin/perl -T

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

  11. 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: 현재 경로

  12. About shell script(5) – Hello world! • $vi hello • #!/bin/sh • echo “hello world!”

  13. About shell script(6) – Hello world! • 실행해봅시다 • ./hello • 안 되죠?

  14. About shell script(7) – Hello world! • Linux에서 기본적으로 파일은 실행권한이 없음 • 실행권한 부여 // $chmod +x hello • $./hello • 혹은 sh hello로 실행

  15. About shell script(8) – 변수선언 • 변수 선언 • name=argon • age=19 • =양쪽으로 공백이 있으면 안 된다. • 변수 호출 • 변수를 호출할 때는 $를 사용

  16. About shell script(9) – 변수입력 • 변수 입력 받기 • read [변수명]

  17. About shell script(10) – 특수 변수 • 특수한 변수 • $$ : Shell 자신의 PID • $! : Shell이 마지막에 실행한 background process PID • $* : 인수 전체의 list 하나의 달라붙어있는 형태 • $@ : 인수 전체의 list, 각각 “”로 묶여서 전개 • # : Shell에 부여된 인수의 개수 • $0 : Shell 자신의 file명 • $1 ~ $n : Shell에 부여된 인수의 값(순서대로) • $? : 이전에 수행한 명령이 성공했는가? (성공 0)

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

  19. About shell script(12) – 연산자 • C언어와 마찬가지로 사칙연산이 가능 + - / \* %

  20. About shell script(13) – 조건문if if[condition1] then commands elif [condition2] then commands else commands fi//if문의 끝

  21. About shell script(14) – 조건문if if[condition1]; then commands elif [condition2]; then commands else commands fi //if문의 끝

  22. About shell script(15) – case case$변수in 0) commands;; 1) commands;; esac //case문의 종결

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

  24. 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: <

  25. About shell script(18) – 반복문 while[Condition] do commands done until[Condition] do commands done for변수in [list] do commands done for((i=0; I < 10; i++)) do commands done

  26. About shell script(19) – 함수 functionname() { commands } 호출은 functionname

  27. About shell script(20) – 배열 • declare –a array (크기 지정 없이 배열을 선언) • array[1]=k 와 같이 접근, • 호출은 변수와 마찬가지로 ${array[1]}

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

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

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

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

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

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

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

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

  36. Makefile Syntax • Makefile도 결국 Shell Script와 유사한 syntax • Command 전에는 항상 TAB • #으로 시작하는 행은 주석 • 이외의 자세한 문법은 • http://www.gnu.org/software/make/manual/make.html

  37. make&Makefile • make의 목표 • 가장 적은 단계를 거쳐 파일 생성 • Makefile에 파일 생성을 위해 필요한 파일들에 대해 기록해둔다. • 파일 생성을 위해 필요한 파일들 중 변경된 파일이 있는지 Timestamp를 가지고 추적.

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

  39. MakefileExample • GcdLcm.c • GcdLcm.h

  40. MakefileExample • main.c

  41. Makefile Example • Makefile

  42. Makefile Example • make • 한 번 더?

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

  44. Clock skew Make: warning: Clock skew detected. Your build may be incomlete • 시스템의 시간과 컴파일 하고자 하는 소스 파일의 시간이 맞지 않음 • Ex) 시스템의 시간보다 미래에 저장된 소스파일 • 해결방법 • 소스 파일들의 access와 modification 시간을 현재시간으로 업데이트

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

  46. 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/Parallel.htmlhttp://blog.naver.com/juner84?Redirect=Log&logNo=100129369041 • http://jacking75.cafe24.com/Boost/graph/file_dependency_example.html

  47. Citation • 2011 Wheel Seminar - 08. GCC and Shell Script정창제 선배님 • 2012 Wheel Seminar - 08. 쉘스크립트박지민 회원

  48. 수고하셨습니다.

More Related