3

I quickly searched for this before posting, but could not find any similar posts. Let me know if they exist.


The commands being executed seem very simple. A directory listing is used as the input for a function.

The directory contains a bunch of files named "epi1_mcf_0###.nii.gz"

Command-line version (bash is running when this is executed):

fslmerge -t output_file `ls epi1_mcf_0*.nii.gz`

Shell script version:

#!/bin/bash
fslmerge -t output_file `ls epi1_mcf_0*.nii.gz`

The command-line version fails, but the shell script one works perfectly.

The error message is specific to the function, but it's included anyway.

** ERROR (nifti_image_read): failed to find header file for 'epi1_mcf_0000.nii.gz'
** ERROR: nifti_image_open(epi1_mcf_0000.nii.gz): bad header info
Error: failed to open file epi1_mcf_0000.nii.gz
Cannot open volume epi1_mcf_0000.nii.gz for reading!

I have been very frustrated with this problem (less so after I figured out that there was a way to get the command to work).

Any help would be appreciated.

(Or is the general consensus that the problem should be looked for in the "fslmerge" function?)

6
  • 2
    Do you have any shell aliases defined? (Type alias) Those will affect commands typed at the command line, but not scripts. Commented Nov 17, 2010 at 1:46
  • That solved the problem... 'ls' was defined as 'ls --color'. I am curious - why does this make a difference? is the raw output of ls different when the color version is used? (Thanks!) Commented Nov 17, 2010 at 1:55
  • The colors are generated with escape codes, extra sequences of characters that are interpreted by the terminal program, but that you typically don't want to pass on to other programs. Commented Nov 17, 2010 at 1:59
  • The alias should be ls --color=auto so that ls can disable the colors when it's not outputting to a tty. Commented Nov 17, 2010 at 1:59
  • 4
    Please read mywiki.wooledge.org/ParsingLs; ls should never be used in this way. Commented Nov 17, 2010 at 2:31

2 Answers 2

6

`ls epi1_mcf_0*.nii.gz` is better written as simply epi1_mcf_0*.nii.gz. As in:

fslmerge -t output_file epi1_mcf_0*.nii.gz

The `ls` doesn't add anything.

Note: Posted as an answer instead of comment. The Markdown-lite comment parser choked on my `` `ls epi1_mcf_0*.nii.gz` `` markup.

Sign up to request clarification or add additional context in comments.

3 Comments

Not just a nitpick, very definitely better - can't have issues with variable ls output (color, say) if you don't use it!
@Jefromi - color is actually a minor issue compared to splitting on IFS; filenames with whitespace, nonprintable characters or newlines are among the many other problems with programmatic use of ls.
@Charles: Of course. I just mentioned color since it was the problem in this question.
2

(I mentioned this in a comment first, but I'll make an answer since it helped!)

Do you have any shell aliases defined? (Type alias) Those will affect commands typed at the command line, but not scripts.

Linux often has ls defined as ls --color. This may affect the output since the colour codes are sent as escape codes through the regular output stream. If you use ls --color=auto it will auto-detect whether its output is a terminal or not. From man ls:

By default, color is not used to distinguish types of files. That is equivalent to using --color=none. Using the --color option without the optional WHEN argument is equivalent to using --color=always. With --color=auto, color codes are output only if standard output is connected to a terminal (tty).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.