LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2006, 07:16 PM   #1
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Rep: Reputation: 30
Wink Need a simple shell script please overwritting files.


Basically has to operate as follows:

Got the following files:

file1
file2
file3
file4
file5

themainfile

I want to copy file1 over themainfile, 2 mins later I want to copy file2 over themainfile and continue on from there.

It's probably simple but not sure where to even start.

Not that fuzzed about using maybe a perl script, doesn't have to be bash script.

Thanks,

Last edited by stefaandk; 10-10-2006 at 07:17 PM.
 
Old 10-10-2006, 07:33 PM   #2
cavalier
Member
 
Registered: Feb 2005
Location: Denver, CO
Distribution: Slack 12, tweaked just so (though I'm also a fan of Ubuntu)
Posts: 198
Blog Entries: 1

Rep: Reputation: 30
Right off the top of my head:

Code:
#!/bin/bash

cat file1 > themainfile
sleep 2 m
cat file2 > themainfile
sleep 2 m
cat file3 > themainfile
sleep 2 m
cat file4 > themainfile
sleep 2 m
cat file5 > themainfile
 
Old 10-10-2006, 08:01 PM   #3
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
That seems to work, cool thanks!
 
Old 10-10-2006, 08:16 PM   #4
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
Following up on this though, how do I loop it back to the beginning?

End if possible even randomize the file that is being used to overwrite the main file?

Last edited by stefaandk; 10-10-2006 at 08:19 PM.
 
Old 10-10-2006, 08:25 PM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
try this one

Code:
while :; do
	cat file$(( RANDOM % 5 + 1 )) > themainfile
	sleep 2m
done
 
Old 10-10-2006, 08:39 PM   #6
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
K thanks, my filenames are slightly different to this so I want to make sure that I understand the variables.

file$ the $ is then replaced by a number between 1 and 5 ?

Can I have the random part be more generic.

In my case I have:

file1_blablabla
file2_totallydifferentdiscription
file3_somethingelseagain

So it's not just a 1 and 5. If this can't be done then I can always rename the files to have 1 and 5 in there, just somewhat annoying as the filename doesn't clearly indicate what it is.
 
Old 10-10-2006, 08:44 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
try again

Code:
FILE=(file1_blablabla file2_totallydifferentdiscription file3_somethingelseagain)

FILES=${#FILE[@]}
while :; do
	cat "${FILE[$(( RANDOM % FILES ))]}" > themainfile
	sleep 2m
done

Last edited by konsolebox; 10-10-2006 at 08:53 PM.
 
Old 10-10-2006, 09:01 PM   #8
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
Getting the following error:

./rotate.sh: line 4: bad substitution: no `)' in $(( RANDOM
 
Old 10-10-2006, 09:20 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
sorry my mistake. please recheck the script above.
 
Old 10-11-2006, 07:24 AM   #10
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
Cheers mate, all working fine!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Shell Script Help Kristijan Programming 3 06-13-2005 09:13 PM
Simple shell script mikz Linux - General 1 02-24-2005 07:18 AM
simple shell script sharpie Programming 9 06-02-2004 11:47 PM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 10:14 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration