从 R 脚本运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11395217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:42:55  来源:igfitidea点击:

Run a bash script from an R script

rbash

提问by cianius

So I have this programme samtools that I want to use from cmd line, converting one file to another. It works like this:

所以我有这个程序 samtools,我想从 cmd 行使用它,将一个文件转换为另一个文件。它是这样工作的:

bash-4.2$ samtools view filename.bam | awk '{OFS="\t"; print ">""\n"}' - > filename.fasta

As I want to automate this, I would like to automate it by using an R script. I know you can use system() to run an OS command, but I cannot get it to work by trying

由于我想自动执行此操作,因此我想使用 R 脚本来自动执行此操作。我知道您可以使用 system() 来运行操作系统命令,但我无法通过尝试使其工作

system(samtools view filename.bam | awk '{OFS="\t"; print ">""\n"}' - > filename.fasta)

Is it just a matter of using regexes to get rid of spaces and stuff so the comma nd argument system(command) is readable? How do I do this?

是否只是使用正则表达式摆脱空格和东西以便命令参数系统(命令)可读的问题?我该怎么做呢?

EDIT:

编辑:

system("samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > first_batch_1.fasta") Error: unexpected input in "system("samtools view filename.bam | awk '{OFS="\"

system("samtools 查看文件名.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > first_batch_1.fasta") 错误:"system("samtools 查看文件名) 中有意外输入.bam | awk '{OFS="\"

EDIT2:

编辑2:

system("samtools view filename.bam | awk '{OFS=\"\t\"; print \">\"$1\"\n\"$10}' - > filename.fasta")

system("samtools 查看文件名.bam | awk '{OFS=\"\t\"; print \">\"$1\"\n\"$10}' -> filename.fasta")

awk: cmd. line:1: {OFS="    "; print ">""
awk: cmd. line:1:                         ^ unterminated string
awk: cmd. line:1: {OFS="    "; print ">""
awk: cmd. line:1:                         ^ syntax error
> 

EDIT3: And the winner is:

EDIT3:获胜者是:

system("samtools view filename.bam | awk '{OFS=\"\t\"; print \">\"\"\n\"}' -> filename.fasta")

回答by Andrie

The way to debug this is to use catto test whether your character string has been escaped correctly. So:

调试这个的方法是用来cat测试你的字符串是否被正确转义。所以:

  1. Create an object xwith your string
  2. Carefully escape all the special characters, in this case quotes and backslashes
  3. Use cat(x)to inspect the resulting string.
  1. x用你的字符串创建一个对象
  2. 小心地转义所有特殊字符,在这种情况下是引号和反斜杠
  3. 使用cat(x)检查结果字符串。

For example:

例如:

x <- 'samtools view filename.bam | awk \'{OFS="\t"; print ">""\n"}\' - > filename.fasta'

cat(x)
samtools view filename.bam | awk '{OFS="\t"; print ">""\n"}' - > filename.fasta

If this gives the correct string, then you should be able to use

如果这给出了正确的字符串,那么您应该能够使用

system(x)