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.
|
|
07-15-2005, 06:26 AM
|
#1
|
Member
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157
Rep:
|
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?
Thanks in advance.
|
|
|
07-15-2005, 06:49 AM
|
#2
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
normally you should post what you tried so far...
i give you a direction about point one..
Code:
#!/bin/sh
for i in `ls /home`
do
echo $i
done
exit 0
see what happens? now i think you can figure the "copy" part by yourself..
regards
slackie1000
|
|
|
07-15-2005, 06:53 AM
|
#3
|
LQ Newbie
Registered: Apr 2004
Location: Cleveland, OH
Posts: 11
Rep:
|
For #2, try something like:
if [ ! "`cat /etc/passwd | cut -d: -f1 | grep $usrnam`" = "" ] ; then
echo found
fi
Of course this only tests to see if the user has an /etc/passwd entry. The account could be locked and it would still have an entry.
|
|
|
07-15-2005, 06:57 AM
|
#4
|
LQ Newbie
Registered: Aug 2004
Location: Recife/PE, Brasil
Distribution: Kubuntu Breezy
Posts: 13
Rep:
|
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
Hope that helps with your issues.
|
|
|
07-16-2005, 07:03 AM
|
#5
|
Member
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157
Original Poster
Rep:
|
yup that helps. thanks guys. I try my best to test it.
|
|
|
07-16-2005, 10:22 AM
|
#6
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Quote:
Originally posted by jwolter0
For #2, try something like:
if [ ! "`cat /etc/passwd | cut -d: -f1 | grep $usrnam`" = "" ] ; then
echo found
fi
Of course this only tests to see if the user has an /etc/passwd entry. The account could be locked and it would still have an entry.
|
Why are people such process hogs? I'm all for making pipelines look impressive but
Code:
grep "^$usrnam:" /etc/passwd >/dev/null && echo found
does the business in a single command.
|
|
|
07-17-2005, 09:31 AM
|
#7
|
Member
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157
Original Poster
Rep:
|
thanks eddie. yours is more efficient.
|
|
|
07-17-2005, 04:16 PM
|
#8
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Thanks for the thanks, glad to help.
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.
|
|
|
09-19-2005, 08:53 AM
|
#9
|
Member
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157
Original Poster
Rep:
|
Hi all (again),
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.
|
|
|
09-20-2005, 02:05 AM
|
#10
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
Quote:
Originally posted by zamri
Hi all (again),
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
|
|
|
09-21-2005, 12:07 AM
|
#11
|
Member
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148
Rep:
|
I am working on a script which i will turn into a gui based file. what I got is:
#!/bin/sh
myvar=""
echo Enter file name:
read myvar
lame -h $myvar.wav $myvar.mp3
exit 0
What I would like to know is. Is there a way to do a search and store the file names in an array based on the file type?
I looked at grep but it doesnt seem to be able to destinguish the file from that type. Are there and commands I should read up on.
|
|
|
09-21-2005, 01:52 AM
|
#12
|
Member
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85
Rep:
|
Here's another one for finding if users exist, although the command as it is below still prints "user doesn't exist" if it doesn't exist.
Code:
id -u $username > /dev/null && echo found
As far as blufire's question goes... what about :
Code:
wavfiles=`ls *.wav`
...?
|
|
|
04-25-2008, 11:46 AM
|
#13
|
LQ Newbie
Registered: Mar 2008
Posts: 3
Rep:
|
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.
So far what I have is:
#!/bin/bash
d1="/mnt/data1/primary"
d1a="/mnt/data1/primary"
d2="/mnt/data2/primary"
d3="/mnt/data3/primary"
#d4="/mnt/data4/primary"
#d5="/mnt/data5/primary"
#d6="/mnt/data6/primary"
#d7="/mnt/data7/primary"
#d8="/mnt/data8/primary"
#d9="/mnt/data9/primary"
#d10="/mnt/data10/primary"
#d11="/mnt/data11/primary"
#d12="/mnt/data12/primary"
#d13="/mnt/data13/primary"
#d14="/mnt/data14/primary"
#d15="/mnt/data15/primary"
#d16="/mnt/data16/primary"
#d17="/mnt/data17/primary"
#d18="/mnt/data18/primary"
#d19="/mnt/data19/primary"
#d20="/mnt/data20/primary"
#d21="/mnt/data21/primary"
#d22="/mnt/data22/primary"
#d23="/mnt/data23/primary"
#d24="/mnt/data24/primary"
# bash check if directory exists
#if [ -d $directory ]; then
if [ -d $d1-$d3 ]; then
echo "$d1-$d24 is mounted";
else
echo "directory does not exists"
---------------------------------------------------------
I am new to bash scripting and I would appreciate your assistance.
Regards,
Michael
|
|
|
04-25-2008, 12:45 PM
|
#14
|
LQ Newbie
Registered: Aug 2004
Location: Recife/PE, Brasil
Distribution: Kubuntu Breezy
Posts: 13
Rep:
|
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
|
|
|
04-29-2008, 10:27 AM
|
#15
|
LQ Newbie
Registered: Mar 2008
Posts: 3
Rep:
|
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.
I would appreciate your help.
regards,
MIKE
|
|
|
All times are GMT -5. The time now is 09:46 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
|
|