Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.

Similar presentations


Presentation on theme: "Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control."— Presentation transcript:

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

2 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. set new values for some variables to customize the shell. find out the value of some variables to help accomplish a task. 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. Shell Variables: only effective within this current shell. Environment Variables: automatically exported to other applications invoked Environment Variables: automatically exported to other applications invoked

3 Shell Variables Operations  Set the variables % set var = % set var = % setenv VAR % setenv VAR  Unset the variables % unset var % unsetenv VAR  Display the variables % set % setenv  Display the value of the variables % echo $var % echo $VAR

4 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

5 Variable Types  Strings and arrays  Arrays hold lists of strings % set array_name=(string1 string2…) % set array_name=(string1 string2…)  % set test1 = “Hello World”  % set test2 = (Hello World)  % set test3 = ($test1)  % set test3 = ($test3 My Name Is Scott)

6 Variables Expressions  Using variables expressions, we can extract 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

7 C Shell Command History  C shell maintains a history of commands executed up to $history maximum Command substitution Command substitution Command history modifiers Command history modifiers Argument substitution 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

8 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 !-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

9 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

10 Command History Modifiers  These modifiers can define the way of executing commands from history :p – display the command, but doesn’t execute :p – display the command, but doesn’t execute :s/old/new – substitute the first instance of old with new :s/old/new – substitute the first instance of old with new :gs/old/new – substitute all instances of old with new :gs/old/new – substitute all instances of old with new

11 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

12 Example % cat test1 test2 test3 % ls !!^ % grep string !cat:1 % ^string^newstring^:p % !cat:gs/t/T

13 Job Control  The shell allows you to manage jobs place jobs in the background place jobs in the background move a job to the foreground move a job to the foreground suspend a job suspend a job kill a job kill a job get information about a job get information about a job

14 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 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. you can have a bunch of jobs running at once with a single terminal.

15 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

16 Job ID Expressions  Every job is assigned a job ID  To access job with job id, start with %: %n : job number n %n : job number n %string: job whose command line starts with string %string: job whose command line starts with string %?string: job whose command line contains string %?string: job whose command line contains string %: current job %: current job %-: previous job %-: previous job  Can use job ID with fg, bg, and kill

17 Job Control Examples  % find. –name myfile.txt Ctrl-zSuspended  % jobs [1] + Suspended find. -name my.txt  %emacs newfile & [2] 17150  % jobs [1] + Suspendedfind. -name my.txt [2] - Runningemacs newfile  % bg %1 [1] find. -name my.txt & [1] Done find. -name my.txt  % kill %2 [2] Terminated emacs newfile

18 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 - previous working directory is added n : nth directory from the stack n : nth directory from the stack  popd – removes top directory from the stack and changes to that directory

19 Recommended Reading  Chapter 9, sections 9.3, 9.5, 9.6, 9.10, 9.12


Download ppt "Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control."

Similar presentations


Ads by Google