views:

285

answers:

1

I'm writing a script to search for a pattern in file. For example

scriptname pattern file1 file2 filenN

I use for loop to loop through arguments argv, and it does the job if all arguments are supplied. However if only one argument is supplied (in that case pattern ) it should ask to input file name or names, and then check for pattern. How I can set variable to include multiple words input from command line so I can use it in loop. Or even better is it possible to assign command line input to argv, so I don’t have to use the same loop twice only because it is different variable name (one to loop through argv if more than on argument, and second to loop through filenames variable if only one argument supplied). Below is the part of my script which is causing problem:

set pattern = $1 
if ($#argv == 1) then
    echo "Enter name of files"
    set filenames = $< #how I can set it to accept more than word?
endif
+1  A: 

You can use shift and $argv

#!/bin/csh
set pattern = $1
echo $pattern
shift
set remainder = "$argv"
echo $remainder

And running it:

% ./demo a b c
a
b c
Dennis Williamson
It answers the second part of my question, but if there is only one argument provided, I need to get additional input from keyboard. If I type during the execution of the script missing name of files (not provided as argument): file1 file2 file3I would like to set remainder to store file1 file2 file3. For example: echo"Enter name of files" set remainder= $< echo $remainder Output: file1 file2 file3
Mike55