1 / 20

Agenda

Agenda. Tools for Shell Scripting – Part I Quoting Special Characters Backslash , single quotes ‘’ , double quotes “” Variables (user-created variables, Keyword Shell variables, Read-only Shell variables (a.k.a. positional parameters). Special Characters.

kluck
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda Tools for Shell Scripting – Part I • Quoting Special Characters • Backslash \ , single quotes ‘’, double quotes “” • Variables • (user-created variables, Keyword Shell variables, Read-only Shell variables (a.k.a. positional parameters)

  2. Special Characters • A special character is used to give special meaning to a file or string when used with a UNIX command or when executing shell scripts. • For example: • redirection (>, >>, < , 2> , 2>> , <) • ambiguous file reference ( * , ? , [ ] ) • etc…

  3. Special Characters • Sometimes, conflicts arise when you use a Unix command to perform something with a special character as an argument, and you get a different result that you anticipate. • For Example: • echo *(you might think that this will display an asterisk * but a listing of all files will be displayed – not what you might have expected!) • What happened? • The shell thinks “*” means to match all or nothing and the this command will display all files.

  4. Quoting text • To make the shell “ignore” or “turn-off” the meaning of the special character when using you use quotes. • Types of Quotes: • Single Quotation marks: echo ‘*’ • Double Quotation marks: echo “*” • Backslash: echo \* • Note: A backslash must appear for every occurrence of a special symbol – Your instructor will provide examples in class the advantages and limitations associated with each of these quotation methods.

  5. Variables • So far, we discussed that shell scripts are simply a file consisting of Unix OS commands to be run automatically. • Shell scripts can become more powerful if the user could input data (either in the form of an argument or input from the keyboard). • Several variables are available to provide Shell scripts this power.

  6. User-Created Variables • A variable that the user creates within the shell script. • A user-created variable must begin with a letter. • You must not include white-space on either side of the equals sign • For example: • cat >test.file • name=Murray • <CTRL><D>

  7. User-Created Variables • In order to access or display the value of the variable, a dollar sign $ must appear before the variable name - otherwise, it would be interpreted as text. • For Example: • cat > test2.file • name=Murray • echo $name • <CTRL><D>

  8. User-Created Variables • You can use single or double quotes to assign an entire string of text • For Example: • name=‘Murray Saul’ • name=“Murray Saul” • Note 1: Single or double quotes need to “ignore” or “turn-off” shells interpretation that space or tab between words are separate arguments!! • Note 2: Single quotes (‘ ’) and backslash (\) will turn-off meaning of special characters including the variable name eg. echo ‘$name’ (Double quotes will allow variable to display value!!)

  9. User-Created Variables • To remove a previously stored variable, assign a “null” value to the variable or use unset command • Example: • name= • unset name

  10. User-Created Variables • The read command is used to pause and prompt the user to enter a line of text. • Example: • echo –n “Enter your full name” • read fullname • echo $fullname echo –n means to prompt input to read on same line as displayed text.(this works in Linux OS)

  11. Shell Variables • There are two categories of shell variables: • Keyword Variables • Variables that have been assigned an easy to remember word that are commonly used • Examples include PATH, USER, PWD, HOME, etc… • Read-only Shell Variables(Positional Parameters) • Variables that are assigned by a command (set) as opposed to assigning by using the equal sign (=) • Examples include $1, $2, $3, … $9

  12. Keyword Shell Variables • Keyword Shell variables are used by the shell and many of these variable have values already assigned to them. • Keyword Shell variables are usually identified as UPPER-CASE letters. • The user can see the values assigned to these Keyword Shell variables by issuing the set command without an argument. • Some of these Keyword Shell variables can be changed by the user, some are assigned by the system and cannot be changed.

  13. Keyword Shell Variables • Keyword shell variables can be used in the shell script with Unix commands to “customize” the script for the particular user. • Examples: • echo “Hi there, $USER” Displays current user’s name. • echo $PWD Displays user’s current directory. • mkdir $HOME/dir1 Creates a directory called dir1 that “branches-off” the user’s home directory.

  14. Read-Only Shell Variables(Positional Parameters) • Read-Only shell variables have the feature to a script’s arguments (when script is issued like a command) into variables to be used within the script when it is running! • You can use the set command to assign values to these read-only shell variables inside the script as well (added feature!). • You can also store standard output from commands issued while script is running to into variables (this is called “command substitution” which is covered later).

  15. Read-Only Shell Variables • Read only variables range from $0 to $9 • $0 is script name (can also represent shell name if set command is issued as command from shell prompt (ie. not running a script!) • $1 to $9 are first nine positional parameters, or command line arguments • Read-Only Shell Variables need to be set • For Example (type from shell prompt) • set name1 name2 name3 echo $0 = setecho $1 = name1echo $2 = name2echo $3 = name3

  16. Read-Only Shell Variables • Try to determine output from the following commands: • set one two three • echo $0 $1 $2 • set “2 1” • echo $1 $2 • set name1 name2 name3 • echo $1 $2

  17. Read-Only Shell Variables • Read-Only Shell Variables are also referred to as “Positional Parameters”. • The term “Positional” in Positional Parameters means that the arguments are stored in a particular position(i.e. $1 - $9)

  18. Positional Parameters • $0 is name of command used to call script $1 to $9 are first nine positional parameters • $* represents all positional parameters as one long string • "$*" will put single pair of double quotes around string • $@ same as $*, except "$@" will put double quotes around each parameter • $# contains number of parameters

  19. Positional Parameters • can also access beyond $9 by using set braces, eg. ${10} will access 10th argument set, etc... • ${var} can be used instead of $var - useful if followed by alpha-numerics • $? is the exit status of the last command, 0 means successful script can be ended with exit n where n is the exit status or condition code of the script

  20. Positional Parameters • shift shifts parameters left, so that the second one which was $2 becomes the first which is $1, etc., allowing parameters beyond the first nine to be accessed • Arguments "falling off" the left side are no longer available, so should be saved in other variables if they are required later in the script • shift nn, where nn is a number, has the same effect as that number of shiftcommands

More Related