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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-10-2006, 07:16 PM
|
#1
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Rep:
|
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.
|
|
|
10-10-2006, 07:33 PM
|
#2
|
Member
Registered: Feb 2005
Location: Denver, CO
Distribution: Slack 12, tweaked just so (though I'm also a fan of Ubuntu)
Posts: 198
Rep:
|
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
|
|
|
10-10-2006, 08:01 PM
|
#3
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Original Poster
Rep:
|
That seems to work, cool thanks!
|
|
|
10-10-2006, 08:16 PM
|
#4
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Original Poster
Rep:
|
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.
|
|
|
10-10-2006, 08:25 PM
|
#5
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
try this one
Code:
while :; do
cat file$(( RANDOM % 5 + 1 )) > themainfile
sleep 2m
done
|
|
|
10-10-2006, 08:39 PM
|
#6
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Original Poster
Rep:
|
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.
|
|
|
10-10-2006, 08:44 PM
|
#7
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
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.
|
|
|
10-10-2006, 09:01 PM
|
#8
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Original Poster
Rep:
|
Getting the following error:
./rotate.sh: line 4: bad substitution: no `)' in $(( RANDOM
|
|
|
10-10-2006, 09:20 PM
|
#9
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
sorry my mistake. please recheck the script above.
|
|
|
10-11-2006, 07:24 AM
|
#10
|
Member
Registered: Jun 2005
Distribution: Centos
Posts: 215
Original Poster
Rep:
|
Cheers mate, all working fine!
|
|
|
All times are GMT -5. The time now is 06:08 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|