Question

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
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top