Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

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
";
for ( grep { /^[wd]/ } readdir DIR ){

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

I guess this is what you want:

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

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?


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/([[:lower:]])/U1/;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...