开发者

Commands work from Shell script but not from command line?

开发者 https://www.devze.com 2023-01-25 05:31 出处:网络
I quickly searched for this before posting, but could not find any similar posts. Let me know if they exist.

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 o开发者_运维知识库utput_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?)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消