Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Shell environment III: - command alias & history; - job control; - editor (vim) CSE4251 The Unix Programming Environment 1.

Similar presentations


Presentation on theme: "Lecture 4 Shell environment III: - command alias & history; - job control; - editor (vim) CSE4251 The Unix Programming Environment 1."— Presentation transcript:

1 Lecture 4 Shell environment III: - command alias & history; - job control; - editor (vim) CSE4251 The Unix Programming Environment 1

2 Recap Shell environment II – I/O redirection & pipe:, >>, 2>, 2>>, 2>&1, |, tee – building complex commands: &, ;, ?, &&, ||, – shell variables: e.g., export PATH in ~/.bashrc – Lab1: DUE 11:59pm, Tuesday, Feb 17, 2015 2

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 $ unalias rm $ unalias cls $ 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 $ echo $HISTSIZE 1000 $ history 3 1003 history 1004 vim ~/.bash_history 1005 history 3

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 $ tar -zvcf /tmp/etc.tar.gz /etc & [1] 7507 run in background job #PID (process# ) E.g., start a background job:

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 [1]+ Done tar -zvcf /tmp/etc.tar.gz /etc the cmd for the job job # finished successfully E.g., when the background job completes:

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 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 Background jobs

14 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:~] $ Kill jobs

15 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 [-signal#] %job_number kill –l : list the kill signals summary of job control

16 editor recommended: either vi/vim or emacs

17 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,...) vi/vim 101

18 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 vi/vim 101

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

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

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

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

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

24 You have successfully created, edited, and saved a file using vim vi/vim 101 [21:25:19][zhengm@apollo-tesla:~] $ ls -l myscript.sh -rw-rw-r--. 1 zhengm zhengm 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

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


Download ppt "Lecture 4 Shell environment III: - command alias & history; - job control; - editor (vim) CSE4251 The Unix Programming Environment 1."

Similar presentations


Ads by Google