Vous êtes sur la page 1sur 20

Chapter 8 Shell Advanced Features

Shell Variable Storage


1. Local
Private to the current shell

2. Environment
Visible to all shells Examples: PATH, HOME, TERM, SHELL

Setting Shell Variables


Syntax: variablename=value
(no space before and after the = sign) Examples: $ color=fuschia $ count=3

Variable Substitution
Syntax: $variablename

directs the shell to perform variable substitution.


Examples: $ echo $PATH $ PATH=$PATH:$HOME $ echo $PATH $ echo $HOME $ filename=$HOME/sample.txt $ more filename

Variable Substitution
The use of an absolute path name for the value of variable that references a file or directory allows you to be anywhere in the file hierarchy and still access the desired file or directory.

$ $ $ $ $ $

dir_name=tree/car.models/ford echo $dir_name ls f dir_name my_ls=ls aFC my_ls my_ls $dir_name

Command Substitution
Used to replace a command with its output within the same command line

Syntax: $(command)
Examples:

$ $ $ $ $ $ $

pwd curdir=$(pwd) echo $curdir cd /temp pwd cd $curdir pwd

Tilde Substitution
If a word begins with a tilde (~), tilde expansion is performed on that word. RULES: A tilde by itself or in front of a / is replaced by the path name set in the HOME variable $ echo $HOME $ echo ~ A tilde followed by a + is replaced with the value of the PWD variable $ cd tree $ echo $PWD $ ls ~+/tree

Tilde substitution
A tilde followed by a replaced with the value of the OLDPWD variable $ echo $OLDPWD $ ls ~ -

Displaying Variable Value


The env command can be used to display all of the variables that are currently held in the environment The set command will display all of the currently defined variable. The unset command can be used to remove the current value of the specified variable.

Transferring local variables to the environment Syntax: $ export variable Example: $ n=5 $ export n

ps Command
$ ps [-options]

use to check the PID and then kill the process if it is taking too long or has stopped

ps Command Output

Parent Child Processes

-First identity the PID of the lowest level unresponsive process -Sometimes necessary to kill the Parent of process and on rare occasions even the Parent of the Parent -Killing a parent process will kill all child processes spawned by it -Look at output to be able to trace from the child up the hierarchy to the parent processes that spawned them

Terminating Processes

$ kill -15 - soft kill


$ kill -9 - sure kill

$ pkill cmd name Solaris

Chapter 9 File Name Generation

File Name Generations and Dot Files

File name generating characters will never generate a file name that has a leading dot The leading dot in dot files must be explicitly provided

File name Generation - ?


? - Matches any single character

Examples: $ ls a
. .. .zz abc abcd bbabb cyz zzayy abcdef abcz

$ls $ls $ls $ls $ls

??? abc? ??a?? .?? ?

File name Generation [ ]


[] - defines a class of characters from which one will be

matched Examples: $ls a . .. .zz 1G 2G 7G 15G Ant Cat Dog abc abcdef ba cyz $ls [abc]??? $ls [1-9][A-Z] $ls [!A-Z]??

File name Generation *


* - Matches zero or more characters except a leading dot (.) Examples: $ ls a . .. .profile ab.dat abcd.dat abcde abcde.data $ls * $ls .* $ls *.dat $ls *e

Review
$ ls a . Abc e35f .. Abcd efg .test1 abc fe3f .test2 abcdemf fe3fg Given the directory, list all file names that 1. Contain only 5 characters 2. Contain at least 5 characters 3. Begin with an a or an A 4. Have at least 4 characters and begin with an a or an A 5. End with a sequence e, a single number, and an f 6. Begin with a dot 7. Begin with a dot, except . 8. Begin with a dot, except . and . .

Vous aimerez peut-être aussi