Más contenido relacionado
La actualidad más candente (20)
Similar a 09.using shell (20)
09.using shell
- 1. Using Shell
Minsuk Lee
Hansung University, Seoul, Korea
minsuk@hansung.ac.kr
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 2. What is Shell ?
• As a command Interpreter
– Shell interprets user command and executes program
• As a programming language
– Shell allows user to make script to do series of commands or
procedural functions
• Shell setups user configurations
– Using startup script
• Setting PATH, umask, any environment variable
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 3. Type of shells
• Bourne Shell (sh)
– Basic shell for UNIX system, from UNIX-v7
– Stephen Bourne created
– Most of the shell is base on this Bourne shell
• Korn Shell (ksh)
– David Korn (AT&T) created for UNIX SVR4
– Compatible with sh, faster
– Support history, alias
• C Shell (csh)
– Bill Joy (UCB) created for 2BSD
– C language style syntax, easy to use
• Bourne Again Shell (bash)
– Brain Fox created in 1988 based on Bourne shell
– Compatible with sh, incorporate functions from csh, ksh
– Default shell for Linux
• Tcsh, dtksh, zsh, …
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 4. How shell works ?
1. Display prompt ( $, # )
2. Read command
3. Lookup the command
4. If it‟s internal command, execute, and goto 1
5. If it‟s external command,
Fork a child process,
Let it execute the command
6. Wait for child ends
7. Goto 1
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 5. My shell ?
• Defined in /etc/passwd
– Only super-user can change it
• Shell is also a program
– You can install ksh, csh, and use it
Invoking a Bourne Shell, and exit
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 6. Environment Variables (1)
• My working environments
– Language, colors, configurations
– Used by applications
– „$ set‟ : current shell‟s env
– „$ env‟ : global env
• Can be changed
– By exporting
– e.g.,
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 7. Environment Variables (2)
• Set, unset (environment) variables
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 8. Using alias
• Rename command
Temporal unalias by „‟
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 9. functions
• Shell allows functions with arguments
– e.g., a function to make a directory and move to there
$ mkcd() { mkdir -p $1 && cd $1; }
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 10. Command history (1)
• Shell saves command history
– Up/down arrow key to repeat/edit previous commands
• Backs up history in ~/.bash_history file when logout
• You can adjust history buffer size
„$ set‟ shows all the environment variables
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 11. Command history (2)
• Repeat specific command (1) !
– See history by $ history or $ history | more
– Try $ !<item>
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 12. Command history (3)
• Repeat specific command (2) !
– See history by $ history or $ history | more
– Try $ !<initial letter of previous command>
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 13. Auto complete
• When type [TAB] in
– Shell completes word, and/or shows choices
– The word can be the name of commands, files
– If you type „$ ls –l my[TAB]‟
• Shell completes „$ ls –l myfile‟, and if you type [TAB] twice,
• Shell shows
• After type „-‟, shell shows two file stating with “myfile-”
• …
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 14. Bash configuration files
• When login bash reads next script files in order
– /etc/profile // global profile
– ~/.bash_profile
– ~/.bash_login // seldom used
– ~/.profile // Only When .bash_profile, .bash_login does not exist
– ~/.bashrc // when directed to run in above files
• When logout
– ~/.bash_logout
• OR you can run shell script anytime by
– „$ source <shell-script-file>‟ or „$ . <shell-script-file>‟
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 15. What you can do with scripts ?
• Changing prompt
– variable: PS1
`command` works !
• Changing command path
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 16. Special Characters for shell
char function example
; Multiple commands $ cd /tmp; ls
*, ?, [ ] Wild cards (any string, one, choice) $ ls h?[a-z]*
>, >>, < Redirection $ ls > ls.out
| Pipe $ ls –l /etc | more
~ Home directory $ cd ~myuser
- Previous working directory $ cd -
„„ Ignore shell special character $ print „$SHELL‟
““ Ignore special character except $, `, $ print “$SHELL”
`` Run shell command $ print `date`
Escape a shell special character $ print “$SHELL”
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 18. Let‟s practice
• Repeat the command sequence of the
screenshots
• Write a shell function tc()
– To replace „$ tar cvf file1.tar file1 file2 …‟
into „$ tc file1 file2 …‟
• $1 : first argument
• $* : all arguments
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA