0

trying to look up files in directories with wildcard *, and put the names into an array

the files have similar names (MATCHr1, MATCHr2 ... )

the problem arises when the file does not exist (which is a possibility)

set command returns a "no match" error and terminates the loop

if this happens how can i get it handle the error by jumping to the next iteration?

set SUBIDS = (10003 10005 10006)

foreach SUBID ($SUBIDS)

foreach SEQR ( MATCH ENC NBACK SIMON FACE )

ls -l  *${SEQR}*.nii.gz

set Array = *${SEQR}*.nii.gz

echo $Array[*]

....rest of code works to use contents of Array to create text files and works great when the *${SEQR}*.nii.gz returns a file name

but fails when the no matches are made with the wildcard

Any help would be well apreciated

thanks!

gijoeyjoe
  • 3
  • 4
  • try adding a check on array size after you define it (`if #$Array >0`) – user2141046 Feb 16 '17 at 12:35
  • where ? `ls -l *${SEQR}*.nii.gz` returns: no match but allows code to continue onto `set Array = *${SEQR}*.nii.gz` which also returns no match but the script exits and does not continue to next `SEQR` – gijoeyjoe Feb 16 '17 at 20:14
  • you can do you ls and check the $status (or $STATUS, depends on your system) flag: `ls -l *${SEQR}*.nii.gz >> /dev/null ; if !$status then ...` i'm not next to my machine now to check the exact syntax, but that's the spirit of it... – user2141046 Feb 18 '17 at 20:44
  • that did it! `ls -l *${SEQR}*.nii.gz >> /dev/null` `# compensate for no match error` `set checkme = $status` `# if return good` `if ( $checkme == 0 ) then ....` THANKS! – gijoeyjoe Feb 22 '17 at 03:43
  • I'm turning my answer to an official answer so you'll be able to close the question. – user2141046 Feb 22 '17 at 06:54

1 Answers1

0

you can do you ls and check the $status (or $STATUS, depends on your system) flag:
ls -l *${SEQR}*.nii.gz >> /dev/null ; if !$status then ...

user2141046
  • 862
  • 2
  • 7
  • 21