Agenda Tools for Shell Scripting Part I n

  • Slides: 20
Download presentation
Agenda Tools for Shell Scripting – Part I n Quoting Special Characters n n

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

Special Characters n n A special character is used to give special meaning to

Special Characters n n 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: n n n redirection (>, >>, < , 2>> , <) ambiguous file reference ( * , ? , [ ] ) etc…

Special Characters n n Sometimes, conflicts arise when you use a Unix command to

Special Characters n n 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: n n 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? n The shell thinks “*” means to match all or nothing and the this command will display all files.

Quoting text To make the shell “ignore” or “turn-off” the meaning of the special

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: Double Quotation marks: Backslash: echo ‘*’ echo “*” 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.

Variables n n n So far, we discussed that shell scripts are simply a

Variables n n n 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.

User-Created Variables n n A variable that the user creates within the shell script.

User-Created Variables n n 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 n For example: cat >test. file name=Murray <CTRL><D>

User-Created Variables In order to access or display the value of the variable, a

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. n For Example: cat > test 2. file name=Murray echo $name <CTRL><D>

User-Created Variables 1. You can use single or double quotes to assign an entire

User-Created Variables 1. You can use single or double quotes to assign an entire string of text n For Example: name=‘Murray Saul’ name=“Murray Saul” Note 1: Single or double quotes need to “ignore” or “turnoff” 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!!)

User-Created Variables To remove a previously stored variable, assign a “null” value to the

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

User-Created Variables The read command is used to pause and prompt the user to

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 –n means to prompt input to read on same line echo $fullname as displayed text. (this works in Linux OS)

Shell Variables There are two categories of shell variables: n Keyword Variables n n

Shell Variables There are two categories of shell variables: n Keyword Variables n n n 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) n n Variables that are assigned by a command (set) as opposed to assigning by using the equal sign (=) Examples include $1, $2, $3, … $9

Keyword Shell Variables n n Keyword Shell variables are used by the shell and

Keyword Shell Variables n n 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.

Keyword Shell Variables n n Keyword shell variables can be used in the shell

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

Read-Only Shell Variables (Positional Parameters) n n n Read-Only shell variables have the feature

Read-Only Shell Variables (Positional Parameters) n n n 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).

Read-Only Shell Variables n n n Read only variables range from $0 to $9

Read-Only Shell Variables n n n 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) n set name 1 name 2 name 3 echo $0 = set echo $1 = name 1 echo $2 = name 2 echo $3 = name 3

Read-Only Shell Variables n Try to determine output from the following commands: set one

Read-Only Shell Variables n Try to determine output from the following commands: set one two three echo $0 $1 $2 set “ 2 1” echo $1 $2 set name 1 name 2 name 3 echo $1 $2

Read-Only Shell Variables n n Read-Only Shell Variables are also referred to as “Positional

Read-Only Shell Variables n n 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)

Positional Parameters n n n $0 is name of command used to call script

Positional Parameters n n n $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

Positional Parameters n n n can also access beyond $9 by using set braces,

Positional Parameters n n n can also access beyond $9 by using set braces, eg. ${10} will access 10 th 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

Positional Parameters n n n shifts parameters left, so that the second one which

Positional Parameters n n n 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 shift commands