190 likes | 316 Views
Lecture 3. Shell Variables Shell Command History Job / Process Control Directory Control. Shell Variables. The shell keeps track of a set of parameter names and values, which determine the behavior of the shell. set new values for some variables to customize the shell.
E N D
Lecture 3 Shell Variables Shell Command History Job / Process Control Directory Control
Shell Variables • The shell keeps track of a set of parameter names and values, which determine the behavior of the shell. • set new values for some variables to customize the shell. • find out the value of some variables to help accomplish a task. • C shell maintains two sets of variables: • Shell Variables: only effective within this current shell. • Environment Variables: automatically exported to other applications invoked
Shell Variables Operations • Set the variables % set var = <value> % setenv VAR <value> • Unset the variables % unset var % unsetenv VAR • Display the variables % set % setenv • Display the value of the variables % echo $var % echo $VAR
Predefined Shell Variables • $ argv: list of arguments passed to the current command • $ cwd: full pathname of current dir • $ history: number of commands saved in history • $ home: home dir (~) • $ path: list of pathnames to search for commands to execute • $ shell: name of shell in use (ex: /bin/csh) • $ status: exit status of last command
Variable Types • Strings and arrays • Arrays hold lists of strings • % set array_name=(string1 string2…) • % set test1 = “Hello World” • % set test2 = (Hello World) • % set test3 = ($test1) • % set test3 = ($test3 My Name Is Scott)
Variables Expressions • Using variables expressions, we can extract other info. about the shell variables: • $var or ${var}: value of variable var • $?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[-10]}: words 1-10 in var • ${var[2-]}: words staring from word 2 • $var[*] or ${var[*]}: all words in the value of var • $0: name of the program being executed • $<: read a line from stdin
C Shell Command History • C shell maintains a history of commands executed up to $history maximum • Command substitution • Command history modifiers • Argument substitution • Current history can be examined by % history • Upon logout, up to $savehist most recent commands from the history are saved in ~/.history, s.t. these commands can be reloaded next time the shell is started
Command Substitution • !! : previous command • !!string: previous command with string appended • !N : command number N in history • !-N: N-th command back from the current command • !string : most recent command that starts with string • !?string?: most recent command that contains string • !$: last argument of previous command • !{str1}str2: get most recent command starting with str1 and append str2 • ^old^new^: change old to new in previous command and execute it
Command Substitution Example % grep “this string” ReadMe.txt % ^R^r^ grep “this string” readMe.txt % more !$ more readMe.txt {file contents} % !g grep “this string” readMe.txt
Command History Modifiers • These modifiers can define the way of executing commands from history • :p – display the command, but doesn’t execute • :s/old/new – substitute the first instance of old with new • :gs/old/new – substitute all instances of old with new
Arguments Substitution • :0 – command name • :n – argument number n • ^ - first argument • $ - last argument • :n-m – arguments n through m • :n* - arguments n through the last one • * - All arguments
Example % cat test1 test2 test3 % ls !!^ % grep string !cat:1 % ^string^newstring^:p % !cat:gs/t/T
Job Control • The shell allows you to manage jobs • place jobs in the background • move a job to the foreground • suspend a job • kill a job • get information about a job
Background jobs • If you follow a command line with "&", the shell will run the job in the background. • you don't need to wait for the job to complete, you can type in a new command right away. • you can have a bunch of jobs running at once with a single terminal.
Job Control Commands • 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] pid (NOTE: find pid with ps) kill –l : list the kill signals
Job ID Expressions • Every job is assigned a job ID • To access job with job id, start with %: • %n : job number n • %string: job whose command line starts with string • %?string: job whose command line contains string • %%: current job • %-: previous job • Can use job ID with fg, bg, and kill
Job Control Examples • % find . –name myfile.txt Ctrl-z Suspended • % jobs [1] + Suspended find . -name my.txt • %emacs newfile & [2] 17150 • % jobs [1] + Suspended find . -name my.txt [2] - Running emacs newfile • % bg %1 [1] find . -name my.txt & [1] Done find . -name my.txt • % kill %2 [2] Terminated emacs newfile
Directory Control • The shell maintains a directory stack • pushd dir: causes dir to be added to the directory stack and changes to that directory • - previous working directory is added • n : nth directory from the stack • popd – removes top directory from the stack and changes to that directory
Recommended Reading • Chapter 9, sections 9.3, 9.5, 9.6, 9.10, 9.12