Visit Our UNIX and Linux User Community


Is it possible in a shell script to modify itself ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible in a shell script to modify itself ??
# 1  
Old 03-22-2005
Is it possible in a shell script to modify itself ??

We are running a quiz and the data collected from the quiz is submitted to the database. My requirement is to write a shell script to get these submitted records.

I should be able to run this shell script at any time and the records it returns should be the ones submitted after the script was last run.

For eg : Assume i ran a script today and the number of records returned are 19, if i ran this script again tomorrow, the records i shd get should be the one submitted after today (excluding 19 records).

i will prefer using a variable in shell script which will hold the last run time.

for eg :
In my shell script can i declare a variable (last_run_time) in the begining, Use this variable thru the code and before exiting the shell script update this variable with the current date

.... shell script start .....
last_run_time=16/03/2005 10:10:10
..
spool test
select * from table1 where creattion_date > $last_run_time;
spool off
..

****
Here i wanted to update the last_run_time variable (declared above) with the current date, so that the next time i run the script i shd get the records which are inserted after i last ran the query
****
.... shell script end .....


Regards
# 2  
Old 03-22-2005
You can try as,

date >> /tmp/sqllogfile
mysql -q -h <host> -p <passwd> <<EOF
use <database>;
select COUNT(*) from <table>;
\q;
EOF >> /tmp/sqllogfile
echo >> /tmp/sqllogfile

It will log every information with row count in /tmp/sqllogfile file. Automate this script with cron.

HTH.
EOF
# 3  
Old 03-22-2005
Thanks Muthu for the reply...


I repeat my question :
Is it possible for a shell script to modify itself ??


In my shell script can i declare a variable (last_run_time) in the begining, Use this variable thru the code and before exiting the shell script update this variable with the current date

.... shell script start .....
last_run_time=16/03/2005 10:10:10
..
spool test
select * from table1 where creattion_date > $last_run_time;
spool off
..

****
Here i wanted to update the last_run_time variable (declared above) with the current date, so that the next time i run the script i shd get the records which are inserted after i last ran the query
****
.... shell script end .....

Regards
# 4  
Old 03-22-2005
why is it necessary to modify the script?
this is a PPP.

just put the date in a file, date>RunTimeFile, at the end of each run and read it the next time you run the script, e.g.:
last_run_time=$(cat RunTimeFile)

this assumes that there is never more than 1 instance of the script running.
# 5  
Old 03-23-2005
I have done something similar in a college AI class where we looked at the efficiency of different languages for AI. I wrote a script that replaced the file it was run from with new code, then ran the new script. Once the scirpt is running you can blow away it's file on disk since it is running from a copy in memory. I think you could pursue two methods to get the results you want.

1. In the script create a procedure that contains the entire text of the script itself and echo's it to $0 (this is the name of the file that was run in the first place). Then just run the procedure and have another section of code spit the new answers into the file as well.

2. Use a unique comment on the end of the lines containing the answers. Use sed to add/remove/modify lines in this section with the new answers from within the script.

This will work but using an external text file is probably a better way to go.
# 6  
Old 03-23-2005
Quote:
Originally Posted by TioTony
Once the scirpt is running you can blow away it's file on disk since it is running from a copy in memory.
That is true only for very small scripts. Most scripts will blow up if you try that.

Previous Thread | Next Thread
Test Your Knowledge in Computers #937
Difficulty: Medium
The echo reply is an ICMP message generated in response to an echo request; it is optional for all hosts, and must include the exact payload received in the request.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Modify Column Data using Shell Script

HI Guys, Input :- P081 wr1 12p0d5: 22.8 P081 wr1 12p2d18: 23.1 P149 wr1 1pxcud6/port_0_dev_7: 20.4 P149 wr1 1pxcud4/port_1_dev_10: 22.4 OutputP081 wr1 120 22.8 P081 wr1 122 23.1 P149 wr1 10 20.4 P149 wr1 11 22.4 In in First two line delete p and after d untill : In Last two line... (4 Replies)
Discussion started by: pareshkp
4 Replies

2. Shell Programming and Scripting

Modify XML tag using shell script

Hi All Need some help with a unix shell script. I have a XML file as shown below: <Root> <Service> <endPoint type="SOAP" protocol="http"> <provider>ABCD</provider> <urlRewrite>/service/xyz/getAccountDetails</urlRewrite> <timeout>30</timeout> </endPoint> </Service> <Service> <endPoint... (3 Replies)
Discussion started by: abhilwa
3 Replies

3. Shell Programming and Scripting

Modify cal command in shell script

Plz help me a) To display on the screen the sorted output of "who" along with the total no. of users. b) the same output (except the no. of users) should be in file FILE1. (2 Replies)
Discussion started by: shivasaini
2 Replies

4. Shell Programming and Scripting

Modify cal command in shell script

Plz help me To modify "cal " command to display calenders of the specified months. $ cal jan....oct (0 Replies)
Discussion started by: shivasaini
0 Replies

5. Shell Programming and Scripting

Modify cal command in shell script

Plz help me To modify "cal " command to display calenders of the specified months. $ cal jan mar nov (0 Replies)
Discussion started by: shivasaini
0 Replies

6. Shell Programming and Scripting

How to modify character to UTF-8 in shell script?

I have a shell script running to load some data from a text file to database. Text file contains some non-ASCII characters like . How can i convert these characters to UTF-8 codes before loading to DB. (5 Replies)
Discussion started by: vel4ever
5 Replies

7. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

8. Shell Programming and Scripting

Modify text file using shell script

Hi, I have a text file which is following format - COL VAL ABC 1 ABC 2 ABC 3 ABC 4 ABC 5 My requirement is to search for a particular value (provided by user) in the file and comment the previous entries including that as well. E.g. If I search for number 3, then the output... (6 Replies)
Discussion started by: bhupinder08
6 Replies

9. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

10. Shell Programming and Scripting

how to modify a file using shell script

Hi, i am using SuonOS and ksh. i need to add data into a file(s.txt) using a shell script. i have to pass 3 parameters and these 3 paramaters should add into the file at end of the file. File s.txt is look like, --------------------------------- column1|column2|column3 ... (1 Reply)
Discussion started by: syamkp
1 Replies

Featured Tech Videos