Find the answer to your Linux question:
Results 1 to 5 of 5
Hello people. This is a question that has been answered probably thousands of times, but without knowing the exact jargon I haven't been able to find a single solution. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    19

    Run Script on Different Files


    Hello people.

    This is a question that has been answered probably thousands of times, but without knowing the exact jargon I haven't been able to find a single solution.

    I have a perl script that parses a spreadsheet file and a bash script that interprets the parsing. I want to be able to run new files through the parsing and interpreting scripts without having to change the filename in both of those scripts, and to generate a output name based on the name of the spreadsheet file I feed them both.

    How can I alter the script so that I can set the filename I want it to process in the same command line, without changing the script itself every time?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,444
    Hi and welcome

    it is not fully clear to me which filenames shall stay the same and which will change per run.
    Can you please show an example of the workflow as it is right now?
    Maybe with comments what you want to have changed.

    In general and what I think might apply:
    1) It is a good idea to stay within one language for a project/issue. At least if you create and maintain the tools/scripts yourself.
    Which in your case probably means perl.
    The advantages are: reuse of code and data structures, minimzing of data handover/synchronisation, maintainability, portability, etc
    2) If I read your post correct, then there are hardcoded filenames/paths in both scripts.
    Dont do that. Use parameters or config files instead.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Feb 2013
    Posts
    19
    Quote Originally Posted by Irithori View Post
    Hi and welcome

    it is not fully clear to me which filenames shall stay the same and which will change per run.
    Can you please show an example of the workflow as it is right now?
    Maybe with comments what you want to have changed.

    In general and what I think might apply:
    1) It is a good idea to stay within one language for a project/issue. At least if you create and maintain the tools/scripts yourself.
    Which in your case probably means perl.
    The advantages are: reuse of code and data structures, minimzing of data handover/synchronisation, maintainability, portability, etc
    2) If I read your post correct, then there are hardcoded filenames/paths in both scripts.
    Dont do that. Use parameters or config files instead.
    Hey, thanks for your reply.

    I understand it would be ideal to have a single language for the same project, but the perl parsing script was built by someone else, and built upon many modules. I have no knowledge of perl and I'm not a programmer, so I went with what I know -- bash -- to make take the parsed file and pull out the data I want.

    You are correct, I did hard code the filenames into my scripts, but my question was HOW do I create "parameters or config files" so that I can point my the perl and bash scripts to a softcoded filename? To say it in a different way, I want to bring in new spreadsheet files as they are generated by the data collectors. I assign them a filename based on their date of release. Then I want to say "run this parsing script (perl) on THIS spreadsheet", then in bash, say "run this interpretation script (bash) on THIS parsed spreadsheet file".


    EDIT:

    I looked up how to use parameters in bash now, so I supplied my script with filename=$1 and putting my parsed filename as an argument works.

    What about with perl? How can I get it to take the filename as an argument? Here's the code I got from the manual of Spreadsheet::ParseExcel.

    Code:
        #!/usr/bin/perl -w
    
        use strict;
        use Spreadsheet::ParseExcel;
    
        my $parser   = Spreadsheet::ParseExcel->new();
        my $workbook = $parser->parse('13_01_28.xls');
    
        if ( !defined $workbook ) {
            die $parser->error(), ".\n";
        }
    
        for my $worksheet ( $workbook->worksheets() ) {
    
            my ( $row_min, $row_max ) = $worksheet->row_range();
            my ( $col_min, $col_max ) = $worksheet->col_range();
    
            for my $row ( $row_min .. $row_max ) {
                for my $col ( $col_min .. $col_max ) {
    
                    my $cell = $worksheet->get_cell( $row, $col );
                    next unless $cell;
                    my $name = $worksheet->get_name();
    
                    print "$row,$name,$col,";
                    print "",$cell->value(),"";
                    print "\n";
                }
            }
        }
    Last edited by deuteros; 02-18-2013 at 03:00 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,653
    You would use the @ARGV array in your script. See here for more info
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  6. #5
    Just Joined!
    Join Date
    Feb 2013
    Posts
    19
    Quote Originally Posted by elija View Post
    You would use the @ARGV array in your script. See (URL) for more info
    Thanks for the link. I played around with some variations, but it seems to like the reference to $ARGV[0] in single quotes just fine.

    Thanks!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •