shell programming features “full” programming language “full” programming language...

23
Shell Programming Shell Programming Features Features Full” programming language Full” programming language Conditional statements Conditional statements Arithmetic, String, File, Arithmetic, String, File, Environment variables Environment variables Loop statements (for, while) Loop statements (for, while) Lists (AND, OR) Lists (AND, OR) Functions Functions Small” differences among Small” differences among shells shells Bash has 3000+ line man page Bash has 3000+ line man page

Upload: angelina-higgins

Post on 18-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Shell Programming Shell Programming FeaturesFeatures

““Full” programming languageFull” programming language Conditional statementsConditional statements Arithmetic, String, File, Environment Arithmetic, String, File, Environment

variablesvariables Loop statements (for, while) Loop statements (for, while) Lists (AND, OR)Lists (AND, OR) FunctionsFunctions

““Small” differences among shellsSmall” differences among shells Bash has 3000+ line man pageBash has 3000+ line man page

Page 2: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

VariablesVariables

variable format:variable format:variable = valuevariable = value(there should not be any spaces around the =)(there should not be any spaces around the =)

Example:Example:$number=7$number=7

$ echo The value of number is $number:$ echo The value of number is $number:

The value of number is 7The value of number is 7

$$

Page 3: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Using QuotesUsing Quotes Four types of quote charactersFour types of quote characters

‘‘ single quote charactersingle quote character

““ double quote characterdouble quote character

\\ backslash characterbackslash character

