How to create a simple copy script?

Guys I want to do this:

copy:

/var/router/system1/config/backup/install.put
/var/router/system2/config/backup/install.put
/var/router/system3/config/backup/install.put
/var/router/system4/config/backup/install.put

into:

/var/router/system1/config/install.dat
/var/router/system2/config/install.dat
/var/router/system3/config/install.dat
/var/router/system4/config/install.dat

in this way, install.put becomes install.dat in new directory.

What is the best way to accomplish?

Your "infile" should contain the above mentioned 4 lines under copy..

$ awk -F. '{print "cp "$0" "$1".dat"}' infile | sh

Thanks, could you explain a bit more of your code?

For instance, it should look like this:

$ awk -F. '{print "cp "$0" "$1".dat"}' /var/router/system1/config/backup/install.put | sh
$ awk -F. '{print "cp "$0" "$1".dat"}' /var/router/system2/config/backup/install.put | sh
$ awk -F. '{print "cp "$0" "$1".dat"}' /var/router/system3/config/backup/install.put | sh
$ awk -F. '{print "cp "$0" "$1".dat"}' /var/router/system4/config/backup/install.put | sh

But then where am i defining the output:

/var/router/system1/config/install.dat

..?

Thanks

put that 4 lines in a file named "infile"

$ cat infile
/var/router/system1/config/backup/install.put
/var/router/system2/config/backup/install.put
/var/router/system3/config/backup/install.put
/var/router/system4/config/backup/install.put

Then run the code which i have given ..

$ awk -F. '{print "cp "$0" "$1".dat"}' infile | sh

Thanks, so the infile and the command should run from the same directory?

Yes ..

Can the infile and command run from:

/var/router/

or does it have to run each time from:

/var/router/system1
/var/router/system2

You can create "infile" under /var/router/ and run the code in the same path ..

When i run this command, i get something like this:


Enterprise:~system$ awk -F. '{print "cp "$0" "$1".dat"}' infile | sh
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

It did not work... anything missing?

Thanks

Try leaving off the "| sh" and posting what it actually prints.

it did this below, without performing the copy function:

user:~ user$ awk -F. '{print "cp "$0" "$1".dat"}' infile
cp  .dat
cp  .dat
cp /Users/config/backup/install.deploy /Users/config/backup/install.dat
cp /Users/config2/backup/install.deploy /Users/config2/backup/install.dat
cp /Users/config3/backup/install.deploy /Users/config3/backup/install.dat
Enterprise:~ user$ 

you have 2 empty lines at the beginning of the infile file.

awk -F. 'NF{print "cp "$0" "$1".dat"}' infile

Still the copy function does not work.

Any ideas?

What actually happens? Any error messages? Same as before?
Once again, try leaving off the trailing '|sh' and post what's getting printed.

It ran without errors, but the main functionality isn't there, which is to copy from one place to another recursively:

Enterprise:~ user$ awk -F. 'NF{print "cp "$0" "$1".dat"}' infile
cp /Users/config/backup/install.deploy /Users/config/backup/install.dat
cp /Users/config2/backup/install.deploy /Users/config/backup/install.dat
cp /Users/config3/backup/install.deploy /Users/config/backup/install.dat

ok, what happens when you put the trailing '|sh' back in?

The copy function is still not there.

Enterprise:~ user$ awk -F. 'NF{print "cp "$0" "$1".dat"}' infile | sh
Enterprise:~ user$ 

what do you expect? There should be no output - the files get copied 'silently' (unless there're errors).
Do you see your new .dat files?

The copy function did not happen.

The contents of install.deploy did not copy over to install.dat

The main idea is to take the contents of install.deploy and put them into install.dat, recursively for each directory listed in infile file.

This is what its doing:

its creating a brand-new install.dat inside the backup directory. which is not what i wanted.

My original request was:

Copy the contents of:

/var/router/system1/config/backup/install.put
/var/router/system2/config/backup/install.put
/var/router/system3/config/backup/install.put
/var/router/system4/config/backup/install.put

Over Write :

/var/router/system1/config/install.dat
/var/router/system2/config/install.dat
/var/router/system3/config/install.dat
/var/router/system4/config/install.dat

in this way, install.put becomes install.dat in config directory.

This is a bit different from the original.
Try this:

nawk -F'[/.]' 'NF{print "cp " $0 " " $0 "/../"$(NF-1)".dat"}' myFile

Add a trailing '|sh' when satisfied with the output.