Register
It is currently Thu Nov 27, 2014 7:08 am

changing script during interpretation causes trouble


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Feb 18, 2010 4:35 am   

Joined: Thu Feb 18, 2010 4:26 am
Posts: 3
Hi,

I've a well unterstood problem, I wonder if there is a solution to it exists.

I've a script, let's call it qwe. qwe calls script asd, which finds an update(zxc) for script qwe and updates it. When it gets back to qwe, it continues to run the updated script zxc from the location where it left after calling asd.

What I want to do is, let shell load qwe. Run it in memory and do not go back to updated zxc after asd quits, and just continue to run qwe till the end.

Thanks in advance,
Ozgur

Files:

qwe
Code:
#! /bin/bash

echo line a1

. /home/ozgur/bin/asd


asd
Code:
#! /bin/bash

cp /home/ozgur/bin/zxc /home/ozgur/bin/qwe


zxc
Code:
#! /bin/bash

echo line b1
echo line b2
echo line b3
echo line b4
echo line b5
echo line b6
echo line b7

/home/ozgur/bin/asd


Top
 Profile  
 PostPosted: Fri Feb 19, 2010 8:59 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
There was a very similar question in the basics forum ... what's going on?

To keep it simple: do not modify interpreted scripts while running and expect reasonable results! There is always a better way. In this case, just use exec. This will replace the running instance with whatever script you call. You can use this in qwe (and zxc, which replaces qwe). For example:

qwe
Code:
#!/bin/bash
echo line a1
exec /home/ozgur/bin/asd


Top
 Profile  
 PostPosted: Tue Feb 23, 2010 4:48 am   

Joined: Thu Feb 18, 2010 4:26 am
Posts: 3
thobbs wrote:
In this case, just use exec. This will replace the running instance with whatever script you call. You can use this in qwe (and zxc, which replaces qwe).


I tried this, but it never comes back from asd to qwe again, it runs asd till end and quits. this is not exactly what I want.


Top
 Profile  
 PostPosted: Tue Feb 23, 2010 12:46 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Sorry, your specifications from the first post weren't clear to me. Your example qwe terminates after calling asd.

if you want to continue running qwe after calling asd, just call asd normally: "./asd" or similar. However, if qwe is going to continue to run, you should not overwrite qwe while it is running. As a general rule, self-modifying source code is a bad idea, but when it's an interpreted language, you really just cannot do it.

Could you give something closer to the exact code that you are using? I can't accurately recommend what you should do at this point.

- thobbs


Top
 Profile  
 PostPosted: Fri Feb 26, 2010 8:44 am   

Joined: Thu Feb 18, 2010 4:26 am
Posts: 3
thobbs wrote:
Could you give something closer to the exact code that you are using? I can't accurately recommend what you should do at this point.


Sorry I can't. Actual files are really big. But I update my sample scripts to reflect my expectations. Here they are:

qwe
Code:
#! /bin/bash

echo line a1

/home/ozgur/bin/asd

echo doing qwe stuff


asd
Code:
#! /bin/bash

cp /home/ozgur/bin/zxc /home/ozgur/bin/qwe


zxc
Code:
#! /bin/bash

echo line b1
echo line b2
echo line b3
echo line b4
echo line b5
echo line b6
echo line b7

/home/ozgur/bin/asd

echo doing zxc stuff


expected output from qwe
Code:
line a1
doing qwe stuff


actual output
Code:
line a1
/home/ozgur/bin/qwe: line 6: e: command not found
line b4
line b5
line b6
line b7
doing zxc stuff


Top
 Profile  
 PostPosted: Sat Feb 27, 2010 12:55 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Why not just wait until qwe is done to copy zxc onto qwe? You're not running the zxc code during the execution of qwe anyway.

Just call "qwe && cp zxc qwe", or write a script that basically does the same thing.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: steveT and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP