Unix Shell Script 2003/03/24  For SPARCS ’03 김군훈   <airlover@sparcs.kaist>
Index <ul><li>What is shell? </li></ul><ul><li>Shell configuration & Environment variable. </li></ul><ul><li>Standard inpu...
What is shell? <ul><li>Command / Interpreter </li></ul><ul><ul><li>Bourn Shell </li></ul></ul><ul><ul><li>C Shell </li></u...
Bash Configuration <ul><li>.bash_login : Login initialization like .login </li></ul><ul><li>.bash_logout : Logout actions ...
Environment Variable. <ul><li>PWD </li></ul><ul><ul><li>Current working directory. </li></ul></ul><ul><li>PATH </li></ul><...
Standard input, output, error Program1 FD Program2 FD 1 - STDOUT 0 - STDIN 2 – STDERR :  :  : 1 - STDOUT 0 - STDIN 2 – STD...
Redirection & Pipelining < | > Etc.. >>  <<  <<<  2>&1  2>  &>   … File File Program Program
Script Examples - Start #!/bin/sh mutt telnet ara telnet ska telnet pie
Script Examples(2) – Color Table #!/usr/local/bin/bash esc=&quot;�33[&quot; echo -e &quot;  40  41  42  43  44  45  46  47...
Script Examples(3) – Dictionary $ sh eng_dic shell shell  [LINK]  발음듣기 [INLINE] shell [ [INLINE]  雩 ] n. 1 a  조가비 (seashel...
Script Examples(4) – Etc. <ul><li>Automatic install program. </li></ul><ul><li>Homework submit program. </li></ul><ul><ul>...
Shell script syntax - for <ul><li>for name [ in word ] ; do list ; done </li></ul>102 Name: sparcs.kaist.ac.kr 103 Name: a...
Shell script syntax(2) - if <ul><li>if list; then list; [ elif list; then list; ] ... [ else list; ] fi </li></ul>$ ./diff...
Shell script syntax(3) – select <ul><li>select name [ in word ] ; do list ; done </li></ul>1) Hello 2) Quit #? 1 Hello Wor...
Shell script syntax(4) - case <ul><li>case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac </li></ul>#!/bin/s...
Shell script syntax(5) – Etc. <ul><li>while list; do list; done </li></ul><ul><li>until list; do list; done </li></ul><ul>...
Special Parameters <ul><li>0 : Name of the shell or shell script </li></ul><ul><li>$ : Process ID of the this shell </li><...
Making Shell <ul><li>2003 SP Seminar on summer vacation </li></ul><ul><ul><li>For studying system programming. </li></ul><...
Homework <ul><li>airlover 라는 폴더를 만든다 . </li></ul><ul><li>위에서 만든 폴더에 이름이  SPARCS 03 학번 회원들의 아이디인 파일을 만들고 그 회원이 마지막으로 접속한 시각...
Reference <ul><li>http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/index.html </li></ul><ul><li>Sh/Bash/Csh/Tcsh man pages </...
Upcoming SlideShare
Loading in...5
×

Airlover 20030324 1

887

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
887
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Airlover 20030324 1

  1. 1. Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈 <airlover@sparcs.kaist>
  2. 2. Index <ul><li>What is shell? </li></ul><ul><li>Shell configuration & Environment variable. </li></ul><ul><li>Standard input/output/error, Redirection, Pipelining. </li></ul><ul><li>Shell script examples. </li></ul><ul><li>Shell script syntax. </li></ul><ul><li>Special parameter </li></ul><ul><li>Making shell. </li></ul><ul><li>Homework. </li></ul>
  3. 3. What is shell? <ul><li>Command / Interpreter </li></ul><ul><ul><li>Bourn Shell </li></ul></ul><ul><ul><li>C Shell </li></ul></ul><ul><ul><li>Korn Shell </li></ul></ul><ul><ul><li>Bash </li></ul></ul><ul><ul><li>Tcsh </li></ul></ul>Kernel Shell Program Program Program User
  4. 4. Bash Configuration <ul><li>.bash_login : Login initialization like .login </li></ul><ul><li>.bash_logout : Logout actions </li></ul><ul><li>.bashrc : Shell configuration </li></ul><ul><li>.bash_profile : Login shell configuration </li></ul><ul><li>.inputrc : Individual readline initialization file </li></ul><ul><li>.bash_history : Command history file </li></ul>
  5. 5. Environment Variable. <ul><li>PWD </li></ul><ul><ul><li>Current working directory. </li></ul></ul><ul><li>PATH </li></ul><ul><ul><li>The search path. </li></ul></ul><ul><li>PS1 </li></ul><ul><ul><li>The primary prompt string. </li></ul></ul><ul><li>MAIL </li></ul><ul><ul><li>Filename to check mail. </li></ul></ul>Shell Var. Environ. Var. export - set - env
  6. 6. Standard input, output, error Program1 FD Program2 FD 1 - STDOUT 0 - STDIN 2 – STDERR : : : 1 - STDOUT 0 - STDIN 2 – STDERR : : :
  7. 7. Redirection & Pipelining < | > Etc.. >> << <<< 2>&1 2> &> … File File Program Program
  8. 8. Script Examples - Start #!/bin/sh mutt telnet ara telnet ska telnet pie
  9. 9. Script Examples(2) – Color Table #!/usr/local/bin/bash esc=&quot;33[&quot; echo -e &quot; 40 41 42 43 44 45 46 47&quot; for fore in 30 31 32 33 34 35 36 37; do line1=&quot;$fore &quot; line2=&quot; &quot; for back in 40 41 42 43 44 45 46 47; do line1=&quot;${line1}${esc}${back};${fore}m 보통 ${esc}0m &quot; line2=&quot;${line2}${esc}${back};${fore};1m 밝은 ${esc}0m &quot; done echo -e &quot;$line1 $line2&quot; done
  10. 10. Script Examples(3) – Dictionary $ sh eng_dic shell shell [LINK] 발음듣기 [INLINE] shell [ [INLINE] 雩 ] n. 1 a 조가비 (seashell); ( 굴의 ) 껍질 b ( 거북 [INLINE] 새우 [INLINE] 게 등의 ) 등딱지 , 껍데기 c ( 콩의 ) 깍지 , 꼬투리 d 시초 ( 翅 ?), 딱지 날개 e ( 번데기의 ) 외피 f ( 과일 [INLINE] 종자 등의 ) 껍질 ; [ pl. ] 카카오 껍질 (cacao shells) #!/bin/sh WORD=$* WORD=$(echo ${WORD} |od -tx1 -w1000 |head -1 |sed -e 's/^[0-9]+ //' -e 's/ 0a$//' -e 's/20/+/g' -e 's/ /%/g' -e 's/^/%/' |tr '[a-z]' '[A-Z]') lynx -nolist -verbose -dump http://kr.engdic.yahoo.com/result.html?p=${WORD} |grep -v &quot;bu2.gif&quot; |tail +14 |tac |tail +11 |tac |sed -e 's/[phon[0-9]+.gif] //g' -e 's/[phon[0-9]+.gif]//g' |less -r
  11. 11. Script Examples(4) – Etc. <ul><li>Automatic install program. </li></ul><ul><li>Homework submit program. </li></ul><ul><ul><li>Homework num., source file </li></ul></ul><ul><ul><li>Delay due </li></ul></ul><ul><li>Homework marking program. </li></ul><ul><ul><li>Compare optimized solution </li></ul></ul><ul><ul><li>Check execution time. </li></ul></ul><ul><li>Check empty IP address. </li></ul>
  12. 12. Shell script syntax - for <ul><li>for name [ in word ] ; do list ; done </li></ul>102 Name: sparcs.kaist.ac.kr 103 Name: ara.kaist.ac.kr 104 Name: baram.kaist.ac.kr 105 Name: gurum.kaist.ac.kr 106 Name: sparcs6.kaist.ac.kr 107 Name: sparcs7.kaist.ac.kr 108 Name: sparcs8.kaist.ac.kr 109 Name: sparcs9.kaist.ac.kr 110 Name: ftp.kaist.ac.kr . . . . . . . . . . . . . . . . #!/usr/local/bin/bash for ((i=102;i<151;i++)); do data=`nslookup 143.248.234.$i 2> /dev/null| tail +4|head -1`; echo $i $data; done;
  13. 13. Shell script syntax(2) - if <ul><li>if list; then list; [ elif list; then list; ] ... [ else list; ] fi </li></ul>$ ./diff abc abc abc abc It’s same $ ./diff abcc abcd abcc Abcd It’s different #!/bin/sh echo $1; echo $2; if [ &quot;$1&quot; = &quot;$2&quot; ]; then echo &quot;It's same&quot;; else echo &quot;It's different&quot;; fi
  14. 14. Shell script syntax(3) – select <ul><li>select name [ in word ] ; do list ; done </li></ul>1) Hello 2) Quit #? 1 Hello World #? 2 done #!/usr/local/bin/bash OPTIONS=&quot;Hello Quit&quot; select opt in $OPTIONS; do if [ &quot;$opt&quot; = &quot;Quit&quot; ]; then echo done exit elif [ &quot;$opt&quot; = &quot;Hello&quot; ]; then echo Hello World else clear echo bad option fi done
  15. 15. Shell script syntax(4) - case <ul><li>case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac </li></ul>#!/bin/sh case $1 in all) telnet ara; telnet ska; telnet noah;; a) telnet aka;; s) telnet ska;; n) telnet noah;; esac
  16. 16. Shell script syntax(5) – Etc. <ul><li>while list; do list; done </li></ul><ul><li>until list; do list; done </li></ul><ul><li>[ function ] name () { list; } </li></ul>
  17. 17. Special Parameters <ul><li>0 : Name of the shell or shell script </li></ul><ul><li>$ : Process ID of the this shell </li></ul><ul><li>! : Process ID of last background command </li></ul><ul><li># : Number of arguments </li></ul><ul><li>@ : Positional parameter </li></ul><ul><li>1, 2, 3, .. 9 : n-th argument </li></ul>
  18. 18. Making Shell <ul><li>2003 SP Seminar on summer vacation </li></ul><ul><ul><li>For studying system programming. </li></ul></ul><ul><ul><li>For being a SPARCS man. </li></ul></ul><ul><ul><li>A flower of circle activation. </li></ul></ul><ul><li>SP + ARCS = SP ARCS </li></ul>
  19. 19. Homework <ul><li>airlover 라는 폴더를 만든다 . </li></ul><ul><li>위에서 만든 폴더에 이름이 SPARCS 03 학번 회원들의 아이디인 파일을 만들고 그 회원이 마지막으로 접속한 시각을 기록한다 . </li></ul><ul><li>하나의 파일로 묶은 후 압축해서 airlover.tar.bz2 라는 파일로 만든다 . </li></ul><ul><li>airlover 폴더를 지운다 . </li></ul><ul><li>실행하면 자동으로 압축이 풀리게 만든다 . </li></ul><ul><li>c.f) /SPARCS/mail/aliases.d/sparcs2003 </li></ul>
  20. 20. Reference <ul><li>http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/index.html </li></ul><ul><li>Sh/Bash/Csh/Tcsh man pages </li></ul>

×