An example from (the excellent) Unix Programming Environment
considers an address book:
John Perkins 616-555-4444
Bill Jotto 773-222-1112
Dial-a-Clown 738-224-5823
Prince Alex 837-999-999
Pizza Hut 833-339-222
Pizza Puk 882-922-222
Pizza Buk 822-221-111
now I am writing a program that searches this address book called
‘411‘
grep $* /file/location/411
now running 411 will yield
$> 411 John
>John Perkins 616-555-4444
now say I want to call John and invite him for some pizza
(So I am searching for John number and Pizza numbers).
$>411 John Pizza
grep: can't open pizza
No match!
So how to I tell the shell to accept multiple argument with arbitrary spaces?
When you call
grepwith more than one argument, it assumes the first is the pattern and all others are the files to be searched. You’ll need to make two modifications:When you call your program, you’ll need to enclose multi-word arguments in double-quote characters. This is standard shell behavior.
Your program will need to read the arguments from the command line and either send them individually to
grepor build a compound expression (arg1|arg2|arg3) and pass it togrepwith the-E(extended regex) flag.For example:
Or: