BASH Shell script : copying a file to multiple folder
ProgrammingThis 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.
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.
BASH Shell script : copying a file to multiple folder
Hi all,
Since i'm learning BASH shell script programming, I would like to as all a few questions.
1. I would like to copy a file to multiple folder/dir. let's say I have 1000 accounts under /home and I want to copy a file (say .config) to every folder.
2. How am i going to test whether a user is exist in my system? against /etc/passwd ? could someone give an example script?
Bash is powerful by itself, but most of the times shell scripts must on external tools, especially for string manipulation tasks. As for your particular questions:
1. I can assume here that you will be receiving a single filename, and one or more directory names, where this file should be copied. You could try something like:
Code:
#!/bin/bash
filename=$1
path=$2
shift 2
while [ ! -z "${path}" ] ; do
cp ${filename} ${path}
path=$1
shift
done
We first set the filename and first path values, and shift the argument list by 2 places. At this point, unless the user only provided a single argument, both ${filename} and ${path} contain string values, and thus the 'while' condition will be true (that is, ${path} will not be an empty string). So we copy ${filename} into ${path}, and reset it to the next command line argument, again shifting the argument list, this time only by one place, repeating the copy while ${path} still contains something (that is, while it is not an empty string).
This script is simple enough, but still could add some error-checking, such as verifying that ${filename} is really a file, or that ${path} is a valid directory, or that we have read permissions on ${file}, and write permissions on ${path} -- that is left as an exercise, but 'help test' should help you.
2. Checking for the presence of a user an be done either by directly checking /etc/passwd (which will work with local users, but won't help with machines with LDAP/NIS/Kerberos authentication), or by directly querying the user database through any of the standard tools, like 'finger' or 'id'. finger has been a major security problem in the past, but if you are running it on the same machine (i.e., you are not gathering info from one machine to another one), it should present no problem. So, if you run it providing a valid user:
Code:
urma@ferrari:/tmp$ finger urma
Login: urma Name: Ulisses Montenegro
Directory: /home/urma Shell: /bin/bash
On since Fri Jul 15 08:19 (BRT) on :0 (messages off)
On since Fri Jul 15 08:19 (BRT) on :0 (messages off)
New mail received Tue Jul 12 07:53 2005 (BRT)
Unread since Mon Jul 11 17:23 2005 (BRT)
No Plan.
With an invalid user:
Code:
urma@ferrari:/tmp$ finger amru
finger: amru: no such user.
So, in order to check for a valid user, we just need to look for either a string that is only found on valid users' output, on a string that is only found on invalid users' output. If we go with the former:
Code:
finger root 2>&1 | grep -c 'Login: '
This will return 1 if the user is valid, or 0 if it is invalid. You could easily use this code fragment in your scripts with something like:
Code:
validuser=$(finger ${username} 2>&1 | grep -c 'Login: ')
if [ ${validuser} -ne 1 ] ; then
echo "Invalid user ${username}"
else
# proceed with valid user code
fi
A tip with pipelines when you're searching for lines with specific values is to try to use the early processes in the pipleline to do the maximum line reduction, so in jwolter0's example, the grep of /etc/passwd should be done first so the cut only operates on matching lines, rather than cutting all the passwd lines and then discarding all but the matching ones.
I have completed my script long ago and want to share with u all here.
Scenario 1 :
Copying one file to multiple dir under home folder.
#!/bin/bash
for i in `ls /home`
do
cp myfile.config /home/$i/
done
exit 0
Scenario 2:
Listing down the details of the file under all dir
#!/bin/bash
for i in `ls /home`
do
ls -al /home/$i/myfile.config
done
exit 0
Thanks all again who helped me.
hi there,
thank you very much zamri.
that is how it should be. you find the solution with some help than you share with the others.
this is really nice: posting solutions .
regards,
slackie1000
assistance in writing an script that will check content in directories
Hi all,
I am trying to write an script which main purpose would be to check for mounted directories under the /mnt fs. In other words there are about 24 mount points and the script should cd to every single mount point that starts with /mnt/data(1-24)/primary and if the directory primary appears it would come back with a result like dir2 mounted successfully.
The directory names can be easily generated automatically, as well as the tests:
Code:
#!/bin/bash
for id in $(seq 1 24) ; do
directory="/mnt/data${id}/primary"
if [ -d ${directory} ] ; then
echo "${directory} is mounted"
else
echo "${directory} does not exist"
fi
done
Thank you Ulises it was very helpful, I have a quick question for you, I am trying to concatenate 2 files, the first file the contains the ip addresses of servers and the other one the path to the, I want to check if the folder exists, so far I have:
cat tmp4 | while read line
do
cat tmp7 | while read line1;
do
ssh -2 root@$line cd ${line1};
if [ -d ${line1} ] ; then
echo "${line1} is mounted"
else
echo "${line1} does not exist"
fi
done
done
tmp4 (list of ip addresses), tmp7 list of paths for each ip address, it is supposed to be
ssh userid@10.11.0.5 (execute command) and/or test if folder exists.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.