LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need a simple shell script please overwritting files. (https://www.linuxquestions.org/questions/programming-9/need-a-simple-shell-script-please-overwritting-files-491261/)

stefaandk 10-10-2006 07:16 PM

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,

cavalier 10-10-2006 07:33 PM

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


stefaandk 10-10-2006 08:01 PM

That seems to work, cool thanks!

stefaandk 10-10-2006 08:16 PM

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?

konsolebox 10-10-2006 08:25 PM

try this one

Code:

while :; do
        cat file$(( RANDOM % 5 + 1 )) > themainfile
        sleep 2m
done


stefaandk 10-10-2006 08:39 PM

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.

konsolebox 10-10-2006 08:44 PM

try again

Code:

FILE=(file1_blablabla file2_totallydifferentdiscription file3_somethingelseagain)

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


stefaandk 10-10-2006 09:01 PM

Getting the following error:

./rotate.sh: line 4: bad substitution: no `)' in $(( RANDOM

konsolebox 10-10-2006 09:20 PM

sorry my mistake. please recheck the script above.

stefaandk 10-11-2006 07:24 AM

Cheers mate, all working fine!


All times are GMT -5. The time now is 02:28 AM.