[Solved] Simple script not working- for loop

Hi all,
Please guide me writing this script
Follwing is the file which I have created, which contains the files to be copied.

cat system1-dr.txt
/etc/passwd
/etc/shadow
/etc/group
/etc/vfstab
/etc/profile
/etc/default/init
/etc/shells
/etc/dfs/dfstab
/etc/dfs/sharetab
/etc/default/password

This is the small script I have written for this task , but when I execute it instead of files defined in system1-dr.txt getting copied system1-dr.txt file get copied.
below is the script

cat script1
for i in /users/jack/system1-dr.txt
do
sudo cp $i /users/jack/DRbep01
done
cd  /users/jack/DRbep01
tar-cvf system1.tar *
scp system1.tar backupserver:/users/jack/stsyem1

Could you please guide me in this and how more sophiscated script can I make it..?

Use while loop instead...

 
while read i
do
sudo cp $i /users/jack/DRbep01
done</users/jack/system1-dr.txt

Sorry,
Can't understand any thing from your reply.
can you explain same with my script.

Thanks,
Manali

Your script should be like this..

 
$cat script1
 
while read i
do
sudo cp $i /users/jack/DRbep01
done</users/jack/system1-dr.txt
cd  /users/jack/DRbep01
tar-cvf system1.tar *
scp system1.tar backupserver:/users/jack/stsyem1
1 Like

replace

for i in /users/jack/system1-dr.txt

with

for i in `cat /users/jack/system1-dr.txt`

the code between ` ` will be executed and become arguments for "for loop"

1 Like

Though the previous post is absolutely correct, we here at unix.com are more in favor of pamu's solution for it avoids the use of a not necessary cat command

1 Like

HI pamu,

Thanks, your while loop works.
But could you all please tell two things

How to put command in this while loop or script which will
-rename /etc/default/passwd to passwd-default as there are two file named passwd on system and other is under /etc/passwd

-secondly this /etc/shadow file is having r-- --- --- permission so first I need to change its permission to say 755 and then copy to /users/jack/DRbep01

It will be great help to me to know this, I am beginner in scripting,I will get idea by your suggestions.

Please advise
Thanks,
Manali

Are you only trying to make an archive to save?

This is how to make your life more complicated and less reliable...
You did not answer my previous question...

Since you are silent and after I will not have the time to explain anymore so look at the following command I just typed for you and meditate: It keeps the perms, timestamps etc....

ant:/home/vbe/admin/shell/tests $ ll
total 30
-rw-rw-r--   1 vbe        bin            137 Jan 30 17:17 infile
-rw-rw-r--   1 vbe        bin          14336 Jan 30 17:20 titi
ant:/home/vbe/admin/shell/tests $ more infile         
./etc/passwd
./etc/shadow
./etc/group
./etc/vfstab
./etc/profile
./etc/default/cron
./etc/default/fs
./etc/shells
./etc/default/security
ant:/home/vbe/admin/shell/tests $ cd /
ant:/ $ cpio -ocv </home/vbe/admin/shell/tests/infile >/home/vbe/admin/shell/tests/titi
etc/passwd
etc/shadow
etc/group
"etc/vfstab": No such file or directory
Cannot stat <etc/vfstab>.
etc/profile
etc/default/cron
etc/default/fs
etc/shells
etc/default/security
28 blocks
ant:/ $ cd ~/admin/shell/tests
ant:/home/vbe/admin/shell/tests $ ll
total 30
-rw-rw-r--   1 vbe        bin            137 Jan 30 17:17 infile
-rw-rw-r--   1 vbe        bin          14336 Jan 30 17:31 titi
ant:/home/vbe/admin/shell/tests $ cpio -cit <titi                                        
etc/passwd
etc/shadow
etc/group
etc/profile
etc/default/cron
etc/default/fs
etc/shells
etc/default/security
28 blocks
ant:/home/vbe/admin/shell/tests $ cpio -icdmv < titi                                     
etc/passwd
etc/shadow
etc/group
etc/profile
etc/default/cron
etc/default/fs
etc/shells
etc/default/security
28 blocks
ant:/home/vbe/admin/shell/tests $ ll
total 32
drwxr-xr-x   3 root       sys           1024 Jan 30 17:33 etc
-rw-rw-r--   1 vbe        bin            137 Jan 30 17:17 infile
-rw-rw-r--   1 vbe        bin          14336 Jan 30 17:31 titi
ant:/home/vbe/admin/shell/tests $ cd etc
ant:/home/vbe/admin/shell/tests/etc $ ll
total 28
drwxr-xr-x   2 root       root            96 Jan 30 17:33 default
-r--rw-r--   1 bin        bin           4509 Jul  8  2011 group
-r--r--r--   1 root       sys           2775 Jun 23  2011 passwd
-r--r--r--   1 bin        bin           2871 Jun  2  2009 profile
-r--------   1 root       sys           1585 Jun 23  2011 shadow
-rw-rw-r--   1 root       bin            159 Sep  2  2008 shells
ant:/home/vbe/admin/shell/tests/etc $ cd default
ant:/home/vbe/admin/shell/tests/etc/default $ ll
total 8
-r--r--r--   1 root       root          1120 Aug 24  2006 cron
-r--r--r--   1 bin        bin             11 Nov 15  2000 fs
-rw-r-----   1 root       bin            232 May  7  2010 security
ant:/home/vbe/admin/shell/tests/etc/default $ 

the commands were passed with a sudo root...
As you can see I created an archive named titi using a file called infile...

Its a HP-UX 11.11 box, using ksh...

HI Vbe,

what is this all I hardly understand anything..

---------- Post updated 01-31-13 at 01:56 AM ---------- Previous update was 01-30-13 at 11:46 PM ----------

how to tar shadow file which has r-- --- --- permission ?
It shows permission denied as below..

a dfstab 1K
a group 1K
a init 1K
a passwd 4K
a profile 2K
tar: shadow: Permission denied
a sharetab 0K
a shells 1K
a vfstab 2K

---------- Post updated at 02:26 AM ---------- Previous update was at 01:56 AM ----------

Hey it is simple:

I wrote two lines before while loop for shadow file;

sudo cp /etc/shadow /users/jack/shadow
sudo chmod  755  /users/jack/shadow

it worked well.

But why it shows this don't know but copy works well.

cp: Insufficient arguments (1)
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn

Well if you looked at what I did:
1) Preamble you have to be root somehow ( I used sudo...)
2) To not mix the files, you keep the file structure ( tree...)
3) I did all you wanted with one command : cpio
a - It can create an archive: titi
b - Using correct options it keeps all you need to be a true archive: ownership, perms, timestamps...
c - you can see the content
d - you can restore its content
So start by reading the man pages of cpio and look carefully what I produced

UNIX has to be seen as a great toolbox, with so many tools... why try to invent new ones when one existing does the job?

--- Addendum---
Thread closed for the initial question/issues has been solved, and the topic changed...