Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,"— Presentation transcript:

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

2 Variables variable format: variable format: variable = value (there should not be any spaces around the =) Example: Example:$number=7 $ echo The value of number is $number: The value of number is 7 $

3 Using Quotes Four types of quote characters Four types of quote characters ‘single quote character “double quote character \backslash character `back quote character Example (single quote): Example (single quote): $ cat city AustinDallas Ft. Worth San Antonio $ grep ‘San Antonio’ city San Antonio $

4 Using Quotes Example (double quote): Example (double quote): $ex1= “‘Dallas,’ A city in Texas” $echo $ex1 ‘Dallas,’ A city in Texas $ex2=‘ “San Antonio,” is also a city in Texas’ $echo $ex2 “San Antonio,” is also a city in Texas $

5 Using Quotes - (cont) Example (backslash): Example (backslash): $echo $ex2 “San Antonio,” is also a city in Texas $ $ echo \$ex2 $ex2$ which is the same as: $echo ‘$’ ex2 $ex2$

6 Using Quotes Example (back quote) Example (back quote) $ echo The date and time is: `date` The date and time is: Tue Oct 10 10:18:34 EDT 2006 $

7 Making Decisions - If if format: if format: if expression then commands elif commands else commands fi

8 Making Decisions - Test (cont) Relational operators Relational operators -eq -eq -ne -ne -lt -lt -gt -gt -le -le -ge -ge

9 Making Decisions - Test (cont) Test file operators returns true if: Test file operators returns true if: -d filefile is a directory -f filefile is an ordinary file, and exists -r filefile is readable by the process -s filefile isn’t empty -w file file is writable by the process -x filefile is executable -G filefile is owned by the group I belong to -O filefile is owned by user -u fileset_user_id bit is set -g fileset_group_id bit is set -

10 Making Decisions Test Examples Examples [ -f /usr/train1/file1 ] if file1 exists and is ordinary file [ -r /usr/train2/file1 ] if file1 exists and is readable by this process [ -s /usr/train3/file1 ] if file1 exists and is not empty

11 Making Decisions Test Test logical operators Test logical operators ! expressionlogical negation expression -a expressionlogical and expression -o expressionlogical or Examples: Examples: [ ! -f /usr/train1/file1 ] [ -f /usr/train1/file1 -a -r /usr/train1/file1 ] [ -n “$var1” -o -r /usr/train2/file1 ]

12 Looping Constructs For For format: For format: for variable in item1 item2..item3 docommand(s)done

13 User Environment During logon a distinct environment is created separate from other users During logon a distinct environment is created separate from other users

14 Local Variables Value cannot be changed by other subshells Value cannot be changed by other subshells Will not be passed to other subshells Will not be passed to other subshells

15 Local Variables - (cont) Example: Example: > cat test1 echo %$var1% $test1 % % % % var1 is equal to null > cat test2 var2=25 echo %$var2% >>test2 %25 % var2 is equal to 25

16 Exported Variables Export command format: Export command format: export vars Allows a value of a variable to be passed to other subshells Allows a value of a variable to be passed to other subshells Changing an exported variable in a subshell does not change the value in the parent shell Changing an exported variable in a subshell does not change the value in the parent shell

17 Exported Variables Example: Example: > cat test3 var3 = 2345 echo var3 = $var3 > test 3 var3 = 2345 $cat test4 echo var3 = $var3 > test4 var3 = var3 is a local variable so its value in test3 is 2345. In test4 it is null.

18 Exported Variables (cont) Another Example: Another Example: $ var3=2345 $ export var3 $cat test3 echo var3 = $var3 $test3 var3 = 2345 $test4 $ var3 is an exported variable so its value in test3 is 2345. In test4 it is also 2345

19 Exported Variables - (cont) Once an exported variable always an exported variable. (Unless you use unset to destroy it.) Once an exported variable always an exported variable. (Unless you use unset to destroy it.) Each subshell makes its own copy of the variable Each subshell makes its own copy of the variable Export with no arguments lists the variables that are exported to users shell Export with no arguments lists the variables that are exported to users shell

20 env Command env [option] [var = val] [command] env [option] [var = val] [command] displays current environment or modifies variables specified. displays current environment or modifies variables specified. Commands specified are executed in the new environment Commands specified are executed in the new environment

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

22 PATH Directories search for program Directories search for program Setup during logon procedure Setup during logon procedure Directories are separated by colon (:) Directories are separated by colon (:) Message “not found” is returned upon unsuccessful search Message “not found” is returned upon unsuccessful search

23 .profile File provided by “Systems Administrator” provided by “Systems Administrator” Executed from the users home directory Executed from the users home directory Can use to customize user environment Can use to customize user environment Example: Example: > cat $HOME/.profile PATH=“/bin:/usr/bin::” export PATH >


Download ppt "Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,"

Similar presentations


Ads by Google