It is currently Tue, 17 May 2022 01:38:06 GMT



 
Author Message
 Script to spaw and run a script
Is there a method to have a shell script write the contents of child
shell script, and then have the parent execute the child script?  I
would like to write one, the parent, script so it may be easily moved
from one host to another.  The child script will remain the same and
maybe deleted before/after it is done.  The key is to ensure when the
child script is written and the variables are not parsed; just spit
out the child with no pre-processing.  Here is an example of what I am
attempting to accomplish.

#!/sbin/sh

child_script() {

        if [ -f /tmp/child ] ; then
                rm /tmp/child;
        fi

        cat << EOF-CHILD > /tmp/child;
#!/sbin/sh

        today=`date +%Y-%m-%d`;

        echo "child on ${date}";

EOF-CHILD

        chmod 755 /tmp/child;

echo "parent";

child_script;

/tmp/child;



 Sat, 30 Sep 2006 09:44:26 GMT   
 Script to spaw and run a script

Yes. Create the file then execute it.

  I

Deleted before it's done?

   The key is to ensure when the

Variables not parsed?

just spit

What pre-processing?

   Here is an example of what I am

Just make the above "rm -f /tmp/child", but you don't even need that
since it'll be overwritten by your "cat".

Did you want this to be the date/time when the child script was created
or when it runs?

Looks fine, but what's with all the semi-colons?

I assume you tried this and weren't happy with the results. If you'd
like help with something, let us know what you expected to happen and
what did happen instead.

        Ed.



 Sat, 30 Sep 2006 22:09:14 GMT   
 Script to spaw and run a script

You almost have it. All that you need to know is that if you quote the
EOF-CHILD, the terminator of the HERE document, then the variables in the
document are not expanded. i.e.

#!/sbin/sh

child_script() {
        rm -f /tmp/child;

        cat << 'EOF-CHILD' > /tmp/child;
#!/sbin/sh
        today=`date +%Y-%m-%d`;
        echo "child on ${date}";
EOF-CHILD
        chmod 755 /tmp/child;



 Sun, 01 Oct 2006 08:33:29 GMT   
 Script to spaw and run a script
Thanks for the response.  See answers below.

[snip]

I would like for the child script to be generated if needed.  I may
add a removal after running the child script.  I'm not sure on that.
Yes, my order is backwards, I should create it, run it, then remove it
if I decide that needs to be done.

What I intend is for the child script to have variables and it would
process those variables not the parent.  Currently, the parent
processes the variables and then spits out the child script.  The
downside is this defeats the possiblity of the child having internal
variables.  I ran the script with a sh -x, see the results below.

--------script output---------
+ echo parent
parent
+ child_script
+ [ -f /tmp/child ]
+ rm /tmp/child
+ cat
+ date +%Y-%m-%d
#!/sbin/sh

        today=2004-04-13;

        echo "child on ";

+ chmod 755 /tmp/child
+ /tmp/child
child on

--------contents of child script---------
#!/sbin/sh

        today=2004-04-13;

        echo "child on ";

--------what I expected the child to look like---------
#!/sbin/sh

        today=`date +%Y-%m-%d`;

        echo "child on ${date}";

What happens is the parent processed the variables and then spit out
the child.  I want the child to determine when it runs what the values
of the variables will be.  As you can see, the $today variable does
not contain a value when it is needed by the echo command.  Basically,
the parent is attempting to process some of what I need put into the
child, when all I want is for it to spit out the childs code with no
thought into it.  This may be the behaviour, but it is not what I am
trying to achieve.

[snip]

I originally learned to program in C++.  I know in sh you can use a
semi-colon to denote the end-of-line or the end of a command.  Old
habit, I like seeing them to denote I have finished a command and am
ready to run a new one.  Just my style I guess.

Thanks in advance.



 Sun, 01 Oct 2006 08:54:33 GMT   
 Script to spaw and run a script

You almost have it, what you are missing is the knowledge that quoting
the HERE document delimiter causes the document itself to be unprocessed.
i.e.

child_script() {
        rm -f /tmp/child
        cat << 'EOF-CHILD' > /tmp/child
#!/sbin/sh
        today=`date +%Y-%m-%d`
        echo "child on ${today}"
EOF-CHILD
        chmod 755 /tmp/child



 Sun, 01 Oct 2006 09:57:16 GMT   
 Script to spaw and run a script
Thanks Icarus!  That did the trick.


 Sun, 01 Oct 2006 20:33:40 GMT   
 
   [ 6 post ] 

Similar Threads

1. scripts running other scripts

2. RUNNING A GROUP OF SCRIPTS FROM A SINGLE SUPERVISORY SCRIPT

3. Incomplete output from crontab email: running shell scripts from a perl script

4. Running scripts without >csh script!

5. Running scripts using variables in a parent script

6. script that run a sql script

7. running a script form csh script


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software