Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Post on 18-Jan-2016
223 Views
Preview:
Transcript
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
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
$$
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
$$
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
$$
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
$$
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
$$
Making Decisions - IfMaking Decisions - If if format:if format:
if expressionif expression
then commandsthen commands
elif commandselif commands
else commandselse commands
fifi
Making Decisions - Test Making Decisions - Test (cont)(cont)
Relational operatorsRelational operators -eq-eq -ne-ne -lt-lt -gt-gt -le-le -ge-ge
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
-
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
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 ]
Looping ConstructsLooping ConstructsForFor
For format:For format:for variable in for variable in
item1 item2..item3item1 item2..item3
dodo
command(s)command(s)
donedone
User EnvironmentUser Environment
During logon a distinct environment During logon a distinct environment is created separate from other usersis created separate from other users
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
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
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
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.
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
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
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
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
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
.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
>>
top related