简体   繁体   English

命令可以从Shell脚本运行,但不能从命令行运行?

[英]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" 该目录包含一堆名为“epi1_mcf_0 ###。nii.gz”的文件

Command-line version (bash is running when this is executed): 命令行版本(bash在执行时运行):

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

Shell script version: Shell脚本版本:

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

The command-line version fails, but the shell script one works perfectly. 命令行版本失败,但shell脚本完美无缺。

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?) (或者是普遍认为应该在“fslmerge”函数中查找问题?)

`ls epi1_mcf_0*.nii.gz` is better written as simply epi1_mcf_0*.nii.gz . `ls epi1_mcf_0*.nii.gz`最好写成epi1_mcf_0*.nii.gz As in: 如:

fslmerge -t output_file epi1_mcf_0*.nii.gz

The `ls` doesn't add anything. `ls`不添加任何东西。

Note: Posted as an answer instead of comment. 注意:作为答案而不是评论发布。 The Markdown-lite comment parser choked on my `` `ls epi1_mcf_0*.nii.gz` `` markup. Markdown-lite注释解析器在我的`` `ls epi1_mcf_0*.nii.gz` ``标记上`` `ls epi1_mcf_0*.nii.gz` ``

(I mentioned this in a comment first, but I'll make an answer since it helped!) (我先在评论中提到了这一点,但是我会做出答案,因为它有所帮助!)

Do you have any shell aliases defined? 你有定义的shell别名吗? (Type alias ) Those will affect commands typed at the command line, but not scripts. (类型alias )这些将影响在命令行键入的命令,但不会影响脚本。

Linux often has ls defined as ls --color . Linux经常将ls定义为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. 如果使用ls --color=auto ,它将自动检测其输出是否为终端。 From man ls : 来自man ls

By default, color is not used to distinguish types of files. 默认情况下,颜色不用于区分文件类型。 That is equivalent to using --color=none . 这相当于使用--color=none Using the --color option without the optional WHEN argument is equivalent to using --color=always . 使用不带可选WHEN参数的--color选项相当于使用--color=always With --color=auto , color codes are output only if standard output is connected to a terminal (tty). 使用--color=auto ,仅当标准输出连接到终端(tty)时才输出颜色代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM