1 / 33

Introduction to Unix (CA263) Quotes

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 `.

Download Presentation

Introduction to Unix (CA263) Quotes

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. Introduction to Unix (CA263)Quotes By Tariq Ibn Aziz

  2. 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 `

  3. The Single Quote • This must occur in pair. • There are several reasons to use single quote in the shell

  4. Character Separated by Space • To keep characters together separated by space $ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Susan Goldberg 338-7776 Susan Topple 243-4932 Tony Iannino 386-1295 $

  5. Example $ grep Susan phonebook Susan Goldberg 338-7776 Susan Topple 243-4932 $ $ grep Susan Goldberg phonebook grep: can’t open Goldberg $ $ grep 'Susan Goldberg' phonebook Susan Goldberg 338-7776 $

  6. 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 four one two three four $ • space characters are preserved in the following example $ echo 'one two three four' one two three four $

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

  8. 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 $

  9. Special characters inside the single quotes $ message='I must say, this sure is fun' $ echo $message I must say, this sure is fun $ text='* means all files' $ echo $text address nu names phonebook stats means all files • * was replaced by the names of all file

  10. 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

  11. Example Double Quote[1] $ x=* $ echo $* address nu names phonebook stats $ echo '$x' $x $ echo "$x" * $

  12. Example Double Quote[2] $ address="11 Driscoll Dr > Brampton, ON, L6Y 3J2" $ echo $address 11 Driscoll Dr Brampton, ON, L6Y 3J2 $ $ echo "$address" 11 Driscoll Dr Brampton, ON, L6Y 3J2 • The new line character is depicted by the characters \n

  13. Example Double Quote[3] $ x="‘Hello,’ he said" $ echo $x ‘Hello,’ he said $ $ article=‘" My Name is," Tariq’ $ echo $article " My Name is," Tariq

  14. 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.

  15. Example[1] $ echo > Syntax error: ‘newline or ;’ unexected • Shell saw > and thought you wanted to redirect echo’s output to a file. $ echo \> > $

  16. 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 '\' \

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

  18. 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"

  19. 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

  20. 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

  21. The Back Quote • The back quote tells the shell to execute the enclosed command. $ cat nu echo There are `who|wc –l` users logged on $ nu There are 13 users logged on

  22. 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

  23. The Back Quote • Execute date and store the output in now $ now=`date` $ echo $now Tue Jul 16 09:14:22 EDT 1985 $

  24. The Back Quote $ filelist=`ls` $ echo $filelist address nu names phonebook stats $ echo "$filelist" address nu names phonebook stats

  25. 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

  26. 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 $name TARIQ AZIZ

  27. 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 /

  28. 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 $lastchar o

  29. Arithmetic on Shell Variable • The shell has no concept of performing arithmetic on values stored inside variables. $ i=1 $ i=$i+1 $ echo $i 1+1 $

  30. Arithmetic on Shell Variable • The UNIX program called expr evaluate an expression given to it on the command line. $ expr 1 + 2 3 $ expr 1+2 1+2 $ • Each operator and operand must be separated by space.

  31. 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 / 2 20 • Each operator and operand must be separated by space. $ expr 1+2 1+2 $

  32. Arithmetic on Shell Variable $ expr 17 * 6 expr: syntax error • What happened here? • The shell saw * and substituted the names of all files in your directory. $ expr "17 * 6" 17 * 6

  33. $ expr 17 \* 6 102 Example $ i=1 $ expr $i + 1 2 Example $ i=1 $ i=`expr $i + 1` $ echo $i 2 Arithmetic on Shell Variable

More Related