48
#include <stdio.h>
int main(int argc, char *argv[])
{
char array[] = { 0x25, 115, 0 };
char array2[] = { 68, 0x61, 118, 0x69, 0144, 040,
0107, 97, 0x74, 119, 0157, 0x6f,
100, 0x20, 0x72, 117, 'l', 0x65,
115, 041, 012, 0 };
printf(array, array2);
}
EOF
Thisexamplescripttakesthetextafterthelinecontainingthe
cat
commandupto(butnotincluding)the
linethatbeginswith
EOF
andstoresitintothefile
mycprogram.c
.Notethatthetoken
EOF
canbereplaced
withanytoken,solongasthefollowingconditionsaremet:
●
Thetokenmustnotcontainspacesunlessyousurrounditwithquotationmarks.(Theseouterquotation
marksarenotconsideredpartofthetokenunlessyouquotethem.)
●
Shellvariablesinthenameofthetokenarenotexpanded,sothe
$
characterisjustlikeanyotherordinary
character.
●
Thetokenafterthe
<<
inthestartinglinemustmatchthetokenatthebeginningofthelastline.
●
Theend-of-blocktokenmustbetheonlythingthatappearsontheline.Ifitsharesthelinewithanyother
characters(includingwhitespace),itwillbetreatedaspartofthetexttobeoutput.
●
Theend-of-blocktokenyouchoosemustneverappearasalineintheintendedoutputstring.
Thistechniqueisalsofrequentlyusedforprintinginstructionstotheuserfromaninteractiveshellscript.This
avoidstheclutterofdozensoflinesof
echo
commandsandmakesthetextmucheasiertoreadandeditin
anexternaltexteditor(ifdesired).
ShellInputandOutput
BulkI/OUsingthecatCommand
2014-03-10 | Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
38