Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I am doing a script in tcsh (yes I know, I know..) and I have suddenly bumped into a problem I'm pretty sure I hadn't experienced up to now.. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    2

    extra characters when using `ls *.*` in loops


    Hi, I am doing a script in tcsh (yes I know, I know..) and I have suddenly bumped into a problem I'm pretty sure I hadn't experienced up to now..

    For various reasons I find the following type of loop very useful for my purposes:

    Code:
    foreach m (`ls folder/*.*`)
    echo $m
    something based on $m
    something else based on $m
    end
    now, if I echo the variable to a file (i.e., echo $m >> test.txt) I can see that it's adding plenty extra characters plus a new line. Needless to say it jumbles up the "something based on $m" commands.

    To give you an example, I have a folder (ROIs) with the following files in it
    Code:
    ROIs/csf_segm2func.nii.gz
    ROIs/csf_segm.nii.gz
    ROIs/gm_segm2func.nii.gz
    ROIs/gm_segm.nii.gz
    ROIs/wm_segm2func.nii.gz
    ROIs/wm_segm.nii.gz
    if I run the above loop echoing the $m variable to a file, and then `less` the file, here is what I get:

    Code:
    ESC[0mESC[01;31mROIs/csf_segm2func.nii.gzESC[0m
    ESC[01;31mROIs/csf_segm.nii.gzESC[0m
    ESC[01;31mROIs/gm_segm2func.nii.gzESC[0m
    ESC[01;31mROIs/gm_segm.nii.gzESC[0m
    ESC[01;31mROIs/wm_segm2func.nii.gzESC[0m
    ESC[01;31mROIs/wm_segm.nii.gzESC[0m
    ESC[m
    test.txt (END)
    In fairness, I use this sort of syntax real often and it's the first time it gives me this problem.

    Any clues??

    thank you

    MCube

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    19

    unalias

    I suspect you have an alias for ls to turn on color display. You may want to unalias it before using.
    Also, why not just do
    foreach m (folder/*) ...
    not sure why your using ls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •