【问题标题】:How to give output location of file in shell script?如何在shell脚本中给出文件的输出位置?
【发布时间】:2019-02-07 11:33:35
【问题描述】:

我有一个包含 Perl 脚本和 R 脚本的 Shell 脚本。

我的 Shell 脚本 R.sh:-

#!/bin/bash
./R.pl                               #calling Perl script
`perl -lane 'print $F[0]' /media/data/abc.cnv > /media/data/abc1.txt`;
                                     #Shell script
 Rscript R.r                         #calling R script

这是我的 R.pl(头):-

`export path=$PATH:/media/exe_folder/bin`;
print "Enter the path to your input file:";
$base_dir ="/media/exe_folder";
chomp($CEL_dir = <STDIN>);
opendir (DIR, "$CEL_dir") or die "Couldn't open directory $CEL_dir";
$cel_files = "$CEL_dir"."/cel_files.txt";
open(CEL,">$cel_files")|| die "cannot open $file to write";
print CEL "cel_files\n";
for ( grep { /^[\w\d]/ } readdir DIR ){

print CEL "$CEL_dir"."/$_\n";
}close (CEL);

Perl 脚本的输出是 Shell 脚本的输入,Shell 的输出是 R 脚本的输入。

我想通过提供输入文件名和输出文件名来运行 Shell 脚本:-

./R.sh  home/folder/inputfile.txt  home/folder2/output.txt

如果文件夹包含许多文件,那么它只需要用户定义文件并处理它。 有没有办法做到这一点?

【问题讨论】:

  • 您可以将文件名作为参数传递给脚本,也可以通过环境变量传递。
  • 您的意思是perl perl_script.pl &lt;parameters for perl&gt; ¦ sh shell_script.sh &lt;parameters for shell&gt; ¦ r r_script.r &lt;parameters for r script&gt; 并且您想将该命令行放入另一个shell 脚本中? (假设 perl 和 shell 脚本写入 stdout 并且 shell 和 R 脚本从 stdin 读取)
  • @StefanBecker 我只想为 shell 脚本传递一次参数。 ./R.sh 。 R.sh 首先调用一个 Perl 脚本,然后调用一个 R 脚本。
  • 请提供minimal reproducible example,在您的情况下至少提供R.sh的内容
  • @StefanBecker 查看编辑

标签: r shell perl variables


【解决方案1】:

我想这就是你想要的:

#!/bin/bash
# command line parameters
_input_file=$1
_output_file=$2

# @TODO: not sure if this path is the one you intended...
_script_path=$(dirname $0)

# sanity checks
if [[ -z "${_input_file}"  ]] ||
   [[ -z "${_output_file}" ]]; then
    echo 1>&2 "usage: $0 <input file> <output file>"
    exit 1
fi
if [[ ! -r "${_input_file}" ]]; then
    echo 1>&2 "ERROR: can't find input file '${input_file}'!"
    exit 1
 fi

 # process input file
 # 1. with Perl script (writes to STDOUT)
 # 2. post-process with Perl filter
 # 3. run R script (reads from STDIN, writes to STDOUT)
 perl ${_script_path}/R.pl <"${_input_file}"      | \
     perl -lane 'print $F[0]'                     | \
     Rscript ${_script_path}/R.r >"${_output_file}"

 exit 0

请查看被调用脚本的行为说明。

注意:我不太明白为什么需要使用 Perl 过滤器对 Perl 脚本的输出进行后处理。为什么不直接将它集成到 Perl 脚本本身呢?


奖励代码:这是您在R.pl 中编写主循环以充当适当过滤器的方式,即从STDIN 读取行并将结果写入STDOUT。您也可以在其他语言中使用相同的方法,例如R.

#!/usr/bin/perl
use strict;
use warnings;

# read lines from STDIN
while (<STDIN>) {
    chomp;

    # add your processing code here that does something with $_, i.e. the line
    # EXAMPLE: upper case first letter in all words on the line
    s/\b([[:lower:]])/\U\1/;

    # write result to STDOUT
    print "$_\n";
}

【讨论】:

  • 请参阅编辑:我的代码采用文件的路径现在我想修改它在 Perl 脚本中需要进行哪些更改。
  • 在脚本中硬编码文件路径是个坏主意。相反,要么读/写 STDIN/STDOUT,要么接受文件名作为命令行参数。我认为这是您问题的根本原因。
猜你喜欢
  • 2013-10-16
  • 2016-05-02
  • 2018-05-15
  • 2012-11-15
  • 2013-04-10
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多