Top Banner
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz
33

Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Jan 18, 2016

Download

Documents

Allan Evans
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Introduction to Unix (CA263)

Quotes

By

Tariq Ibn Aziz

Page 2: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Objectives

• In this lecture you will learn about one of the most unique feature of the shell programming:– The single quote character ‘

– The double quote character “

– The backslash character \

– The back quote character `

Page 3: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Single Quote

• This must occur in pair.

• There are several reasons to use single quote in the shell

Page 4: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Character Separated by Space

• To keep characters together separated by space

$ cat phonebookAlice Chebba 596-2015Bob Swingle 598-9257Liz Stachiw 775-2298Susan Goldberg 338-7776Susan Topple 243-4932Tony Iannino 386-1295$

Page 5: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example

$ grep Susan phonebookSusan Goldberg 338-7776Susan Topple 243-4932$$ grep Susan Goldberg phonebookgrep: can’t open Goldberg$$ grep 'Susan Goldberg' phonebookSusan Goldberg 338-7776$

Page 6: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Shell preserve spaces between quotes

• No matter how many space characters are enclosed between quotes, they are preserved by the shell.

• Shell removes the extra space characters from line and passes echo four argument one two three four

$ echo one two three fourone two three four$• space characters are preserved in the following example

$ echo 'one two three four'one two three four$

Page 7: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Special characters inside the single quotes

• All special inside the single quotes are ignored by the shell if they appear inside single quote

$ file=/usr/steve/bin/prog1$ echo $file/usr/steve/bin/prog1$ echo '$file'$file

Page 8: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Special characters inside the single quotes

$ echo *address nu names phonebook stats$ echo '*'*$ echo '< > | ; ( ) { } >> " `&'< > | ; ( ) { } >> " `&$ echo 'How are you today,> John'How are you today,John$

Page 9: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Special characters inside the single quotes

$ message='I must say, this sure is fun'

$ echo $messageI must say, this sure is fun$ text='* means all files'$ echo $textaddress nu names phonebook stats means all files

• * was replaced by the names of all file

Page 10: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Double Quotes

• Double quotes work similarly to single quotes, except they are not quite restrictive.– Single quote tells the shell to ignore all enclosed

characters.– Double quote says to ignore most. In particular three

characters are not ignored inside double quotes:• Dollar signs• Back quotes• Backslash

Page 11: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example Double Quote[1]

$ x=*$ echo $*address nu names phonebook stats $ echo '$x'$x$ echo "$x"*$

Page 12: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example Double Quote[2]

$ address="11 Driscoll Dr> Brampton, ON, L6Y 3J2"$ echo $address11 Driscoll Dr Brampton, ON, L6Y 3J2$$ echo "$address"11 Driscoll DrBrampton, ON, L6Y 3J2• The new line character is depicted by the

characters \n

Page 13: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example Double Quote[3]

$ x="‘Hello,’ he said"$ echo $x‘Hello,’ he said$$ article=‘" My Name is," Tariq’$ echo $article" My Name is," Tariq

Page 14: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Backslash

• Basically, the backslash is equivalent to placing single quotes around a single character, with a few minor exceptions.

• The general format is:– \c– Where c is the character you want to quote.

Page 15: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example[1]

$ echo >Syntax error: ‘newline or ;’ unexected

• Shell saw > and thought you wanted to redirect echo’s output to a file.

$ echo \>>$

Page 16: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Example[2]

$ x=*$ echo \$x$x• In this case backslash ignore the $ that followed

the backslash and as a result variable substitution was not performed.

$ echo \\\$ echo '\'\

Page 17: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Using Backslash for Continuing Lines

• Single quote tells shell to ignore newline$ lines=one’> 'two$ echo "$lines"onetwo• Try this with \ instead. $ lines=one\> two$ echo "$lines"onetwo• The shell treat \ as a line continuation.

Page 18: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Backslash inside Double Quotes

• If backslash precedes any character inside double quotes, then backslash is ignored by the shell and passed on to the program:

$ echo "\$x"$x$ echo "\ is the backslash character"\ is the backslash character$ x=5$ echo "The value of x is \"$x\""The value of x is "5"

Page 19: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Lab Exercise

• Display the following line:

<<< echo $x >>> display the value $x, which is $x

$ x=5<<< echo $x >>> display the value $x, which is 5

Page 20: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

• The back quote tells the shell to execute the enclosed command.

$ echo The date and time is: `date`The date and time is: Tue Jul 16 09:14:22 EDT 1985

$ echo current dir is: `pwd`current dir is: /usr/steve/bin

Page 21: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

• The back quote tells the shell to execute the enclosed command.

$ cat nuecho There are `who|wc –l` users logged on

$ nuThere are 13 users logged on

Page 22: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

• The Single quotes protect everything.$ echo ‘`who|wc –l` tell how many users’`who|wc –l` tell how many users

• The Double quotes interpret back quotes.$ echo "You have `ls|wc –l` files"You have 7 files

Page 23: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

• Execute date and store the output in now

$ now=`date`

$ echo $nowTue Jul 16 09:14:22 EDT 1985$

Page 24: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

$ filelist=`ls`$ echo $filelistaddress nu names phonebook stats$ echo "$filelist"address nu names phonebook stats

Page 25: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

The Back Quote

• To store a content of a file into the variable, you can use cat:

$ namelist=`cat names`$ echo "$names"address nu names phonebook stats

Page 26: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Change the Variable Value[1]

• The back quote mechanism can be used to change the value of a variable:

$ name="Tariq Aziz"$ name=`echo $names | tr '[a-z]' '[A-Z]'`$ echo $nameTARIQ AZIZ

Page 27: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Change the Variable Value[2]

• The back quote mechanism can be used to change the value of a variable:

$ filename=/usr/steve/memos$ firstchar=`echo $filenames | cut –c1`$ echo $firstchar/

Page 28: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Change the Variable Value[3]

• The back quote mechanism can be used to change the value of a variable:

$ file=exec.o$ lastchar=`echo $filenames|sed 's/.*\(.\)$/\1/'`

$ echo $lastcharo

Page 29: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Arithmetic on Shell Variable

• The shell has no concept of performing arithmetic on values stored inside variables.

$ i=1$ i=$i+1$ echo $i1+1$

Page 30: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Arithmetic on Shell Variable

• The UNIX program called expr evaluate an expression given to it on the command line.

$ expr 1 + 23$ expr 1+21+2$

• Each operator and operand must be separated by space.

Page 31: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Arithmetic on Shell Variable

• The usual arithmetic operators are recognized by expr: + for addition, - for subtraction, / for division, * for multiplication, and % for modulus.

$ expr 10 + 20 / 220• Each operator and operand must be separated

by space.$ expr 1+21+2$

Page 32: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Arithmetic on Shell Variable

$ expr 17 * 6expr: syntax error• What happened here?

• The shell saw * and substituted the names of all files in your directory.

$ expr "17 * 6"17 * 6

Page 33: Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.

Arithmetic on Shell Variable

$ expr 17 \* 6102• Example

$ i=1$ expr $i + 12

• Example

$ i=1$ i=`expr $i + 1`$ echo $i2