如何在shell脚本中给出文件的输出位置? [英] How to give output location of file in shell script?

查看:88
本文介绍了如何在shell脚本中给出文件的输出位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a a Shell script that contain a Perl script and R script.

我的 Shell 脚本 R.sh:-

my Shell script 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(头):-

This is my R.pl (head):-

`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 脚本的输入.

The output of Perl script is input for Shell script and Shell's output is input for R script.

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

I want to run the Shell script by providing the input file name and output file name like :-

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

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

If folder contain many files then it will take only user define file and process it. Is There is a way to do this?

推荐答案

我猜这就是你想要的:

#!/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

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

Please see the notes how the called scripts should behave.

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

NOTE: I don't quite understand why you need to post-process the output of the Perl script with Perl filter. Why not integrate it directly into the Perl script itself?

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

BONUS CODE: this is how you would write the main loop in R.pl to act as proper filter, i.e. reading lines from STDIN and writing the result to STDOUT. You can use the same approach also in other languages, e.g. 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";
}

这篇关于如何在shell脚本中给出文件的输出位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