Originally posted by: Wouter Liefting
If it works on other platforms, what versions of the csh (both in AIX and on the other platforms) are you using?
Anyway, I'm not an expert on the csh but from what I can see it looks like you are trying to achieve the following: Your script is passed a number of parameters which each represent a filename OR a wildcard expression of some sort. You're expanding the wildcards within the csh, and for each of the filenames you're doing a grep to find something. If that something is found, you mess about with a few $status variables to record that.
If I ignore the grep & status part (which seems to work fine) and just write a shell script like this:
#!/bin/csh
foreach i ($argv:q)
echo $i
echo "XXX"
end
and I call this script with ./test.csh "*" (thus forcing the csh to expand the wildcard, instead of my current shell - bash), the results are NOT what you are trying to achieve. It seems the :q forces the ($argv:q) argument to be one argument, where all filenames are space-separated in a single string. So the foreach loop only iterates once.
On the other hand, if I simply use ($argv), a proper list results, and the foreach loop iterates over each list element once. It even works properly in the presence of filenames and directory names that contain spaces.
This is on Linux (CentOS 6.4, with the tcsh version 6.17.00 - I don't have an AIX system to hand right now) but it's consistent with what the csh manual page tells me. So I doubt you're on the right track with your :q solution. To make sure, I'd put in a few echo lines like above, to see what the $i variable is really doing.
I also see something else wrong with your script. You're currently testing whether $#argv >= 2. Which means that if only one argument is passed, the foreach will not execute - not even when that single argument is "*", which may expand to 1000s of filenames. Unless the first argument is, or can be, a flag of some sort, I'd say you need to test whether $#argv >= 1. And if the first argument is a flag which is handled earlier on in your script, you'd need to do something (a "shift" perhaps) to get rid of that first argument properly before calling the loop. (The script may work fine now since you're testing whether the argument exists as a file later on, but what would happen if you accidently created a file with the same name as your flag?)
Another thing is that you're using -e, presumably to test if the file exists. It might be better to use -r or even -f and -r. The single fact that the file exists may not mean that the grep is going to be successful. It may be a directory or special file, or you may not have read permissions.
Hope this helps.
#AIX-Forum