Presentation is loading. Please wait.

Presentation is loading. Please wait.

ㅎㅎ 콘 쉘 (배시쉘) 활용하기 기본 사용법 익히기 입출력 방향 변경 콘 쉘의 환경설정 콘 쉘 명령 다루기 프롬프트 설정하기

Similar presentations


Presentation on theme: "ㅎㅎ 콘 쉘 (배시쉘) 활용하기 기본 사용법 익히기 입출력 방향 변경 콘 쉘의 환경설정 콘 쉘 명령 다루기 프롬프트 설정하기"— Presentation transcript:

1 ㅎㅎ 콘 쉘 (배시쉘) 활용하기 기본 사용법 익히기 입출력 방향 변경 콘 쉘의 환경설정 콘 쉘 명령 다루기 프롬프트 설정하기
환경설정 파일 설정하기 한빛미디어(주)

2 내용 학습목표 5장. 콘 쉘 (배시쉘) 활용하기 쉘 기본 사용법 익히기 입출력 방향 변경 배시쉘의 환경 설정 명령 다루기
콘 쉘(배시쉘)이 제공하는 다양한 기능을 이해하고 활용한다. 환경변수를 이용해 작업의 효율을 높이는 환경을 설정하는 방법을 익힌다. 환경 설정 파일을 이용해 자신만의 환경을 설정하는 방법을 익힌다. 내용 쉘 기본 사용법 익히기 입출력 방향 변경 배시쉘의 환경 설정 명령 다루기 프롬프트 설정하기 환경 설정파일 설정하기

3 쉘 ? 쉘의 종류 종류 확인하기 Section 01 기본 사용법 익히기 사용자와 커널 사이의 중간 역할
사용자가 입력한 명령을 처리 하고 실행 결과를 알려줌 편리한 사용을 위해 다양한 기능 제공 쉘의 종류 본쉘, 콘쉘, C쉘, 배시쉘,… 로그인 쉘, 서브쉘 종류 확인하기 프롬프트로 확인 : C쉘은 %, 본쉘/콘쉘/배시쉘은 $ 명령으로 확인 프롬프트 모양이 # 인 경우? 시스템관리자(root) 계정임 텔넷 cookook.co.kr $ echo $SHELL /usr/bin/bash $ bash 쉘!

4 There are five shells that are commonly supported by UNIX vendors:
A shell is a program that sits between you and the raw UNIX operating system. There are five shells that are commonly supported by UNIX vendors: the Bourne shell(sh), the Korn shell(ksh), the C shell(csh), TC shell(tcsh) and Bourne Again Shell (bash). In the next segment, we will study Bash (Bourne Again Shell) that is compatible with Bourne shell and borrows best features from the other shells. Bash is becoming the most popular shell - it is the default shell in Linux.

5 Five Standard UNIX Shells
Figure 5-1 Five Standard UNIX Shells

6 SHELL FUNCTIONALITY(기능)
- This chapter describes the common core of functionality that all four shells provide. Here is a diagram that illustrates the relationship among the four shells: Bourne Again Shell Korn shell C shell Bourne shell Common core Common core

7 로그인 쉘과 서브쉘 로그인쉘과 서브쉘 로그인 쉘 : 사용자가 로그인한 직후 자동 생성되는 쉘
서브쉘 : 사용자가 직접 실행한 쉘 ksh csh login logout 로그인쉘 서브쉘 exit ray.kyungil.ac.kr $ csh % ksh $ exit % exit $

8 Figure 5-3 Shell Relationship

9 echo print 사용법 문자열 출력 쉘 명령과 유틸리티 모두 제공 지정한 문자열 출력 쉘 명령
ray.kyungil.ac.kr $ echo ‘test’ test $ print ‘I love UNIX!!’ I love UNIX!! $

10 쉘 특수 문자 - 종류 메타문자 기능 예제 ; 한 줄에 여러 개의 명령 입력 $ date;cal;ls * 임의의 문자 또는 문자들 $ ls h* ? 임의의 한 문자 $ ls dir? [ ] 한 문자 위치를 위한 문자의 범위 표시 $ ls [a-f]* >, >>, < 입출력 방향 전환 $ ls > ls.out | 명령어 파이핑 $ ls –l /etc | more ~ 홈 디렉토리 $ cd ~user1 - 이전 작업 디렉토리 $ cd - ‘ ‘ 모든 쉘 문자 무시 $ echo ‘$SHELL’ “ “ $, `, \를 제외한 모든 쉘 문자 무시 $ echo “$SHELL” ` ` 쉘 명령 수행 $ echo `date` \ 특수문자 기능 제거 $ echo “\$SHELL”

11 Figure 5-12 Quotes(인용) \ ‘ ’ “ ”

12 QUOTING - There are often times when you want to inhibit the shell’s wildcard-replacement, variable-substitution, and/or command-substitution mechanisms. The shell’s quoting system allows you to do just that. - Here’s the way that it works: 1) Single quotes(’) inhibit wildcard replacement, variable substitution, and command substitution. 2) Double quotes(”) inhibit wildcard replacement only. 3) When quotes are nested, it’s only the outer quotes that have any effect.

13 QUOTING - The following example illustrates the difference between the two different kinds of quotes: $ echo 3 * 4 = > remember, * is a wildcard. 3 a.c b b.c c.c 4 = 12 $ echo ”3 * 4 = 12” ---> double quotes inhibit wildcards. 3 * 4 = 12 $ echo ’3 * 4 = 12’ ---> single quotes inhibit wildcards. $ name=Graham

14 - By using single quotes(apostrophes) around the text,
we inhibit all wildcarding and variable and command substitutions: $ echo ’my name is $name - date is `date`’ my name is $name - date is ‘date’ $ _ - By using double quotes around the text, we inhibit wildcarding, but allow variable and command substitutions: $ echo ”my name is $name - date is `date`” my name is Graham - date is Mon Feb 2 23:14:56 CST 1998 $ -

15 ; : 명령 연결 ? : 임의의 한문자 * : 임의의 문자 [ ] : 범위내의 한문자 쉘 특수 문자 -사용예제
; : 명령 연결 ? : 임의의 한문자 * : 임의의 문자 [ ] : 범위내의 한문자 ray.kyungil.ac.kr $ mkdir ~/unix/ch5 $ cd ~/unix/ch5 $ pwd ; touch a.txt b.txt; ls /export/home/user1/unix/ch5 a.txt b.txt $ ray.kyungil.ac.kr $ ls *.txt a.txt b.txt $ ray.kyungil.ac.kr $ touch ab.txt $ ls ?.txt a.txt b.txt $ ray.kyungil.ac.kr $ ls [ab].txt a.txt b.txt $

16 ` ` : back quotation명령 실행
쉘 특수 문자 - 사용예제 - : 이전작업디렉토리 ‘ ‘ , “ ” : 문자열 묶기 ` ` : back quotation명령 실행 ray.kyungil.ac.kr $ pwd /export/home/user1/unix/ch5 $ cd /tmp /tmp $ cd - $ cd – $ cd - ray.kyungil.ac.kr $ echo ’$SHELL’ $SHELL $ echo ”$SHELL” /bin/ksh $ ray.kyungil.ac.kr $ echo ”Today: `date`” Today : 2007년 4월 25일 화요일 오후 02시 15분 04초 $

17 cd ../ch3 ls ls f* ls *.c ls * ls file? ls file[2-3] cd /etc pwd cd -
[실습하기] 쉘 특수 문자 실습하기 cd ../ch3 ls ls f* ls *.c ls * ls file? ls file[2-3] cd /etc pwd cd - cd ../ch5 디렉토리 내용 확인 f 로 시작하는 모든 파일 출력 .c로 끝나는 모든 파일 출력 모든 파일 출력 file_ 파일 출력 file2, file3 출력 /etc로 이동 현재 경로 확인 이전 작업디렉토리로 이동 5장 디렉토리로 이동

18 print “현재 시스템은 `uname –a`입니다.”
[실습하기] 쉘 특수 문자 실습하기 print “abc” print ‘$TERM’ print “$TERM” print “현재 시스템은 `uname –a`입니다.” print : 화면에 출력 $로 시작하면 쉘 변수. ‘ ‘안에서는 모든 특수문자가 그 의미를 잃어버림. 그냥 $TERM 출력 “ “ 안에서는 쉘 변수 값 출력 ` ` 은 명령의 실행 결과 출력

19 한 명령의 실행결과를 다음 명령의 입력으로 전달 | (파이프) 양쪽에 명령이 와야 함 사용법 $ 명령1 | 명령2 | 명령3
쉘 특수 문자 - 파이프 한 명령의 실행결과를 다음 명령의 입력으로 전달 | (파이프) 양쪽에 명령이 와야 함 사용법 $ 명령1 | 명령2 | 명령3 telnet hanbitbook.co.kr $ ls /etc | more

20 ls /etc ls /etc > ls.out more ls.out ls /etc | more ps -ef
[실습하기] 쉘 특수 문자 – 파이프 실습 실습하기 ls /etc ls /etc > ls.out more ls.out ls /etc | more ps -ef ps -ef | more ls /etc 의 결과는 한 화면에 다 보이지 않음 ls /etc 의 결과를 파일에 저장 (다음절에 배움) more 명령을 이용하여 파일을 한 화면씩 출력 파이프를 이용한 결과와 3)의 결과 비교 현재 서버에서 실행중인 프로세스 목록 출력 목록을 한 화면씩 출력

21 Piping Output to Next Command
Figure 5-8 Piping Output to Next Command

22 $ head -4 /etc/passwd ---> look at the password file.
root:eJ2S10rVe8mCg:0:1:Operator:/:/bin/csh nobody:*:65534:65534::/: daemon:*:1:1::/: sys:*:2:2::/:/bin/csh $ cat /etc/passwd | awk -F: ‘{ print $1 }’ | sort audit bin daemon glass ingres news nobody sync sys tim uucp $ _

23 - Here’s an illustration of the pipeline that we just used:
cat Pipe awk Pipe sort Terminal In the example, we pipe the contents of the “/etc/passwd” file into the awk utility to extract the first field of each line. The output of awk is then piped to the sort utility, which sorts the lines alphabetically. The result is a sorted list of every user on the system.

24 Figure 5-9 The tee Command

25 • Utility : tee -a -{fileName}+
The tee utility copies its standard input to the specified files and to its standard output. The -a option causes the input to be appended to the files rather than overwriting them. - In the following example, we copy the output of who to a file called “who.capture” and also let it pass through to sort: $ who | tee who.capture | sort ables ttyp6 May :54 ( waterloo.com ) glass ttyp0 May :49 ( bridge05.utdalla ) posey ttyp2 May :44 ( blackfoot.utdall ) posey ttyp4 May :44 ( blackfoot.utdall ) $ cat who.capture > look at the captured data. posey ttyp2 Apr :44 ( blackfoot.utdall ) posey ttyp4 Apr :44 ( blackfoot.utdall )

26 표준 출력(Standard output)
Section 02 입출력 방향 변경 표준 입력 (Standard input) 프로그램 실행에 필요한 데이터를 읽어드리는 기본 장치 기본 표준 입력은 키보드 표준 출력(Standard output) 프로그램의 실행 결과를 출력하는 장치 기본 표준 출력은 모니터 표준 오류(Standard error) 프로그램 실행중 발생한 오류 메시지를 출력하는 장치 기본 표준 오류는 모니터

27 0< 1>, >> 2>
입출력 방향 변경 표준 입출력 장치를 변경시 특수 기호 사용 리다이렉션 (redirection): 표준 입출력 파일의 변경 표준 입력 표준 출력 표준 오류 명령어 수행 1) 키보드 2) 파일 모니터 파일 0< 1>, >> 2>

28 Redirecting Standard Input 표준입력의 재지정
Figure 5-5 Redirecting Standard Input 표준입력의 재지정

29 Redirecting Standard Output 표준출력의 재지정
Figure 5-6 Redirecting Standard Output 표준출력의 재지정 >| : overwrite (set –o noclobber 시) >> : append

30 Redirecting Standard Error 표준오류의 재지정
1. Standard Output and Error on Monitor $ ls -l file1 notfound notFound: 해당 파일이나 디렉토리가 없음 -rw-r--r-- 1 kky pro 123 Oct 2 16:20 file1 2. Standard Output to File; Errors on Monitor $ ls -l file1 notfound 1> FileL $ more FileL

31 $ ls -l file1 notfound 1> mystout 2> mystderr $ more mystdout
3. Redirecting to Different Files (표준출력, 표준에러) $ ls -l file1 notfound 1> mystout 2> mystderr $ more mystdout -rw-r--r-- 1 kky pro 123 Oct 2 16:20 file1 $ more mystderr notFound: 해당 파일이나 디렉토리가 없음 4. Redirecting to Same Files (표준출력, 표준에러) $ ls -l file1 notfound 1> FileL 2>& 1 $ more FileL

32 >는 원래 1>의 형태이나 1을 생략할 수 있음.
입출력 방향 변경 – 출력 리다이렉션 표준출력을 모니터에서 파일로 변경 > : 새로운 파일로 생성, 기존 파일의 내용은 없어짐 >> : 기존 파일의 끝에 내용 추가 사용법 $ 명령 > 파일명 $ 명령 >> 파일명 >는 원래 1>의 형태이나 1을 생략할 수 있음. 대부분 >을 사용함 ray.kyungil.ac.kr $ ls –al > test $ date > test $ cat test 2007년 5월 1일 화요일 오후 2시 30분 $ pwd >> test test파일의 내용은 무엇일까?

33 [실습하기] 입출력 방향 변경 – 출력 리다이렉션
실습하기 (> 와 >> 비교) ls -al ls -al > ls.out cat ls.out pwd > ls.out date >> ls.out 결과가 모니터로 출력 결과가 파일로 저장 파일 내용 확인 파일 내용에서 2)의 결과는 어떻게 되었는가? 명령의 결과를 파일에 추가 파일 내용은 어떻게 되었는가? 4)의 결과는 어떻게 되었는가?

34 $ 명령 2> 파일명 표준 오류메시지를 파일에 저장 사용법 입출력 방향 변경 – 오류 리다이렉션
$ 명령 2> 파일명 ray.kyungil.ac.kr $ ls /ttt 해당 파일이나 디렉토리가 없음 $ ls /ttt 2> ls.err $ cat ls.err $

35 [실습하기] 입출력 방향 변경 – 오류 리다이렉션
ls /test ls /test 2> ls.err cat ls.err rm ls.out ls.err ls /var /test 1> ls.out 2> ls.err cat ls.out ls /var /test 1> ls.out 2>&1 오류메시지가 화면으로 출력 오류메시지가 파일에 저장 파일 내용 확인 파일 삭제 결과는 ls.out으로 오류메시지는 ls.err로 저장 파일 내용을 확인 결과와 오류메시지를 같은 ls.out 파일로 저장

36 $ 명령 < 파일명 표준 입력을 키보드에서 파일로 변경 사용법 입출력 방향 변경 – 입력 리다이렉션
$ 명령 < 파일명 ray.kyungil.ac.kr $ cat < loveletter I love UNIX! $

37 [실습하기] 입출력 방향 변경 – 입력 리다이렉션
vi loveletter I love UNIX! 입력 mailx user1 < loveletter mailx ? 1 I love UNIX! ? d ? q 메일 내용을 미리 파일로 작성 자신에게 메일 전송 mailx 툴을 이용하여 메일 확인 메일번호 입력 d : 메일 삭제 q : mailx 종료

38 The Bash, C and Korn shells also provide protection against accidental
overwriting of a file due to output redirection. In Bash: $ set -o noclobber $ echo text > test bash: test: cannot overwrite existing file $ echo text >| test When the shell encounters a sequence of the form $ command << word - it copies its standard input up to, but not including, the line starting with word into a buffer and then executes command using the contents of the buffer as its standard input. - that allows shell programs( scripts ) to supply the standard input to other commands as in-line text, $ cat << eof > line 1 > line 2 > line 3 > eof line 1 line 2 line 3 $ _

39 Command Execution: 명령어 실행
Four Syntactical formats for combining commands into one line 1. Sequence Commands(순차명령) ; $ echo “Hello\n” > File1; cal >> File1 2. Grouped Commands (그룹명령) ( ) $ (echo “Hello\n”; cal) > File1 3. Chained Commands (체인명령) | 4. Conditional Commands (조건명령) &&, || $ cp file1 Temp && echo “success” $ cp notfound Temp || echo “failed”

40 • SEQUENCES If you enter a series of simple commands or pipelines separated by semicolons, the shell will execute them in sequence, from left to right. This facility is useful for type-ahead(and think-ahead) addicts who like to specify an entire sequence of actions at once. Here’s an example: $ date; pwd; ls > execute three commands in sequence. Mon Feb :11:10 CST 1998 /home/glass/wild a.c b.c cc.c dir dir2 $ _

41 - Each command in a sequence may be individually I/O redirected as well:
$ date > date.txt; ls; pwd > pwd.txt a.c b.c cc.c date.txt dir dir2 $ cat date.txt Mon Feb :12:16 CST $ cat pwd.txt > look at output of pwd. /home/glass $ _

42 • Conditional Sequences
- Every UNIX process terminates with an exit value. By convention, an exit value of 0 means that the process completed successfully, and a nonzero exit value indicates failure. - All built-in shell commands return a value of 1 if they fail. You may construct sequences that make use of this exit value: 1) If you specify a series of commands separated by “&&” tokens, the next command is executed only if the previous command returns an exit code of 0. 2) If you specify a series of commands separated by “||” tokens, a nonzero exit code.

43 - For example, if the C compiler cc compiles a program without fatal errors, it creates an executable program called “a.out” and returns an exit code of 0; otherwise, it returns a nonzero exit code. $ cc myprog.c && ./a.out - The following conditional sequence compiles a program called “myprog.c” and displays an error message if the compilation fails: $ cc myprog.c || echo compilation failed.

44 • GROUPING COMMANDS - Commands may be grouped by placing them between parentheses, which causes them to be executed by a child shell(subshell). - The group of commands shares the same standard input, standard output, and standard error channels and may be redirected and piped as if it were a simple command. - Here are some examples: $ date; ls; pwd > out.txt > execute a sequence. Mon Feb 2 00:33:12 CST > output from date. a.c b.c > output from ls. $ cat out.txt > only pwd was redirected. /home/glass $ ( date; ls; pwd ) > out.txt > group and then redirect. $ cat out.txt > all output was redirected. Mon Feb :33:28 CST a.c b.c $ _

45 변수 쉘(지역) 변수 환경(전역) 변수 시스템이나 사용자에 의해 사용되는 정보를 저장하기 위한 저장소
Section 03 콘 쉘 환경 설정( 배시쉘) 변수 시스템이나 사용자에 의해 사용되는 정보를 저장하기 위한 저장소 쉘 변수는 관례적으로 대문자를 사용 쉘(지역) 변수 현재의 쉘에서만 사용 가능한 변수 확인 명령 : set 환경(전역) 변수 전체 쉘에서 사용 가능한 변수 확인 명령 : env

46 콘쉘 (배시 쉘) 변수 정의 변수 값 확인 변수 정의 해제 콘 쉘(배시 쉘) 환경 변수 쉘 변수 : 변수=값
환경 변수 : export 변수=값 변수 값 확인 print $변수 echo $변수 변수 정의 해제 unset 변수 ray.kyungil.ac.kr $ ATEST=test $ export ATEST $ ray.kyungil.ac.kr $ echo $ATEST test $ ray.kyungil.ac.kr $ unset ATEST $ echo $ATEST $ env

47 env set export MYVAR=user1 csh exit export MYVAR print $MYVAR
[실습하기] 콘 쉘 환경 변수 실습하기 env set export MYVAR=user1 csh exit export MYVAR print $MYVAR unset MYVAR env로 환경변수 출력 set으로 쉘변수 출력 export로 환경변수 출력 쉘 변수 MYVAR 정의 csh로 전환 (서브쉘) 환경변수를 출력하여, 그중에 MYVAR가 있는지 확인 다시 ksh로 전환 MYVAR를 export하여 전역변수로 바꿈 다시 csh로 전환 MYVAR값을 출력 ksh로 돌아감 MYVAR 변수를 해제

48 CDPATH cd 명령 사용시 주어진 디렉토리 검색 경로 PWD 작업 디렉토리 절대경로 SHELL 로그인쉘
환경 변수 환경변수 의미 HOME 사용자 홈 디렉토리 절대경로 LOGNAME 사용자 계정 이름 PATH 명령을 탐색할 경로 CDPATH cd 명령 사용시 주어진 디렉토리 검색 경로 PWD 작업 디렉토리 절대경로 SHELL 로그인쉘

49 Section 04 콘 쉘 명령 다루기 – alias
명령어에 다른 이름을 정의하여 사용 긴 명령어 대신 짧은 별칭을 사용 자주 사용하는 옵션 자동 수행 여러 명령을 하나의 명령으로 대체 가능 명령 현재 설정값 보기 : alias 앨리어스 설정 : alias 별칭=명령 앨리어스 해제 : unalias 별칭 alias [ 별칭=명령 ] unalias 별칭

50 alias 설정 alias 해제 현재 설정된 alias $ alias ls=’ls –F’ $ alias cd=’cd;pwd’
ray.kyungil.ac.kr $ alias autoload='typeset -fu' command='command ' functions='typeset -f' h=history history='fc -l' integer='typeset -i' local=typeset nohup='nohup ' r='fc -e -' stop='kill -STOP' suspend='kill -STOP $$' ray.kyungil.ac.kr $ alias ls=’ls –F’ $ alias cd=’cd;pwd’ $ alias rm=’rm -i’ $ ray.kyungil.ac.kr $ unalias rm $ unalias cd $ unalias ls

51 alias home=‘cd;pwd;ls’ home alias copy=‘cp’ alias move=‘mv’
실습하기 alias alias c=clear c alias dir=‘ls -l’ dir alias home=‘cd;pwd;ls’ home alias copy=‘cp’ alias move=‘mv’ alias ls=‘ls –F’ ls 기존 alias를 확인 clear명령의 alias 생성 c를 실행 ls –l을 dir로 alias dir을 실행 home이라는 이름으로 alias를 생성. 3개의 명령을 하나의 이름으로 실행 가능 home 실행 copy 생성 move 생성 ls를 ls –F로 변경 ls 실행 alias 설정 확인

52 사용자가 입력한 명령을 기록, 재사용 할 수 있도록 함
콘 쉘 명령 다루기 - history 사용자가 입력한 명령을 기록, 재사용 할 수 있도록 함 ~/.sh_history 파일에 저장 (~/.bash_history 배시 쉘) 기본적으로 128개의 명령이 저장 history 명령 사용시 화면에는 최근 16개의 명령 출력 옵션 -숫자 출력할 명령 개수 지정 가장 최근 명령으로 출력 history [옵션]

53 history 목록 최근 3개 명령만 보기 콘 쉘 명령 다루기 – history ray.kyungil.ac.kr
102 pwd 103 cp *.c ~/C 104 alias 105 ls 106 ls /tmp 107 ! 108 man history 109 !99 110 ls 111 history 112 alias 113 unalias history 114 history 115 alias history=‘fc –l’ 116 history 117 fc -l ray.kyungil.ac.kr $ history -3 115 alias history=‘fc –l’ 116 history 117 fc -l $

54 r 문자 r 번호 이미 정의되어 있는 alias History 목록의 명령어를 재실행 r 문자 r 번호
콘 쉘 명령 다루기 – 명령 재실행 이미 정의되어 있는 alias History 목록의 명령어를 재실행 r 문자 지정한 문자로 시작하는 가장 최근의 명령 재실행 r 번호 지정한 history 번호의 명령 재실행 r 문자 r 번호

55 history 목록 문자 이용 숫자 이용 $ r c $ $ r 116 History 123 cd .. 124 …
콘 쉘 명령 다루기 – 명령 재실행 history 목록 문자 이용 숫자 이용 ray.kyungil.ac.kr $ history 102 pwd 103 cp *.c ~/C 104 alias 105 ls 106 ls /tmp 107 ! 108 man history 109 !99 110 ls 111 history 112 alias 113 unalias history 114 history 115 alias history=‘fc –l’ 116 history 117 fc -l ray.kyungil.ac.kr $ r c cp *.c ~/C $ ray.kyungil.ac.kr $ r 116 History 123 cd .. 124

56 history h cd ls –a .[a-z]* vi .sh_history r a r l r h r 번호 실습하기
[실습하기] 콘 쉘 명령 다루기 – 명령 재실행 실습하기 history h cd ls –a .[a-z]* vi .sh_history r a r l r h r 번호 history 확인 history가 h로 alias되어 있는지 확인 홈 디렉토리로 이동 숨겨진 파일 목록 확인 .sh_history파일의 내용 확인 a로 시작하는 명령 재실행 l로 시작하는 명령 재실행 h로 시작하는 명령 재실행 history목록의 번호로 재실행

57 히스토리 – fc (배시쉘) Fix Command
사용자가 입력한 명령을 기록, 재사용 할 수 있도록 함 히스토리 목록에서 명령어를 선택하거나 편집 ~/.bash_history 파일에 명령 저장 -l 옵션이 없으면 최근 명령을 편집기로 읽어들임 옵션 -l -x –y : 히스토리 목록에서 지정한 행이나 범위의 명령 출력 -r : 히스토리 목록을 역순으로 출력 -n : 히스토리에서 행번호를 제외하고 출력 -e : 히스토리 목록을 편집기로 읽어들임 -s : 최근 명령을 재실행 fc [ 옵션 ]

58 이전 명령 목록 보기 히스토리 – fc (배시쉘) $ fc -l 102 pwd 103 cp *.c ~/C 104 alias
텔넷 cookook.co.kr $ fc -l 102 pwd 103 cp *.c ~/C 104 alias 105 ls 106 ls /tmp 107 ! 108 man history 109 !99 110 ls 111 history 112 alias 113 unalias history 114 history 115 alias history=‘fc –l’ 116 history 117 fc -l

59 ! 문자 ! 번호 이전에 실행했던 명령을 반복 실행하는 기능 이벤트 지정자 (!)을 이용하여 명령 실행 명령 재실행 (배시쉘)
설명 !! 마지막 명령 실행 !n n번째 명령 실행 !-n 현재 명령에서 뒤로 n번째 명령 !문자열 주어진 문자열로 시작하는 명령중 마지막 명령 <이벤트명령>:p !!, !n등 이벤트 지정자로 지정한 명령을 실행하지 않고 화면에 출력 !?문자열? 주어진 문자열을 포함하는 명령중 마지막 명령. ?는 선택항목 ^문자열1^문자열2 마지막 명령의 문자열1을 문자열2로 대체하여 다시 실행

60 히스토리 목록 명령 재실행 및 편집 [실습하기] 명령 재실행 (배시쉘) $ fc -l 102 pwd 103 cp *.c ~/C
텔넷 cookook.co.kr $ fc -l 102 pwd 103 cp *.c ~/C 104 alias 105 ls 106 ls /tmp 107 ! 108 man history 109 !99 110 ls 111 history 112 alias 113 unalias history 114 history 115 alias history=‘fc –l’ 116 history 117 fc -l 텔넷 cookook.co.kr $ !! fc –l 103 cp *.c ~/C 104 … $ !c cp *.c ~/C $ !102 /home/user1/Unix/ch5 $ !115:p alias history=’fc –l’ $ ^history^h alias h=‘fc –l’ $

61 history h cd ls –a .[a-z]* vi .bash_history !a !l !h ! 번호 !a : p
[실습하기] 명령 재실행 (배시쉘) history h cd ls –a .[a-z]* vi .bash_history !a !l !h ! 번호 !a : p ^history^h history 확인 history 가 h로 alias 되어 있는지 확인 홈 디렉토리로 이동 숨겨진 파일 목록 확인 .bash_history파일의 내용 확인 a로 시작하는 명령 재실행 l로 시작하는 명령 재실행 h로 시작하는 명령 재실행 history목록의 번호로 재실행 a로 시작하는 명령을 화면에 출력 직전에 입력한 alias 명령의 history를 h로 변경

62 이전 명령을 편집하여 다시 실행 방법 명령 편집하기 – 화살표 키 이용 (배시쉘)
↑ 또는 ↓ 화살표키를 이용하여 이전 명령을 불러옴 ← 또는 → 화살표키를 이용하여 수정할 위치로 커서 이동 [Del] 또는 [←]BS 키를 이용하여 수정할 내용 삭제 후 새로운 내용 입력 $ set -o vi Esc

63 set -o vi set +o vi 명령어 라인 편집 설정 콘 쉘(베시쉘) 명령 다루기 – 명령 편집 $ set -o vi
ESC 키를 누르면 편집 가능 Vi 명령인 k, j 키를 이용하여 이전 명령을 호출 h, l, x, r, c 등 vi편집 명령을 이용하여 명령어 편집 $ set +o vi 명령어 편집 기능 해제 set -o vi set +o vi

64 Using command line editor(vi)
Execute a previous Line 1. Move to command mode : esc 2. Move up the list using Move-up key : k 3. When command has been located, key Return to re-execute it. Edit and Execute a Previous Command Assume previous command : $ more file  more file1 2. recall previous line : k 3. insert at end of line : A 4. key missing character and Return : 1

65 set –o vi cd /temp Esc+k llll x Enter 실습하기 [실습하기] 콘 쉘 명령 다루기 – 명령 편집
틀린 명령 입력 Esc키를 눌러 편집상태로 변경 Vi의 ㅣ이동키로 이동 틀린 글자 삭제 원하는 명령에서 Enter를 치면 명령 실행

66 콘 쉘(배시 쉘)의 프롬프트 $ echo $PS1 $ Section 05 프롬프트 설정하기 기본 프롬프트 : $
프롬프트 확인 ray.kyungil.ac.kr $ echo $PS1 $

67 콘 쉘 (배시 쉘) 의 프롬프트 $ echo $PS1 $ 콘 쉘의 프롬프트 설정 기본 프롬프트 : $
프롬프트 확인 프롬프트 변경 ray.kyungil.ac.kr $ echo $PS1 $ ray.kyungil.ac.kr $ PS1=‘Unix ’ Unix PS1=‘haha ’ haha

68 실습하기 PS1=“[$LOGNAME] “ PS1=“{$PWD} “ PS1=“$PWD [!] “
[실습하기] 콘 쉘 프롬프트 설정하기 실습하기 PS1=“[$LOGNAME] “ PS1=“{$PWD} “ PS1=“$PWD [!] “ PS1=“`uname –n` !$ “ 프롬프트를 LOGNAME값으로 변경 프롬프트를 현재 디렉토리가 나오도록 변경 현재 디렉토리와 히스토리 라인번호(!)가 나오도록 수정 호스트명과 일련번호가 나오게 수정 \$PWD

69 이스케이프 시퀀스 컬러 프롬프트 설정 터미널 제어를 위해 정의된 문자열 Esc(^[)로 시작한다.
‘^[‘는 ‘Ctrl-v [‘ 이다.

70 [x;y;nm : 특별한 기능이 정의된 문자열 PS1=“^[[x;y;nm 프롬프트 ^[[x;y;0m”
컬러 프롬프트 설정 ^[ : 이스케이프 문자(esc) [x;y;nm : 특별한 기능이 정의된 문자열 x, y : color 번호 n : 특수 기능(숫자) PS1=“^[[x;y;nm 프롬프트 ^[[x;y;0m”

71 특수기능번호 컬러 프롬프트 설정 번호 기능 기본색 1 굵게 4 흑백에서 밑줄 5 반짝임 7 역상 10 기본 폰트 38
기본색 1 굵게 4 흑백에서 밑줄 5 반짝임 7 역상 10 기본 폰트 38 밑줄 사용가능 39 밑줄 사용 불가능

72 컬러번호 컬러 프롬프트 설정 컬러 글자색번호 배경색번호 검정색 30 40 빨강색 31 41 녹색 32 42 갈색 33 43
파랑색 34 44 보라색 35 45 청록색 36 46 흰색 37 47

73 프롬프트를 파란색으로 굵게 (배경색은 그대로 두고)
컬러 프롬프트 설정 프롬프트를 파란색으로 굵게 (배경색은 그대로 두고) 프롬프트를 파란색으로 보통 ray.kyungil.ac.kr $ PS1=“^[[34;1mUnix $ ^[[0;0m” Unix $ ls ray.kyungil.ac.kr $ PS1=“^[[34mUnix $ ^[[0m” Unix $ ls

74 프롬프트를 밑줄 넣고 빨간색으로 프롬프트를 파란색 역상으로 컬러 프롬프트 설정 ray.kyungil.ac.kr
$ PS1=“^[[31;4mUnix $ ^[[0;0m” Unix $ ls ray.kyungil.ac.kr $ PS1=“^[[34;7mHaHa $ ^[[0m” HaHa $ ls HaHa $

75 프롬프트를 파란색 현재 디렉토리로 프롬프트를 녹색, 호스트 이름으로 컬러 프롬프트 설정 ray.kyungil.ac.kr
$ PS1=‘$PWD> ‘ $ PS1=“\$PWD> “ ray.kyungil.ac.kr $ PS1=“^[[34;0m[$PWD]$ ^[[0;0m” [/export/home/user1/unix/ch5]$ ls ray.kyungil.ac.kr $ PS1=“^[[32;1m –n` ^[[0m” $ $ ls

76 이스케이프 시퀀스 (Escape Sequence)
컬러 프롬프트 설정 (배시쉘) 이스케이프 시퀀스 (Escape Sequence) 단말기의 제어를 위해 정의된 문자열 쉘에 출력되지는 않음 이스케이프 문자 기능 \u 현재 사용자 이름 ($LOGNAME 과 동일) \h 호스트 이름 (`uname –n` 명령과 동일) \w 현재 작업 디렉토리 ($PWD와 유사. 홈디렉토리를 ~로 표시) \t 24시간 형식으로 나타낸 시각(HH:MM:SS) \A 24시간 형식으로 나타낸 시각(HH:MM) \e ASCII 이스케이프 문자. 터미널 창에 고급 옵션을 전달 \[ 출력하지 않을 문자열의 시작 표시 \] 출력하지 않을 문자열의 끝 표시 \! 히스토리 번호

77 PS1=‘\[\e[x;y;nm\]프롬프트 \[\e[x;y;0m\]’
컬러 프롬프트 설정하기 (배시쉘) \[ ~ \]: 이 사이의 문자들은 화면에 출력되지 않음 \e : 이스케이프 문자. 단말기 제어의 시작을 알림 \e ~ m 사이에 프롬프트 컬러 색상 지정 x;y : 컬러 번호 (x는 글자, y는 배경) n : 특수 기능 (밑줄, 역상 등) 프롬프트 : 프롬프트로 사용할 문자열 PS1=‘\[\e[x;y;nm\]프롬프트 \[\e[x;y;0m\]’ 프롬프트의 색상 및 특수 효과 표시 원래 색상으로 돌아가기 위한 지정

78 파란색으로 굵게 빨간색에 밑줄 파란색으로 보통 파란색, 굵게 현재디렉토리 컬러 프롬프트 설정 (배시쉘)
파란색, 굵게 현재디렉토리 텔넷 cookook.co.kr $ PS1=“\e[34;1mUnix$ \e[0;0m” Unix$ ls 텔넷 cookook.co.kr $ PS1=“\e[34mUnix$ \e[0;0m” Unix$ ls 텔넷 cookook.co.kr $ PS1=“\e[31;4mUnix$ \e[0;0m” Unix$ ls 텔넷 cookook.co.kr $ PS1=‘\e[34;1m[$PWD] \e[0;0m’ [/home/user1/ch5] $ bash // 실행 후 PS1 설정!!!

79 초기화 파일 시스템 초기화 파일 사용자 초기화 파일 Section 06 환경설정 파일 설정하기
쉘이 시작할 때 실행되는 명령어와 변수 설정 저장 로그인 할 때 실행 종류 : 시스템 초기화 파일, 사용자 초기화 파일 시스템 초기화 파일 시스템을 사용하는 전체 사용자의 공통 환경설정 /etc 디렉토리에 위치 시스템 관리자가 관리 사용자 초기화 파일 사용자 홈 디렉토리에 위치 사용자별 환경 설정 : 프롬프트, 경로, 앨리어스 등

80 쉘에 따른 시스템 초기화 파일 환경 설정 파일 쉘 시스템초기화파일 사용자초기화파일 본쉘 1. /etc/profile
2. $HOME/.profile 콘쉘 (ENV=$HOME/.kshrc;export ENV) 3. $HOME/.kshrc C쉘 1. /etc/.login 2. $HOME/.login 3. $HOME/.cshrc 4. $HOME/.logout

81 쉘에 따른 시스템 초기화 파일 환경 설정 파일 실행 시기 쉘 시스템 초기화 파일 사용자 초기화 파일 실행 조건 본쉘
로그인 서브쉘 로그아웃 본쉘 /etc/profile $HOME/.profile - 배시쉘 $HOME/.bash_profile .bash_profile이 없으면 $HOME/. bashrc 콘쉘 $HOME/.kshrc ENV 변수 설정 C 쉘 /etc/.login $HOME/.login $HOME/.cshrc $HOME/.logout

82 콘쉘 로그인 과정 사용자 로그인 시작 /etc/profile ~/.profile ENV 변수 ~/.kshrc login 성공
환경 설정 파일 콘쉘 로그인 과정 사용자 로그인 시작 /etc/profile ~/.profile ENV 변수 ~/.kshrc login 성공 No Yes

83 수정된 환경 설정 파일 내용의 적용 $. ~/.kshrc $. ~/.profile 환경 설정 파일 (콘쉘)
logout했다가 다시 login logout하지 않고 적용하려면 다음 명령 수행 $. ~/.kshrc $. ~/.profile Bash 쉘  $ source ~/.profile

84 $ source ~/.profile $ . ~/.profile
환경 설정 파일 (배시쉘) 수정된 환경 설정 파일 내용의 적용 logout했다가 다시 login logout하지 않고 적용하려면 위의 명령 수행 $ source ~/.profile $ ~/.profile

85 실습 순서 cd vi .profile set –a PATH=$PATH:~/bin:.
환경 설정 파일 실습 순서 cd vi .profile set –a PATH=$PATH:~/bin:. PS1=“^[[32;1m[\$PWD] ^[[0;0m” ENV=~/.kshrc set +a . ~/.profile vi .kshrc stty erase ^h TERM=vt100 alias dir=’ls -l’ . ~/.kshrc $ dir // alias 동작 확인 홈 디렉토리로 이동 자동으로 export실행토록 설정 경로설정 추가 프롬프트 변경 추가 ENV추가 자동설정 해제 .profile 수정내용 적용 .kshrc 내용 수정 alias 등 필요내용 삽입 13) .kshrc 수정내용 적용 14) alias가 동작되는지 확인

86 Lab Sessions Session I Log into the system.
Use the appropriate command to determine your login shell. 여러분의 로그인 쉘이 무엇이지 알기 위해 적절한 명령어를 사용하라. Use the /etc/passwd file to verify the result of step 2. 단계 2의 결과를 검증하기 위해 /etc/passwd 파일을 사용하라. Use the appropriate command do determine your current shell. It is the same as your login shell? 여러분의 현 쉘이 무엇인지 알기 위해 적절한 명령어를 사용하라. 현 쉘이 여러분의 로그인 쉘과 같은가? Create a subshell shell that is not the same as your login shell. For example, if your login shell is Korn, create a Bash or C shell. 여러분의 로그인 쉘과 같지 않은 하위 쉘을 하나 만들라. 예를 들면 여러분의 로그인 쉘이 큰 쉘이면, 본-어게인 쉘이나 씨-쉘을 만들라. Check the current shell again. Does it match with your login shell? 현재의 쉘이 무엇인지 다시 한번 확인하라. 여러분의 로그인 쉘과 같은가?

87 Create another shell which is the same type as your login shell
Check the current shell again. Does it match with previous one? 현재의 쉘이 무엇인지 다시 한번 확인하라. 이전 쉘과 일치하는가? Exit from the subshell shell and move to your login shell. 하위 쉘을 빠저 나가고 로그인 쉘로 돌아가라. Check the current shell again. Is it the same the login shell? 다시 한번 현재 쉘을 확인하라. 로그인 쉘과 같은가? Log out of the system.

88 Session II Log into the system. Use the who command and redirect the result to a file called file1. Use the more command to see the contents of file1. Use the date and who commands in sequence (in one line) such that the output of date will display on the screen and the output of who will be redirected to a file called file2. Use the more command to check the contents of file2. It should be the same as file1 unless someone logged in or logged off between the who commands. If your login shell is not the Korn shell, create a Korn subshell. Set vi to be your command-line editor. Use your command-line editor to recall the line you typed in step 3. Edit the line so that the output of both commands (date and who) are redirected to a file called file3. Use the more command to display the result. Use the command-line editor to recall the line you typed in step 3. Edit the line so that the output of the first command (date) is redirected to a file called file4, while the result of the who command displays on the monitor without changing the relative positions of the date and who commands. Use the more command to verify the result.

89 Make a duplicate of file3 and call it file3.bak.
Use the command-line editor to recall the line created at step 6. Edit the line so that the word “date” will be misspelled as “bate”. Execute the edited command and append the result to file3. use more command to compare file3 and file3.bak. Using the command-line editing, recall the line created in step 9. Edit it so that the errors also go to file3 without replacing the files contents. Check the contents of file3 for verification. Using the command-line editing, recall the line created in step 10. Edit it so that the output goes to file4 and errors go to file5. Log out of the system.

90 Session III Log into the system. Create a Korn subshell. Set your command-line editor to vi. Create a long list of the current directory and pipe the output to the screen using a more command. Edit the line in step 4 so that the result goes to the screen and at the same time is saved as a file called file1. (use tee, pipe and redirection) Edit the line in step 5 so that the result of ls –l command be sent to two files file2 and file3. Set the noclobber option. // not allow redirection Repeat the command-line in step 5. Do you get any error message? If so, write down the error message. Edit the command-line in step 8 to force clobbering of file1. Unset the noclobber.

91 연습문제 2. > 사용시 기존 파일의 내용을 덮어쓸 수 있다. 이를 방지하기 위한 방법을 설명하시오.
3. “ ”와‘ ’의 차이점을 설명하시오. 4. 현재 사용중인 쉘이 무엇인지 확인하는 방법을 두가지 적으시오. ※ 다음 작업을 수행하기 위한 명령을 기술하시오. 6. 현재 디렉토리에서 숫자로 시작하는 모든 파일의 상세정보를 한 화면씩 출력하시오. 7. 현재 디렉토리에 있는 파일명의 확장자가 c인 모든 파일을 /tmp로 복사하시오. 8. ls 명령 실행시 ls –aF 가 실행되도록 설정하시오. 9. 현재 히스토리 번호가 600번, 사용 중인 시스템 이름이 cookbook이고, 로그인 명이 user1 이라면 프롬프트가 이 되도록 설정하시오. (bash 쉘 상에서 ) 10. 이전의 작업 디렉토리로 이동하시오. 11. 홈 디렉토리에 있는 모든 파일의 상세 정보는 파일 ls.out에, 오류 메시지는 ls.err 파일에 저장하되, 하나의 명령으로 실행하시오.

92 연습문제 $ fc –l ... 13. 이전에 실행했던 명령들의 목록이 다음과 같을 때 물음에 답하시오. (bash 쉘)
1) 576번 작업에서 현재 디렉토리의 모든 c 소스 파일을 홈 디렉토리 아래의 C 디렉토리로 복사하려고 했는데, ~/C 디렉토리가 없어서 실패하였다. 그래서 그 다음 작업으로 ~/C 디렉토리를 생성하였고, 576 번에서 작업하였던 복사 명령을 다시 실행하려고 한다. 해당 작업을 반복하는 방법을 두가지 이상 적으시오 $ fc –l ... 569 cd .. 570 rmdir Test 571 cd ~/Unix/ch3 572 vi t2 573 cd Unix 574 history 575 fc -l 576 cp *.c ~/C 577 mkdir ~/C 578 clear $

93 2) 방금 작업했던 복사 명령을 활용하여 현재 디렉토리의 모든 c 소스 파일을 /tmp로 복사하려고 한다. 어떻게 하면 되겠는가?
3) 571번에 있는 cd 명령을 실행하지 말고 출력만 하시오. 4) 방금 작업한 명령을 반복하는 방법을 두가지 적으시오.

94 수고 하셨습니다!!!!!!!!!!!! Mail to kky!!!!!!!!!!!!!!!!!!!!!!!!


Download ppt "ㅎㅎ 콘 쉘 (배시쉘) 활용하기 기본 사용법 익히기 입출력 방향 변경 콘 쉘의 환경설정 콘 쉘 명령 다루기 프롬프트 설정하기"

Similar presentations


Ads by Google