Tải bản đầy đủ - 0 (trang)
Appendix C. Steps for Using Quoting Correctly

Appendix C. Steps for Using Quoting Correctly

Tải bản đầy đủ - 0trang

AppendixC.StepsforUsingQuoting

Correctly

C.1Backslash

C.2SingleQuotes

C.3DoubleQuotes

C.4CombiningQuotes

C.5SettingtheShellVariable



C.1Backslash

1. Precedesacharacterandescapesthatcharacter.

Sameasputtingsinglequotesaroundonecharacter.



C.2SingleQuotes

1. Mustbematched.

Protectallmetacharactersfrominterpretationexceptthefollowing:

a. Itself.

b. Exclamationpoint(cshonly).

c. Backslash.



Examples

CShell



BourneShell



echo'$><%^&*' echo'$*&!>
echo'Ineed

echo'Ineed



KornShell



echo'$*&!>
echo'Ineed



$5.00\!'

ho'Shecried,

"Help"'

echo'\\\\'

\\\\



\$5.00!'

echo'She

cried,"Help"'

echo'\\\\'

\\



\$5.00!'

echo'She

cried,"Help"'

print'\\\\'

\\



C.3DoubleQuotes

1. Mustbematched.

Protectallmetacharactersfrominterpretationexceptthefollowing:

a. Itself.

b. Exclamationpoint(cshonly).

c. $usedforvariablesubstitution.

d. ''Backquotesforcommandsubstitution.



Examples:

CShell



BourneShell



echo"Hello

$LOGNAME\!"

echo"Idon't

care"

echo"Thedate

is'date'"

echo"\\\\"

\\\\



echo"Hello

$LOGNAME!"

echo"Idon't

care"

echo"Thedate

is'date'"

echo"\\\\"

\



KornShell



print"Hello

$LOGNAME!"

print"Idon't

care"

print"Thedate

is$(date)"

print"\\\\"

\



C.4CombiningQuotes

Theendresultistobeabletoembedtheshellvariableintheawk

commandlineandhavetheshellexpandthevariablewithoutinterfering

withnawk'sfielddesignators,$1and$2.



C.5SettingtheShellVariable

name="JacobSavage"(BourneandKornShell)

setname="JacobSavage"(CShell)

(Thelinefromthedatafile)

JacobSavage:408-2987732:934LaBarbaraDr.,SanJose,CA:02/27/78:500000

(Thenawkcommandline)

nawk-F:'$1~/^'"$name"'/{print$2}'datafile

(Output)

408-298-7732



Step1:

TestyourknowledgeoftheUNIXcommandatthecommandlinebefore

plugginginanyshellvariables.

nawk-F:'$1~/^JacobSavage/{print$2}'filename

(Output)

408-298-7732



Step2:

Plugintheshellvariablewithoutchanginganythingelse.Leaveall



quotesastheywere.

nawk-F:'$1~/^$name/{print$2}'datafile

Startingattheleft-handsideoftheawkcommandleavethefirstquoteas

is;rightbeforetheshelldollarsignin$name,placeanothersinglequote.

Nowthefirstquoteismatchedandalltextwithinthesetwoquotesis

protectedfromshellinterference.Thevariableisexposed.Nowput

anothersinglequoterightaftertheein$name.Thisstartsanother

matchedsetofsinglequotesendingafterawk'sclosingcurlybrace.

Everythingwithinthissetofquotesisalsoprotectedfromshell

interpretation.



Step3:

Enclosetheshellvariableinasetofdoublequotes.Thisallowsthe

variabletobeexpandedbutthevalueofthevariablewillbetreatedas

singlestringifitcontainswhitespace.Thewhitespacemustbeprotected

sothatthecommandlineisparsedproperly.



Countthenumberofquotes.Thereshouldbeanevennumberofsingle

quotesandanevennumberofdoublequotes.



Example:



oldname="EllieMain"

newname="EleanorQuigley"



1. Makesurethecommandworks.



nawk-F:'/^EllieMain/{$1="EleanorQuigley";print$0}'datafile



Pluginthevariables.

nawk-F:'/^$oldname/{$1="$newname";print$0}'datafile



Playthequotinggame.Startingatthefirstsinglequoteattheleft,

moveacrossthelineuntilyoucometothevariable$oldnameandplace

anothersinglequotejustbeforethedollarsign.Putanothersinglequote

rightafterthelastletterinthevariablename.

Nowmovetotherightandplaceanothersinglequoterightbeforethe

dollarsignin$newname.Putanothersinglequoteafterthelastcharacter

in$newname.



Countthenumberofsinglequotes.Ifthenumberofsinglequotesisan

evennumber,eachquotehasamatchingquote.Ifnot,youhave

forgottenastep.

Encloseeachoftheshellvariablesindoublequotes.Thedouble

quotesareplacedsnuglyaroundtheshellvariable.





CONTENTS



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix C. Steps for Using Quoting Correctly

Tải bản đầy đủ ngay(0 tr)

×