LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-15-2005, 06:26 AM   #1
zamri
Member
 
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157

Rep: Reputation: 30
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.
 
Old 07-15-2005, 06:49 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
Old 07-15-2005, 06:53 AM   #3
jwolter0
LQ Newbie
 
Registered: Apr 2004
Location: Cleveland, OH
Posts: 11

Rep: Reputation: 0
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.
 
Old 07-15-2005, 06:57 AM   #4
Ulisses
LQ Newbie
 
Registered: Aug 2004
Location: Recife/PE, Brasil
Distribution: Kubuntu Breezy
Posts: 13

Rep: Reputation: 0
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.
 
Old 07-16-2005, 07:03 AM   #5
zamri
Member
 
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157

Original Poster
Rep: Reputation: 30
yup that helps. thanks guys. I try my best to test it.
 
Old 07-16-2005, 10:22 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 07-17-2005, 09:31 AM   #7
zamri
Member
 
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157

Original Poster
Rep: Reputation: 30
thanks eddie. yours is more efficient.
 
Old 07-17-2005, 04:16 PM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 09-19-2005, 08:53 AM   #9
zamri
Member
 
Registered: May 2004
Location: Malaysia
Distribution: Mandrake,Slackware,RedHat
Posts: 157

Original Poster
Rep: Reputation: 30
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.
 
Old 09-20-2005, 02:05 AM   #10
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
Old 09-21-2005, 12:07 AM   #11
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Rep: Reputation: 16
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.
 
Old 09-21-2005, 01:52 AM   #12
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Rep: Reputation: 15
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`
...?
 
Old 04-25-2008, 11:46 AM   #13
dpoper1
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
Smile 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
 
Old 04-25-2008, 12:45 PM   #14
Ulisses
LQ Newbie
 
Registered: Aug 2004
Location: Recife/PE, Brasil
Distribution: Kubuntu Breezy
Posts: 13

Rep: Reputation: 0
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
 
Old 04-29-2008, 10:27 AM   #15
dpoper1
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH Shell Scripting -- foreach file in folder??? AC97Conquerer Programming 11 07-06-2011 12:25 AM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
Bash script to do copying and ISO image creating, etc. satimis Programming 2 09-19-2004 09:47 PM
Error Copying File or Folder nysethe Linux - Networking 3 08-17-2004 01:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration