unix shell script 2003/03/24 for sparcs ’03 김군훈. index what is shell? shell configuration...

20
Unix Shell Script 2003/03/24 For SPARCS ’03 김김김 <[email protected]>

Upload: louisa-hopkins

Post on 18-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Unix Shell Script

2003/03/24 For SPARCS ’03김군훈 <[email protected]>

Page 2: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Index

• What is shell?• Shell configuration & Environment variable.• Standard input/output/error,

Redirection, Pipelining.• Shell script examples.• Shell script syntax.• Special parameter• Making shell.• Homework.

Page 3: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

What is shell?

• Command / Interpreter– Bourn Shell– C Shell– Korn Shell– Bash– Tcsh Kerne

l

Shell

Program

Program

Program

User

Page 4: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Bash Configuration

• .bash_login: Login initialization like .login• .bash_logout: Logout actions• .bashrc: Shell configuration• .bash_profile: Login shell configuration• .inputrc: Individual readline initialization file

• .bash_history: Command history file

Page 5: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Environment Variable.

• PWD– Current working directory.

• PATH– The search path.

• PS1– The primary prompt string.

• MAIL– Filename to check mail.

Shell Var.

Environ. Var.

export

- set

- env

Page 6: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Standard input, output, error

1 - STDOUT

0 - STDIN

2 – STDERR

: : :1 - STDOUT

0 - STDIN

2 – STDERR

: : :

Program1 FD

Program2 FD

Page 7: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Redirection & Pipelining

File

File

Program

Program

<

|

>

Etc..

>> << <<< 2>&1 2> &> …

Page 8: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Script Examples - Start

#!/bin/sh

mutt

telnet ara

telnet ska

telnet pie

Page 9: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Script Examples(2) – Color Table

#!/usr/local/bin/bash

esc="\033["

echo -e " 40 41 42 43 44 45 46 47"

for fore in 30 31 32 33 34 35 36 37; do

line1="$fore "

line2=" "

for back in 40 41 42 43 44 45 46 47; do

line1="${line1}${esc}${back};${fore}m 보통 ${esc}0m "

line2="${line2}${esc}${back};${fore};1m 밝은 ${esc}0m "

done

echo -e "$line1\n$line2"

done

Page 10: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

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 "bu2.gif" |tail +14 |tac |tail +11 |tac \

|sed -e 's/[phon[0-9]\+\.gif] //g' -e 's/[phon[0-9]\+\.gif]//g' \

|less -r

Page 11: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Script Examples(4) – Etc.

• Automatic install program.• Homework submit program.

– Homework num., source file– Delay due

• Homework marking program.– Compare optimized solution– Check execution time.

• Check empty IP address.

Page 12: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Shell script syntax - for

• for name [ in word ] ; do list ; done

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;

Page 13: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Shell script syntax(2) - if

• if list; then list; [ elif list; then list; ] ... [ else list; ] fi

$ ./diff abc abc

abc

abc

It’s same

$ ./diff abcc abcd

abcc

Abcd

It’s different

#!/bin/sh

echo $1;

echo $2;

if [ "$1" = "$2" ]; then

echo "It's same";

else

echo "It's different";

fi

Page 14: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Shell script syntax(3) – select

• select name [ in word ] ; do list ; done

1) Hello

2) Quit

#? 1

Hello World

#? 2

done

#!/usr/local/bin/bash

OPTIONS="Hello Quit"

select opt in $OPTIONS; do

if [ "$opt" = "Quit" ]; then

echo done

exit

elif [ "$opt" = "Hello" ]; then

echo Hello World

else

clear

echo bad option

fi

done

Page 15: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Shell script syntax(4) - case

• case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

#!/bin/sh

case $1 in

all)

telnet ara; telnet ska; telnet noah;;

a)

telnet aka;;

s)

telnet ska;;

n)

telnet noah;;

esac

Page 16: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Shell script syntax(5) – Etc.

• while list; do list; done• until list; do list; done• [ function ] name () { list; }

Page 17: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Special Parameters

• 0 : Name of the shell or shell script• $ : Process ID of the this shell• ! : Process ID of last background comma

nd• # : Number of arguments• @ : Positional parameter• 1, 2, 3, .. 9 : n-th argument

Page 18: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Making Shell

• 2003 SP Seminar on summer vacation– For studying system programming.– For being a SPARCS man.– A flower of circle activation.

• SP + ARCS = SPARCS

Page 19: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Homework

• airlover 라는 폴더를 만든다 .• 위에서 만든 폴더에 이름이 SPARCS 03 학번

회원들의 아이디인 파일을 만들고 그 회원이 마지막으로 접속한 시각을 기록한다 .

• 하나의 파일로 묶은 후 압축해서 airlover.tar.bz2 라는 파일로 만든다 .

• airlover 폴더를 지운다 .• 실행하면 자동으로 압축이 풀리게 만든다 .• c.f) /SPARCS/mail/aliases.d/sparcs2003

Page 20: Unix Shell Script 2003/03/24 For SPARCS ’03 김군훈. Index What is shell? Shell configuration & Environment variable. Standard input/output/error, Redirection,

Reference

• http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/index.html

• Sh/Bash/Csh/Tcsh man pages