Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For loop questions

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
Hi all,

I have a series of files I want to put into a for loop for subsequent action. The files are all named in the same way:

(somenumber)_spgr+acpc.img
10202_spgr+acpc.img
3287_spgr+acpc.img
h6785_spgr+acpc.img

Here is the loop I've been trying to use, but it doesn't seem to work. Any help would be appreciated.

#################################################
#!/bin/csh -ef

foreach SUBJ ( `ls *_spgr.img | sed 's/.img//g' `)
set SUBJ = ${1}_spgr

echo $SUBJ
#################################################

~Czar


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Sorry, I mistyped. The for loop is such:

#################################################
#!/bin/csh -ef

foreach SUBJ ( `ls *_spgr+acpc.img | sed 's/.img//g' `)
set SUBJ = ${1}_spgr+acpc

echo $SUBJ
#################################################


I've also tried some variations on the "spgr+acpc" line

thanks, CzarJ

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
First of all, consider not programming in C-Shell, for why read this:
Second, (and I haven't programmed in csh in about 15 years) the + may have significance to the shell and is messing it up.

Third, you haven't closed your loop.

Fourth, and actual error message would be *very* helpful.

Finally, I don't even think the loop is doing what you think it's doing. The loop would set $SUBJ, then you're immediately setting it again with a "set", plus $1 is the first argument of the command line.

In a *good* shell like ksh or bash you're write:

Code:
for SUBJ in $( ls *_spgr+acpc.img | sed 's/.img//g' )
do
  echo $SUBJ

done
 
What are you trying to do with $1?

$1 refers to the First Command argument to your SCRIPT. It has nothing to do with the for loop.

The For Loop Variable of you your SCRIPT is $SUBJ already.

In Addition, CSH can remove the EXTENSION from the contents of the variable directly so there is no need for the SED.

${var:h} Directory (DIRNAME )
${var:t} Filename (BASENAME)
${var:e} extension
${var:r} root ( everything minus the extension )

I like using the braces because that avoids confusions with other concatenations you might have.

In Addition CSH will expand the * on the command line to file names automatically so you don't need the ls.

Therefore what you have above could be coded as


Code:
#!/bin/csh -ef

foreach SUBJ (*_spgr+acpc.img)
   echo ${SUBJ:r}
end

Again I didn't know what you were trying to do with the $1 argument so I left it out. If you could explain what you are using the $1 for maybe we could give a better example.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top