`̀ back quote characterback quote character

Example (single quote):Example (single quote):$ cat city$ cat city

AustinAustin

DallasDallas

Ft. WorthFt. Worth

San AntonioSan Antonio

$ grep ‘San Antonio’ city$ grep ‘San Antonio’ city

San AntonioSan Antonio

$$

Page 4: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Using QuotesUsing Quotes

Example (double quote):Example (double quote):

$ex1= “‘Dallas,’ A city in Texas”$ex1= “‘Dallas,’ A city in Texas”

$echo $ex1$echo $ex1

‘‘Dallas,’ A city in TexasDallas,’ A city in Texas

$ex2=‘ “San Antonio,” is also a city in Texas’$ex2=‘ “San Antonio,” is also a city in Texas’

$echo $ex2$echo $ex2

““San Antonio,” is also a city in TexasSan Antonio,” is also a city in Texas

$$

Page 5: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Using Quotes - (cont)Using Quotes - (cont) Example (backslash):Example (backslash):$echo $ex2$echo $ex2

““San Antonio,” is also a city in TexasSan Antonio,” is also a city in Texas

$$

$ echo \$ex2$ echo \$ex2

$ex2$ex2

$$

which is the same as:which is the same as:

$echo ‘$’ ex2$echo ‘$’ ex2

$ex2$ex2

$$

Page 6: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Using QuotesUsing Quotes

Example (back quote)Example (back quote)

$ echo The date and time is: `date`$ echo The date and time is: `date`

The date and time is: Tue Oct 10 10:18:34 EDT 2006The date and time is: Tue Oct 10 10:18:34 EDT 2006

$$

Page 7: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Making Decisions - IfMaking Decisions - If if format:if format:

if expressionif expression

then commandsthen commands

elif commandselif commands

else commandselse commands

fifi

Page 8: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Making Decisions - Test Making Decisions - Test (cont)(cont)

Relational operatorsRelational operators -eq-eq -ne-ne -lt-lt -gt-gt -le-le -ge-ge

Page 9: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Making Decisions - Test Making Decisions - Test (cont)(cont)

Test file operators returns true if:Test file operators returns true if:-d -d filefile file is a directoryfile is a directory-f -f filefile file is an ordinary file, and existsfile is an ordinary file, and exists-r -r filefile file is readable by the processfile is readable by the process-s -s filefile file isn’t emptyfile isn’t empty-w -w file file is writable by the processfile file is writable by the process-x -x filefile file is executablefile is executable-G -G filefile file is owned by the group I belong tofile is owned by the group I belong to-O -O filefile file is owned by userfile is owned by user-u -u filefile set_user_id bit is setset_user_id bit is set-g -g filefile set_group_id bit is setset_group_id bit is set

-

Page 10: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Making DecisionsMaking DecisionsTestTest

ExamplesExamples[ -f /usr/train1/file1 ] [ -f /usr/train1/file1 ]

if file1 exists and is ordinary fileif file1 exists and is ordinary file

[ -r /usr/train2/file1 ] [ -r /usr/train2/file1 ]

if file1 exists and is readable by this if file1 exists and is readable by this processprocess

[ -s /usr/train3/file1 ] [ -s /usr/train3/file1 ]

if file1 exists and is not emptyif file1 exists and is not empty

Page 11: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Making Decisions Making Decisions TestTest

Test logical operatorsTest logical operators! expression! expression logical negationlogical negation

expression -a expressionexpression -a expression logical andlogical and

expression -o expressionexpression -o expression logical orlogical or Examples:Examples:[ ! -f /usr/train1/file1 ] [ ! -f /usr/train1/file1 ]

[ -f /usr/train1/file1 -a -r /usr/train1/file1 ][ -f /usr/train1/file1 -a -r /usr/train1/file1 ]

[ -n “$var1” -o -r /usr/train2/file1 ][ -n “$var1” -o -r /usr/train2/file1 ]

Page 12: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Looping ConstructsLooping ConstructsForFor

For format:For format:for variable in for variable in

item1 item2..item3item1 item2..item3

dodo

command(s)command(s)

donedone

Page 13: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

User EnvironmentUser Environment

During logon a distinct environment During logon a distinct environment is created separate from other usersis created separate from other users

Page 14: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Local VariablesLocal Variables

Value cannot be changed by other Value cannot be changed by other subshellssubshells

Will Will notnot be passed to other subshells be passed to other subshells

Page 15: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Local Variables - (cont)Local Variables - (cont) Example:Example:> cat test1> cat test1

echo %$var1%echo %$var1%

$test1$test1

% %% %

var1 is equal to nullvar1 is equal to null

> cat test2> cat test2

var2=25var2=25

echo %$var2%echo %$var2%

>>

>test2>test2

%25 %%25 %

var2 is equal to 25var2 is equal to 25

Page 16: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Exported VariablesExported Variables Export command format:Export command format:

export export varsvars

Allows a value of a variable to be passed Allows a value of a variable to be passed to other subshellsto other subshells

Changing an exported variable in a Changing an exported variable in a subshell does not change the value in subshell does not change the value in the parent shellthe parent shell

Page 17: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Exported VariablesExported Variables Example:Example:> cat test3> cat test3

var3 = 2345var3 = 2345

echo var3 = $var3echo var3 = $var3

> test 3> test 3

var3 = 2345var3 = 2345

$cat test4$cat test4

echo var3 = $var3echo var3 = $var3

> test4> test4

var3 = var3 =

var3 is a local variable so its value in test3 is 2345. In test4 it is var3 is a local variable so its value in test3 is 2345. In test4 it is null.null.

Page 18: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Exported VariablesExported Variables(cont)(cont)

Another Example:Another Example:$ var3=2345$ var3=2345

$ export var3 $ export var3

$cat test3$cat test3

echo var3 = $var3echo var3 = $var3

$test3$test3

var3 = 2345var3 = 2345

$test4$test4

var3 = 2345var3 = 2345

$ $ var3 is an exported variable so its value in test3 is 2345. var3 is an exported variable so its value in test3 is 2345. In test4 it is also 2345In test4 it is also 2345

Page 19: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

Exported Variables - (cont)Exported Variables - (cont) Once an exported variable always an Once an exported variable always an

exported variable. (Unless you use unset exported variable. (Unless you use unset to destroy it.)to destroy it.)

Each subshell makes its own copy of the Each subshell makes its own copy of the variablevariable

Export with no arguments lists the Export with no arguments lists the variables that are exported to users shellvariables that are exported to users shell

Page 20: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

env Commandenv Command

env [option] [var = val] [command]env [option] [var = val] [command] displays current environment or displays current environment or

modifies variables specified.modifies variables specified. Commands specified are executed in Commands specified are executed in

the new environmentthe new environment

Page 21: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

SubshellsSubshells

A new shell to execute a programA new shell to execute a program Has its own environment local varsHas its own environment local vars Can’t change variable in parent shellCan’t change variable in parent shell Subshell variables destroyed on exitSubshell variables destroyed on exit

Page 22: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

PATHPATH

Directories search for programDirectories search for program Setup during logon procedureSetup during logon procedure Directories are separated by colon Directories are separated by colon

(:)(:) Message Message “not found” “not found” is returned is returned

upon unsuccessful searchupon unsuccessful search

Page 23: Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

.profile File.profile File provided by “Systems Administrator”provided by “Systems Administrator” Executed from the users home Executed from the users home

directorydirectory Can use to customize user Can use to customize user

environment environment Example:Example:> cat $HOME/.profile> cat $HOME/.profile

PATH=“/bin:/usr/bin::”PATH=“/bin:/usr/bin::”

export PATHexport PATH

>>