简体   繁体   English

复制粘贴代码有效但不能作为脚本

[英]copy paste code works but not as a script

I wrote a script with six if statements that looks like this:我写了一个包含六个 if 语句的脚本,如下所示:

#!/usr/bin/bash
if [ -n $var1 ]
        then
            for f in /path/*.fastq.gz
            do
                x=${f/%.fastq.gz/_sample1-forward.fastq.gz}
                y=${f/%.fastq.gz/_sample1-forward.out}
                q=${f/%.fastq.gz/_temp.fastq.gz}
                command [options] -i $f -o $temp${x##*/} 
                cp $temp${x##*/} $temp${q##*/}
            done
        else
            echo "no $var1"
            for f in /path/*.fastq.gz
            do
                q=${f/%.fastq.gz/_temp.fastq.gz}
                cp $f $temp${q##*/}
            done
fi

The other five statements do a similar task for var2 to var6.其他五个语句对 var2 到 var6 执行类似的任务。 When I run the script I get unexpected output (no errors no warnings), but when I copy paste each of the if statements to terminal I end up with the exact result I would expect.当我运行脚本时,我得到了意外的输出(没有错误没有警告),但是当我将每个 if 语句复制粘贴到终端时,我最终得到了我期望的确切结果。 I've looked for hidden characters or syntax issues for hours now.几个小时以来,我一直在寻找隐藏的字符或语法问题。 Could this be a shell issue?这可能是外壳问题吗? Script written on OSX (default zsh) and execution on server (default bash).在 OSX 上编写的脚本(默认 zsh)并在服务器上执行(默认 bash)。 I have a feeling this issue is similar but I couldn't find an answer to my issue in the replies.我觉得这个问题很相似,但我在回复中找不到我的问题的答案。

Any and all ideas are most welcome!任何和所有的想法都是最受欢迎的!

Niwatori庭取

You should maybe look at the shebang.你也许应该看看shebang。 I think proper usage would be #!/usr/bin/env bash or #!/bin/bash .我认为正确的用法是#!/usr/bin/env bash#!/bin/bash

thanks for the help, much appreciated.感谢您的帮助,非常感谢。 Shebang didn't seem to be the problem although thanks for pointing that out.尽管感谢指出这一点,但 Shebang 似乎不是问题所在。 Shellcheck.net reminded me to use Shellcheck.net 提醒我使用

[[ ]] [[]]

for unquoted variables, but that didn't solve the problem either.对于未引用的变量,但这也没有解决问题。 Here's what went wrong and how I 'fixed' it:这是出了什么问题以及我如何“修复”它:

for every variable the same command (tool) is used which relies on a support file (similar in format but different content).对于每个变量,使用依赖于支持文件(格式相似但内容不同)的相同命令(工具)。 Originally, before every if statement I replaced the support file for the previous variable with the one needed for the current variable.最初,在每个 if 语句之前,我将前一个变量的支持文件替换为当前变量所需的支持文件。 For some reason (curious why, any thoughts are welcome) this didn't always happen correctly.出于某种原因(好奇为什么,欢迎提出任何想法)这并不总是正确发生。 As a quick workaround I made six versions of the tool, all with a different support file and used PYTHONPATH=/path/to/version/:$PYTHONPATH before every if statement.作为一种快速解决方法,我制作了该工具的六个版本,所有版本都具有不同的支持文件,并在每个 if 语句之前使用 PYTHONPATH=/path/to/version/:$PYTHONPATH。 Best practice would be to adapt the tool so it can use different support files or an option that deals with repetitive tasks but I don't have the time at the moment.最佳做法是调整该工具,以便它可以使用不同的支持文件或处理重复性任务的选项,但我目前没有时间。

Have a nice day, Niwatori祝你有美好的一天,Niwatori

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

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