lecture 3 shell variables shell command history job / process control directory control

19
Lecture 3 Lecture 3 Shell Variables Shell Variables Shell Command History Shell Command History Job / Process Control Job / Process Control Directory Control Directory Control

Upload: dwain-walsh

Post on 24-Dec-2015

223 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Lecture 3Lecture 3

Shell VariablesShell VariablesShell Command HistoryShell Command HistoryJob / Process ControlJob / Process ControlDirectory ControlDirectory Control

Page 2: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Shell VariablesShell Variables The shell keeps track of a set of parameter names The shell keeps track of a set of parameter names

and values, which determine the behavior of the and values, which determine the behavior of the shell.shell. set new values for some variables to customize the set new values for some variables to customize the

shell.shell. find out the value of some variables to help accomplish a find out the value of some variables to help accomplish a

task.task.

C shell maintains two sets of variables:C shell maintains two sets of variables: Shell Variables: only effective within this current shell.Shell Variables: only effective within this current shell. Environment Variables: automatically exported to other Environment Variables: automatically exported to other

applications invokedapplications invoked

Page 3: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Shell Variables OperationsShell Variables Operations Set the variablesSet the variables

% set var = <value>% set var = <value>% setenv VAR <value>% setenv VAR <value>

Unset the variablesUnset the variables% unset var% unset var% unsetenv VAR% unsetenv VAR

Display the variablesDisplay the variables% set% set% setenv% setenv

Display the value of the variablesDisplay the value of the variables% echo $var % echo $var % echo $VAR% echo $VAR

Page 4: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Predefined Shell VariablesPredefined Shell Variables $ argv: list of arguments passed to the current $ argv: list of arguments passed to the current

commandcommand $ cwd: full pathname of current dir$ cwd: full pathname of current dir $ history: number of commands saved in history$ history: number of commands saved in history $ home: home dir (~)$ home: home dir (~) $ path: list of pathnames to search for commands $ path: list of pathnames to search for commands

to executeto execute $ shell: name of shell in use (ex: /bin/csh)$ shell: name of shell in use (ex: /bin/csh) $ status: exit status of last command$ status: exit status of last command

Page 5: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Variable TypesVariable Types

Strings and arraysStrings and arrays Arrays hold lists of stringsArrays hold lists of strings

% set array_name=(string1 string2…)% set array_name=(string1 string2…) % set test1 = “Hello World”% set test1 = “Hello World” % set test2 = (Hello World)% set test2 = (Hello World) % set test3 = ($test1)% set test3 = ($test1) % set test3 = ($test3 My Name Is Scott)% set test3 = ($test3 My Name Is Scott)

Page 6: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Variables Expressions Variables Expressions Using variables expressions, we can extract Using variables expressions, we can extract

other info. about the shell variables: other info. about the shell variables: $var or ${var}: value of variable var$var or ${var}: value of variable var $?var or ${?var}: 1- if var is set ; 0 – if var is not set$?var or ${?var}: 1- if var is set ; 0 – if var is not set $var[1] or ${var[1]}: first word in the value of var$var[1] or ${var[1]}: first word in the value of var

• ${var[-10]}: words 1-10 in var${var[-10]}: words 1-10 in var• ${var[2-]}: words staring from word 2${var[2-]}: words staring from word 2

$var[*] or ${var[*]}: all words in the value of var$var[*] or ${var[*]}: all words in the value of var $0: name of the program being executed$0: name of the program being executed $<: read a line from stdin$<: read a line from stdin

Page 7: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

C Shell Command HistoryC Shell Command History C shell maintains a history of commands C shell maintains a history of commands

executed up to $history maximumexecuted up to $history maximum Command substitutionCommand substitution Command history modifiersCommand history modifiers Argument substitutionArgument substitution

Current history can be examined by Current history can be examined by % history% history

Upon logout, up to $savehist most recent Upon logout, up to $savehist most recent commands from the history are saved in commands from the history are saved in ~/.history, s.t. these commands can be reloaded ~/.history, s.t. these commands can be reloaded next time the shell is startednext time the shell is started

Page 8: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Command SubstitutionCommand Substitution !! : previous command!! : previous command !!!!stringstring: previous command with : previous command with stringstring appended appended !N : command number N in history!N : command number N in history

!-N: N-th command back from the current command!-N: N-th command back from the current command

!string : most recent command that starts with string!string : most recent command that starts with string !?string?: most recent command that contains string!?string?: most recent command that contains string !$: last argument of previous command!$: last argument of previous command !{str1}str2: get most recent command starting with str1 !{str1}str2: get most recent command starting with str1

and append str2and append str2 ^old^new^: change old to new in previous command and ^old^new^: change old to new in previous command and

execute itexecute it

Page 9: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Command Substitution ExampleCommand Substitution Example% grep “this string” ReadMe.txt% grep “this string” ReadMe.txt

% ^R^r^% ^R^r^grep “this string” readMe.txtgrep “this string” readMe.txt

% more !$% more !$more readMe.txtmore readMe.txt{file contents}{file contents}

% !g% !ggrep “this string” readMe.txtgrep “this string” readMe.txt

Page 10: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Command History ModifiersCommand History Modifiers

These modifiers can define the way of These modifiers can define the way of executing commands from historyexecuting commands from history :p – display the command, but doesn’t :p – display the command, but doesn’t

executeexecute :s/old/new – substitute the first instance of old :s/old/new – substitute the first instance of old

with newwith new :gs/old/new – substitute all instances of old :gs/old/new – substitute all instances of old

with newwith new

Page 11: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Arguments SubstitutionArguments Substitution

:0 – command name:0 – command name :n – argument number n:n – argument number n ^ - first argument^ - first argument $ - last argument$ - last argument :n-m – arguments n through m:n-m – arguments n through m :n* - arguments n through the last one:n* - arguments n through the last one * - All arguments* - All arguments

Page 12: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

ExampleExample

% cat test1 test2 test3% cat test1 test2 test3

% ls !!^% ls !!^

% grep string !cat:1% grep string !cat:1

% ^string^newstring^:p% ^string^newstring^:p

% !cat:gs/t/T% !cat:gs/t/T

Page 13: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Job Control Job Control

The shell allows you to manage The shell allows you to manage jobsjobs place place jobsjobs in the in the backgroundbackground move a job to the foregroundmove a job to the foreground suspend a jobsuspend a job kill a jobkill a job get information about a jobget information about a job

Page 14: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Background jobsBackground jobs

If you follow a command line with "&", the If you follow a command line with "&", the shell will run the shell will run the jobjob in the background. in the background. you don't need to wait for the job to complete, you don't need to wait for the job to complete,

you can type in a new command right away.you can type in a new command right away. you can have a bunch of jobs running at once you can have a bunch of jobs running at once

with a single terminal.with a single terminal.

Page 15: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Job Control CommandsJob Control Commands

jobs: list current jobsjobs: list current jobs Ctrl-z: suspends the foreground jobCtrl-z: suspends the foreground job Ctrl-c: kill the foreground jobCtrl-c: kill the foreground job bg: run the most recently suspended job in the bg: run the most recently suspended job in the

backgroundbackground fg: move the most recently backgrounded job fg: move the most recently backgrounded job

from the background into the foregroundfrom the background into the foreground kill: terminate a jobkill: terminate a job

kill [-s signal] pid (NOTE: find pid with ps)kill [-s signal] pid (NOTE: find pid with ps)kill –l : list the kill signalskill –l : list the kill signals

Page 16: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Job ID ExpressionsJob ID Expressions Every job is assigned a job IDEvery job is assigned a job ID To access job with job id, start with %:To access job with job id, start with %:

%n : job number n%n : job number n %string: job whose command line starts with %string: job whose command line starts with

stringstring %?string: job whose command line contains %?string: job whose command line contains

stringstring %%: current job%%: current job %-: previous job%-: previous job

Can use job ID with fg, bg, and killCan use job ID with fg, bg, and kill

Page 17: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Job Control ExamplesJob Control Examples % find . –name myfile.txt% find . –name myfile.txt

Ctrl-zCtrl-zSuspendedSuspended

% jobs% jobs[1] + Suspended find . -name my.txt[1] + Suspended find . -name my.txt

%emacs newfile &%emacs newfile &[2] 17150[2] 17150

% jobs% jobs[1] + Suspended[1] + Suspended find . -name my.txtfind . -name my.txt[2] - Running[2] - Running emacs newfileemacs newfile

% bg %1% bg %1[1] find . -name my.txt &[1] find . -name my.txt &[1] Done [1] Done find . -name my.txtfind . -name my.txt

% kill %2% kill %2[2] Terminated [2] Terminated emacs newfileemacs newfile

Page 18: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Directory ControlDirectory Control

The shell maintains a directory stackThe shell maintains a directory stack pushd dir: causes dir to be added to the pushd dir: causes dir to be added to the

directory stack and changes to that directory stack and changes to that directorydirectory - previous working directory is added- previous working directory is added n : nth directory from the stackn : nth directory from the stack

popd – removes top directory from the popd – removes top directory from the stack and changes to that directory stack and changes to that directory

Page 19: Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control

Recommended ReadingRecommended Reading

Chapter 9, sections 9.3, 9.5, 9.6, 9.10, Chapter 9, sections 9.3, 9.5, 9.6, 9.10, 9.129.12