Cse4251 the unix programming environment
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

CSE4251 The Unix Programming Environment PowerPoint PPT Presentation


  • 89 Views
  • Uploaded on
  • Presentation posted in: General

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: & , ; , ? , && , || ,

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cse4251 the unix programming environment

CSE4251 The Unix Programming Environment

Lecture 4

Shell environment III:

- command alias & history;

job control;

editor (vim)


Recap

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


Alias

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’


Alias1

alias

  • shortcuts for long/complex commands

    • show all aliases defined:

      $ alias

[23:58:29][[email protected]:~]

$ 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‘

...


Alias2

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)


Command history

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


Command history1

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


Job control

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


Background jobs

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# )


Background jobs1

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


Background jobs2

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


Background jobs3

Background jobs

  • Throw a job into background (suspend a job)

    • [ctrl]-z

  • List current background jobs

    • $ jobs

[17:41:57][[email protected]:~]

$ vi ~/.bashrc

[1]+ Stopped vim ~/.bashrc

[18:04:24][[email protected]:~]

$

$ jobs -l

[1]+ 7903 Stopped vim ~/.bashrc


Background jobs4

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][[email protected]:~]

$ vi ~/.bashrc

[1]+ Stopped vim ~/.bashrc

[18:04:24][[email protected]:~]

$ fg %1


Kill jobs

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][[email protected]:~]

$ kill -9 %1; jobs

[1]+ Stopped vim ~/.bashrc

[18:30:30][[email protected]:~]

$ jobs

[1]+ Killed vim ~/.bashrc

[18:30:51][[email protected]:~]

$ jobs

[18:30:58][[email protected]:~]

$


Summary of job control

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


Editor

editor

  • recommended: either vi/vim or emacs


Vi vim 101

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, ...)


Vi vim 1011

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


Vi vim 1012

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


Vi vim 1013

vi/vim 101

  • enter edit mode: type i

typing “i” tells the editor you want to insert text


Vi vim 1014

vi/vim 101

  • edit text: type “hello,” [Enter], “world!”

type letters normally;

[Enter] to start a newline

keep showing your current mode


Vi vim 1015

vi/vim 101

  • finish editing, exit edit mode and return to command mode: [Esc]

after [Esc], you have returned to command mode


Vi vim 1016

vi/vim 101

  • enter a command: type :wq, then [Enter]


Vi vim 1017

vi/vim 101

  • You have successfully created, edited, and saved a file using vim

[21:25:19][[email protected]:~]

$ ls -l myscript.sh

-rw-rw-r--. 1 zhengmzhengm 14 Sep 25 21:25 myscript.sh

[21:25:37][[email protected]:~]

$ cat myscript.sh

hello,

world!

[21:25:51][[email protected]:~]

$


More resources

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


  • Login