Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
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.
|
 |
02-05-2002, 06:03 AM
|
#1
|
Member
Registered: Jan 2002
Posts: 54
Rep:
|
copy 1 file to multiple dir script
Hello, i have a server that has hundreds of domains that are use for email, the actual sites are just used to point users to main site and act as search engine boosters.
Obviously i do not want to changed each file one by one, and though i am new to linux i know that i can create a shell script to copy the new index.html to all the individual /home/domain/public_html/ directories.
Can any one help me out with how best to acheive this?
many thanks
|
|
|
02-05-2002, 06:42 AM
|
#2
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
for dirname in domain1 domain2 domain3
do
cp index.html /home/${dirname}/public_html
done
Or you could do something like
for dirname in `ls /home/domain*`
do
cp index.html /home/${dirname}/public_html
done
Last edited by Mik; 02-05-2002 at 06:47 AM.
|
|
|
02-05-2002, 06:16 PM
|
#3
|
Member
Registered: Jan 2002
Posts: 54
Original Poster
Rep:
|
thanks for reply, this looks good if i want to changed index.html file in all
/home/domain/public_html/
which i don't, it need it to be selective
somthing like
cp index.html /home/${domain1,domain2,domain,7,domain15}/public_html
done
|
|
|
02-05-2002, 06:34 PM
|
#4
|
Member
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184
Rep:
|
Just as Mik said:
for dirname in domain1 domain2 domain15
do
cp index.html /home/${dirname}/public_html/
done
|
|
|
02-06-2002, 12:46 PM
|
#5
|
Member
Registered: Jan 2002
Posts: 54
Original Poster
Rep:
|
eithir i am slow to undestand or you do not understand what i want to do.
The syntax given seems to be to copy new index.html to one domain at a time or all domains at same time.
I want to automatically copy new index.html file to a list of selected domains, not all my domains.
This why i asked how to incluede list of domains 'user name' dir
In below how would i insert a list of domain user dir, where
{list} is where the list would go.
cp index.html /home/${list}/public_html
done
|
|
|
02-06-2002, 12:53 PM
|
#6
|
Member
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184
Rep:
|
An another way:
it's
for domain in domainx domainxy domaintotallydifferent
do
cp index.html /home/${domain}/public_html
done
Note that its totally up to you which domain-names you choose in your "in ..." List.
you might also do something like
for domain in domain1 steave domain17 www
do ...
to have the index.html copied to the public_html dir of the "users"
domain1, steave, domain17 and www
Steave.
Last edited by Steave; 02-06-2002 at 12:54 PM.
|
|
|
02-06-2002, 02:31 PM
|
#7
|
Member
Registered: Jan 2002
Posts: 54
Original Poster
Rep:
|
Hello, thanks for help but i am really confused now
Ok i have 10 domains and i wish to change the index.html in 5 of them. The dir structure is like this
/home/domain1/public_html/
/home/domain2/public_html/
/home/domain3/public_html/
/home/domain4/public_html/
/home/domain5/public_html/
/home/domain6/public_html/
/home/domain7/public_html/
etc
i create a shell script with syntax
cp index.html /home/${domain1 domain2 domain3 domain4 domain5 }/public_html
done
would this work ?
thanks for help
|
|
|
02-06-2002, 03:13 PM
|
#8
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
what kind of syntax do you mean?
what you said, "cp index.html /home/${domain1 domain2 domain3 domain4 domain5 }/public_html"
is completely wrong (no offense!), bash can't do that, and specifying a program to run like that would not work, as you'd be using 'cd' not your own script by the way you wrote that, you have been given pretty much the same solutio tweice now, whcih does exactly what you are after. what else do you require that their solutions don't provide?
personally i'd use a shift call to move the script parameters along, and call $2 continuously in a for loop, but there's loads of ways of doing the same thing.
you seem to be being somewhat dogged in this illegal syntax you seem to be after. I'd suggest reading up on shell scripting. look at some of the books on the link on my signature
|
|
|
02-06-2002, 06:17 PM
|
#9
|
Member
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184
Rep:
|
OK. Probably some explanation here:
for dirname in astring anotherstring somestring
do
- code -
done
lets $dirname be (in the order stated) all of the strings in the list followin the "in"
so inside the do-done segment, you can use $dirname to -in your case copy- stuff to the desired directories.
Got it now?
|
|
|
02-07-2002, 04:36 AM
|
#10
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
Very very confusing. To confuse you even more try this:
export list="domain1 domain2 domain5"
for dirname in ${list}
do
cp index.html /home/${dirname}/public_html
done
Still the same thing but maybe it makes it clearer for you.
|
|
|
02-07-2002, 04:45 AM
|
#11
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
filename=$1
shift
for dirname in $*
do
cp ${filename}/home/${dirname}/public_html
done
removes the need to set a variable. just state all names as parameters
|
|
|
02-07-2002, 06:12 AM
|
#12
|
Member
Registered: Jan 2002
Posts: 54
Original Poster
Rep:
|
Ok i think you lot are winding me up
let me see if i have got it,
if i create a file called copy-index.txt with the following syntax in it and set permission to execute it will do the required task.
syntax in file copy-index.txt =
filename=$index.html
shift
for dirname in $domain1 domain 2 domain3
do
cp ${filename}/home/${dirname}/public_html
done
many thanks guys
|
|
|
02-07-2002, 06:43 AM
|
#13
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
nah, you see now that's a general jumble of al the different solutions... you really need to read up on shell scripting. the way i've done it here is a little more subtle i think than most other solutions, and is more flexible. i'll let you fine out exactly what shift and $* actaully mean.. but this DOES work...
[chris@trevor chris]$ cat script
filename=$1
shift
for dirname in $*
do
echo cp $filename /home/$dirname/public_html
done
[chris@trevor chris]$ ./script a b c d e
cp a /home/b/public_html
cp a /home/c/public_html
cp a /home/d/public_html
cp a /home/e/public_html
yeah? obviously, just remove the word 'echo' and it'll go copy the stuff, rather than just pritning out the command like i've done there.
and why do you want to call it blah.txt? it's not a text file, it's a script file. generally you wouldn't ever give script files a suffix at all.
Last edited by acid_kewpie; 02-07-2002 at 06:44 AM.
|
|
|
02-07-2002, 07:06 AM
|
#14
|
Member
Registered: Jan 2002
Posts: 54
Original Poster
Rep:
|
thanks i will give it a try after i do a little more reading on subject.
|
|
|
02-07-2002, 09:29 AM
|
#15
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
what your not understanding is this
the word for sets a variable
for variable in something anything somethingelse
now all of the words values to the right of in will be assigned to $variable one at a time in the loop.
since there are three words the loop will run three times.
1st run
do
echo $variable
is the same as
echo something
done
2nd run
do
echo $variable
is the same as
echo anything
done
3rd run
do
echo $variable
is the same as
echo somethingelse
done
that being said, the shift is better
Last edited by DavidPhillips; 02-08-2002 at 04:56 AM.
|
|
|
All times are GMT -5. The time now is 04:11 PM.
|
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
|
|