1 / 26

CSE4251 The Unix Programming Environment

CSE4251 The Unix Programming Environment. Lecture 4 Shell environment III: - command alias & history; job control; editor ( vim). Recap. Shell environment II I/O redirection & pipe: < , > , >> , 2> , 2>> , 2>&1 , | , tee building complex commands: & , ; , ? , && , || ,

kesler
Télécharger la présentation

CSE4251 The Unix Programming Environment

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. CSE4251 The Unix Programming Environment Lecture 4 Shell environment III: - command alias & history; job control; editor (vim)

  2. Recap • Shell environment II • I/O redirection & pipe: <, >, >>, 2>, 2>>, 2>&1, |, tee • building complex commands: &, ;, ?, &&, ||, • shell variables: e.g., exportPATH in ~/.bashrc • Lab1: DUE 11:59pm, Monday, Sept 29, 2014

  3. alias • shortcuts for long/complex commands • set alias: $ alias short_name=‘long_complex_command’ E.g., $ alias lm=‘ls –l | more’ #ls page by page E.g., $ alias rm=‘rm –i’ #prompt before every removal E.g., simulate DOS commands cls and dir : $ alias cls=‘clear’ #clear the screen$ alias dir=‘ls -l’

  4. alias • shortcuts for long/complex commands • show all aliases defined: $ alias [23:58:29][zhengm@apollo-tesla:~] $ alias ... alias ls='ls --color=auto' alias ll='ls -l --color=auto‘ alias vi='vim‘ alias cdmy='cd /home/zhengm/0repos/emulator-iscsi/pfe_apps/MySQL‘ ...

  5. alias • shortcuts for long/complex commands • unset alias: unalias $ unalias lm $ unaliasrm $ unaliascls $ unalias dir • difference b/w alias and variable • aliases are used directly as “new” commands • variables are used as part of other commands (e.g., echo $SHELL)

  6. command history • shell saves recently used commands • list all saved commands: $ history or, use alias to save some typing in the future: $ alias h=‘history’ $ h • list N most recent commands: $ history N $ history 3 • size of history: $ echo $HISTSIZE $ history 3 1003 history 1004 vim ~/.bash_history 1005 history 3 $ echo $HISTSIZE 1000

  7. command history • invoke previously used commands • execute the last command again: $ !! • execute a command based on cmd#: $ !cmd_number E.g., $ !1005 #equal to $ man rm • execute a command based on (partial) name: $ !cmd_name E.g., $ !al #the last command begin w/ “al” #equal to $ alias • where is history stored: ~/.bash_history $ history ... 1005 man rm 1006 alias 1007 man history 1008 history

  8. Job control • The shell allows you to run/manage multiple jobs in one terminal • place jobs in the background • you can have multiple background jobs • two background states: stopped and running • move a job to the foreground • you can have only one foreground job (in one terminal) • suspend a job • kill a job • get information about a job

  9. Background jobs • If you follow a command with "&", the shell will run the job in the background • don't need to wait for the job to complete, you can type in a new command right away • can have a bunch of jobs running in the background E.g., start a background job: $ tar -zvcf/tmp/etc.tar.gz /etc & [1] 7507 run in background job # PID (process# )

  10. Background jobs • If you follow a command with "&", the shell will run the job in the background • don't need to wait for the job to complete, you can type in a new command right away • can have a bunch of jobs running in the background E.g., when the background job completes: [1]+ Done tar -zvcf/tmp/etc.tar.gz /etc job # finished successfully the cmd for the job

  11. Background jobs • If you follow a command with "&", the shell will run the job in the background • don't need to wait for the job to complete, you can type in a new command right away • can have a bunch of jobs running in the background • still output to the screen by default (stdout & stderr); • use I/O redirection to save stdout/stderr in log file and thus avoid interference $ tar -zvcf/tmp/etc.tar.gz /etc > /tmp/log.txt 2>&1 & [1] 7529

  12. Background jobs • Throw a job into background (suspend a job) • [ctrl]-z • List current background jobs • $ jobs [17:41:57][zhengm@apollo-tesla:~] $ vi ~/.bashrc [1]+ Stopped vim ~/.bashrc [18:04:24][zhengm@apollo-tesla:~] $ $ jobs -l [1]+ 7903 Stopped vim ~/.bashrc

  13. Background jobs • make a stopped background job run in background • $ bg %job_number • bring a background job to foreground • $ fg %job_number E.g. $ fg %1 [17:41:57][zhengm@apollo-tesla:~] $ vi ~/.bashrc [1]+ Stopped vim ~/.bashrc [18:04:24][zhengm@apollo-tesla:~] $ fg %1

  14. Kill jobs • kill the foreground job • [ctrl]-c • kill a background job • $ kill –signal %job_number E.g., $ kill -9 %1 • Check signals • $ kill –l E.g., 9) SIGKILL 15) SIGTERM $ jobs [1]+ Stopped vim ~/.bashrc [18:30:12][zhengm@apollo-tesla:~] $ kill -9 %1; jobs [1]+ Stopped vim ~/.bashrc [18:30:30][zhengm@apollo-tesla:~] $ jobs [1]+ Killed vim ~/.bashrc [18:30:51][zhengm@apollo-tesla:~] $ jobs [18:30:58][zhengm@apollo-tesla:~] $

  15. summary of job control • jobs: list current jobs • [ctrl]-z: suspends the foreground job • [ctrl]-c: kill the foreground job • bg: run the most recently suspended job in the background • fg: move the most recently backgrounded job from the background into the foreground • kill: terminate a job kill [-s signal] %job_number kill –l : list the kill signals

  16. editor • recommended: either vi/vim or emacs

  17. vi/vim 101 • vi exists in every Unix-like OSes • default interface for important commands/tools, e.g.: visudo #edit superuser privileges crontab #schedule auto-run programs • a “modal” editor: has two modes • edit mode: your keyboard is the one you’re familiar with (e.g., typing “i” means printing “i” on screen), just like typing in gedit or MS Word • command mode: letters have special meaning; do some special tasks (e.g., jump to line# 10,000 directly, replace all “cse4241” with “cse4251” in one go, delete 100 lines below, save and quit, ...)

  18. vi/vim 101 • vim is an improved version of vi • more functionalities, easier to use • similar to ~/.bashrc, vim has a configuration file called ~/.vimrc Example configurations: set nu " show line number set nonu " no line number set tabstop=4 " numbers of spaces of tab character syntax on " syntax highlight

  19. vi/vim 101 • open an file: $ vim myscript.sh cursor line# (if enabled) ~ means empty line information about file and command; default in command mode

  20. vi/vim 101 • enter edit mode: type i typing “i” tells the editor you want to insert text

  21. vi/vim 101 • edit text: type “hello,” [Enter], “world!” type letters normally; [Enter] to start a newline keep showing your current mode

  22. vi/vim 101 • finish editing, exit edit mode and return to command mode: [Esc] after [Esc], you have returned to command mode

  23. vi/vim 101 • enter a command: type :wq, then [Enter]

  24. vi/vim 101 • You have successfully created, edited, and saved a file using vim [21:25:19][zhengm@apollo-tesla:~] $ ls -l myscript.sh -rw-rw-r--. 1 zhengmzhengm 14 Sep 25 21:25 myscript.sh [21:25:37][zhengm@apollo-tesla:~] $ cat myscript.sh hello, world! [21:25:51][zhengm@apollo-tesla:~] $

  25. More resources • How to use vi • http://www.washington.edu/computing/unix/vi.html • http://www.openvim.com/tutorial.html • How to use Emacs • http://zoo.cs.yale.edu/classes/cs210/help/emacs.html

More Related