0

I've created a variable $date in Main.pl script that I would like to pass to Annotator.pl script. I use shell script to execute Annotator.pl. I can't figure out how to pass $date to Annotator.pl. When I run my $date = $ARGV[0]; in Annotator.pl I get the name of the current directory, however, $date = $ARGV[1]; returns nothing.

Please see the code below. The date is important because it has to be exact and I can't figure out how to pass it to Annotator.pl. Thanks for your help.

Main.pl Script:

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

    my $sec; my $min; my $hour; my $mday; my $mon; my $year; my $wday; my $yday;    my $isdst;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
    $mon=$mon+1; $year = 1900+$year;
    if (length($mon)==1) {$mon="0".$mon;}
    if (length($mday)==1) {$mday="0".$mday;}
    if (length($hour)==1) {$hour="0".$hour;}
    if (length($min)==1) {$min="0".$min;}
    if (length($sec)==1) {$sec="0".$sec;}

    my $date = "$mon"."_"."$mday"."_"."$year"."-".$hour.$min.$sec;

    my $cmd5 = `perl MDL_unzip_annotate.sh /data/test_all_runs pVCF $date`;   print "$cmd5";

Shell script: MDL_unzip_annotate.sh that executes Annotator.pl

home="/data/test_all_runs" #location of the run directory from which the program is launched
   scripts="/data/test_scripts"  

     datapath=$1 #this is called in Main.pl as [test_all_runs]
     process=$2 #the process  

    if [[ "$process" == "pVCF" ]];then
            cd $datapath
            folders="$(ls)"
            cd $scripts
        for ff in $folders; do
            dname=$ff
                echo $dname
                if [  ! -f $dname ];then

                    cmd2="perl Annotator.pl $dname"
                    echo $cmd2

                    cmd2=`perl Annotator.pl $dname`
                    echo $cmd2
                fi
            done    
    done          

    fi

Annotator.pl script:

#!perl
    use strict;
    use warnings;

    my $date = $ARGV[1]; print "the date is######## ".$date."\n";
13
  • perl MDL_unzip_annotate.sh /data/test_all_runs pVCF $date is wildly dangerous -- concatenating your command into a string means you're calling system(), which runs sh -c with that string as an argument, which means you're vulnerable to shell injection attacks. Don't do that. Commented Jul 19, 2016 at 19:56
  • I'm a novice... please suggest how to pass $date to Annotator.pl. Thanks Commented Jul 19, 2016 at 19:58
  • Think about what happens if you populate your year from user input rather than the local time -- if the user runs the program with 1994$(rm -rf $HOME) as the year, you don't want the system to actually invoke rm, but that's what'll happen if that string is passed to a shell -- and literal quoting is easy to work around. Commented Jul 19, 2016 at 19:58
  • Run your shell component through shellcheck.net, and fix the bugs it identifies. Commented Jul 19, 2016 at 19:58
  • Also, see mywiki.wooledge.org/ParsingLs Commented Jul 19, 2016 at 19:59

1 Answer 1

1

Instead of using shell script I will use the following code to capture the name of the folder in a specific directory:

opendir my $dir, "/data/test_all_runs" or die "Cannot open directory: $!";
my @run_folder = readdir $dir;
closedir $dir;
my $last_one = pop @run_folder; print "The folder is".$last_one."\n";

Thanks for your suggestions.

Sign up to request clarification or add additional context in comments.

1 Comment

Great :) Please note, readdir returns files without any path. So to get workable lists you should prepend path as needed. For example, @filenames = map { "$path/$_" } readdir $dir. Or you can chdir $dirname and then work with bare names. But then do not forget that your script's working directory is $dirname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.