0

I have started learning Perl scripting today because of my project.

Let me first define you my project so that you have clear image what I wanted to do .

I am scripting for the test run of applications. When my Script will execute it will take 3 arguments from user and then it will source the *.ini file for the application . after sourcing that file it this script will execute another perl script from a different location and this second perl script will give me two text files in (lets say) folder1. and files will be test.date1.txt and test.date11.txt after then my script will call another perl script from different location for the same application and this third perl script will also give me two different text files(test.date2.txt and test.date22.txt) in different folders. Now I want to rename the files in both 1st and 2nd folder 1st run results 2nd run results resoectively and I want to compare what are the differences.

Till Now I have assigned my input arguments and if user don't give the arguments then what error to show to the user , and also I have read about the comparison on online tutorial and made a little comparison code like this :

use Text::Diff;

my $diffs = diff 'test.date1.csv' => 'test.date2.csv';

print $diffs;

I am stuck at how to source my *.ini file , and then run my other perl scripts ? Can anyone help me with this. It would be a great help for me .

Note: I started scripting today, so sorry if I made a silly mistake any where defining the project

user2828488
  • 65
  • 1
  • 8
  • so you want to write your ini file and use the absolute path as a parameter for another script? – Alex Tape Jan 20 '14 at 18:58
  • I already have ini file , and other two perl scripts , I use "Source my.ini" in shell terminal to source the ini file , Now I don't know how to run the source my.ini command in perl. – user2828488 Jan 20 '14 at 19:04
  • Wait ... you mean `source my.ini`? So your 'ini file' is not an [ini file](https://en.wikipedia.org/wiki/INI_file) but a a shell script? That is confusing! – reinierpost Jan 21 '14 at 07:38
  • What does the shell script do? It's probably easier to rewrite that in Perl than to source the script and then use the results (which can only be done in a really convoluted way: from Perl, `exec` a shell that first runs the script, then `exec`s Perl with the rest of your Perl script). – reinierpost Jan 21 '14 at 07:42

2 Answers2

3

ok.. i think i got it..

you mean the bash shell built-in command

source

that executes the content of the file passed as argument, in the current shell

. filename [arguments] source filename [arguments]

to reload your bash setting for current running bash you can do

source ~/.bashrc

or

. ~/.bashrc

Link

which source

man bash

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing filename. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

maybe good to know: Is there a difference between “.” and “source” in bash, after all?


well, to answer your question.. you are looking for one of these:

  1. system() you want to execute a command and don't want to capture its output

  2. exec you don't want to return to the calling perl script

  3. backticks you want to capture the output of the command

  4. open you want to pipe the command (as input or output) to your script


  1. example system()

    system("command arg1 arg2 arg3");
    

    or

    system("command", "arg1", "arg2", "arg3");
    
  2. example exec

    exec("ls -l");
    

    keep in mind exec() executes the command specified and never returns to the calling program, except in the case of failure because the specified command does not exist AND the exec argument is an array.

  3. example backticks

    $result = `command arg1 arg2`;
    

    or even

    @result = `command arg2 arg2`;
    

    ..command is executed and the output of the command is returned to the calling script

    try

    $result = `command 2>&1`;
    

    to get sterr as well as stdout

  4. example open()

    open(PS,"ps -e -o pid,stime,args |") || die "Failed: $!\n";
    while ( <PS> ) {
        ...
    }
    

    open() captures the data of a command

    open("command |"))
    

    furthermore you can feed an external command with data generated from your script

    open("| command")
    

    just pipe playin ;)

Community
  • 1
  • 1
Alex Tape
  • 2,291
  • 4
  • 26
  • 36
  • Thank you for your help , I got some help from internet too that I can run the command in perl by `system (". /my.ini");` but since I dnt have permission to usr/bin so it won't work , I have to run it via source my.ini via command line , I have also found 1 more solution and that is I can make wrapper script and with in perl I can call that wrapper script , Sorry I don't have idea about wrapper scripts so may be I am defining it wrong – user2828488 Jan 21 '14 at 21:55
1

What do you mean by sourcing an .ini file? An .ini file is a configurations file. You need an ini parser. Here's a SO link to help with that: How can I access INI files from Perl? .

Personally I used Tiny and I found it easy and suitable for my needs.

Community
  • 1
  • 1
Eugene K
  • 3,381
  • 2
  • 23
  • 36
  • I use "Source my.ini" in shell terminal to set the enviroment variables, this ini files has Shell scripting and some other data for my application. – user2828488 Jan 20 '14 at 19:05
  • You can use system("source my.ini"). There are several caveats with using a system call though. It might work for your problem. – Eugene K Jan 20 '14 at 19:10
  • @ Eugene K: Thanks , I tried at my work PC but this command didn't work there but I tried in my laptop it with just "echo hello" in my.ini. If i just write "source my.ini" then it's showing the "hello" but if I write this system("source my.ini") in perl script then it just execute it but don't show the output in terminal . At my work pc it was giving me error that ini is not exec. – user2828488 Jan 20 '14 at 19:43
  • Hello, Can anyone explain me what is RAC in perl ? I got some reference from intnenet about sourcing the shell script here is the example: `make_path( "$self->{project_rerun_path}/$self->{cfgname}.$self->{id}" ); open RAC, ">$self->{project_rerun_path}/$self->{cfgname}.$self->{id}/run_and_check.csh"; print RAC '#!/bin/csh -f' . "\n"; print RAC "setenv CT_LIB_PATH $self->{my_path}\n"; print RAC "cd $self->{my_path}\n"; print RAC "source my.ini\n"; }` – user2828488 Jan 23 '14 at 22:45