Angry_at_Linux
Angry_at_Linux

Reputation: 73

Commands work from Shell script but not from command line?

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?)

Upvotes: 3

Views: 2637

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 994531

(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).

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 362037

`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.

Upvotes: 6

Related Questions