Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Source command in perl script

by bannaN (Acolyte)
on Oct 20, 2011 at 08:22 UTC ( #932586=perlquestion: print w/replies, xml ) Need Help??
bannaN has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to create a perlscript that executes a DB2 ddl file for initializing a database.

But the problem is that in order to do so (Script is runned by root) i have to do a source command.

source /home/db2inst1/sqllib/db2profile
How can i execute this inside a script, and make the script inherit this environment, so that commands later in the script issued with system will inherit this env?

if if run
`source /home/db2inst1/sqllib/db2profile`;
This will result in the following error 'Can't exec "source": No such file or directory' even though i can execute the same command directly in the shell.

Replies are listed 'Best First'.
Re: Source command in perl script
by GrandFather (Sage) on Oct 20, 2011 at 08:26 UTC

    Can you do what you need using DBI and DBD::DB2?

    True laziness is hard work
      To my knowledge DBD::DB2 doesnt support execution of sql scripts / ddl file that sets up a database. But if thats wrong i would be very happy if someone would enlighten me.

      Just to be clear. The task of the ddl file contains definition of numerous tables.


        One of the examples (Listing 5) in this article show how to do DDL with DBI and DBD::DB2, so perhaps you can just convert your script.


        When your only tool is a hammer, all problems look like your thumb.

        Update: Trivial edit (specify which example performs DDL), changed plural to singular.

Re: Source command in perl script
by Utilitarian (Vicar) on Oct 20, 2011 at 08:45 UTC
    What's wrong with running it in the apropriate shell?
    ie. /bin/sh /home/db2inst1/sqllib/db2profile

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Have the script like this?
      `/bin/sh /home/db2inst1/sqllib/db2profile`; system("db2 terminate");
      If i try to run something like this, the db2 command will not be recognized.
      Error: "Can't exec "db2": No such file or directory"

      So if i understood you correctly that doesnt work for some reason. As you may have understood i am not that into the logic of how different shells acquire their environment, and pass it on.
      Am i right in the assumption that the system command will fork and inherit the environment from the perl script?
      If i am, the question still stands, how can i make the perl script acquire the db2 environment
        A parent process cannot inherit from a process it spawns.

        You can however set the environment within the Perl script and then the system call will inherit it or you could run the db2profile scipt prior to running the Perl script and then everything would inherit it.

        Rewriting as Perl

        $ENV{PATH}="$ENV{PATH}:/opt/db2/bin" ...
        Or calling everything from a shell script...
        #!/bin/sh . /home/db2inst1/sqllib/db2profile /home/db2instl/bin/ # Or whatever the script is called

        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Source command in perl script
by pjcj (Novice) on Oct 20, 2011 at 13:19 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932586]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-05-24 20:11 GMT
Find Nodes?
    Voting Booth?