# Another shellscript question

**URL:** <https://community.unix.com/t/another-shellscript-question/157575>\
**Category:** Shell Programming and Scripting\
**Created:** [November 17, 2005, 3:52pm UTC](https://community.unix.com/t/another-shellscript-question/157575 "2005-11-17T15:52:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 17, 2005, 3:52pm UTC](https://community.unix.com/t/another-shellscript-question/157575/1 "2005-11-17T15:52:56Z")

</div>

Folks;

on a unix server I have a mapping file which holds a list mountpoints of all databases and their mountpoints. tab delimited or colon deliminted..I needed to copy the datafiles from the pristine mountpoints to test's mountpoints in this case. I needed to do this by passing sid name using $SID. How can I perform that?

ex.  
cat mapfile.lst  
test /u01:/u02:/u03:/u04  
dev /u05:/u06:/u07:/u08

Pristine mountpoints  
/u01p:/u02p:/u03p:/u04p

thanks

I came up with this but its not working..I somehow need to do the following.  
a. grep for test then move onto building the cp query. the cp query should look like..  
cp -rp /u01\_p /pristine u01/oradata/test  
cp -rp /u02\_p /pristine u02/oradata/test

for i in `cat dbmap|awk '{for (i=2; i\<=NF; ++i) print $i}'`;  
do  
cp -p -r ${i} $i  
done

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 22, 2005, 9:52pm UTC](https://community.unix.com/t/another-shellscript-question/157575/2 "2005-11-22T21:52:28Z")

</div>

> [@jigarlakhani](#):
>
> Folks;
> 
> on a unix server I have a mapping file which holds a list mountpoints of all databases and their mountpoints. tab delimited or colon deliminted..I needed to copy the datafiles from the pristine mountpoints to test's mountpoints in this case. I needed to do this by passing sid name using $SID. How can I perform that?
> 
> ex.  
> cat mapfile.lst  
> test /u01:/u02:/u03:/u04  
> dev /u05:/u06:/u07:/u08
> 
> Pristine mountpoints  
> /u01p:/u02p:/u03p:/u04p
> 
> thanks
> 
> I came up with this but its not working..I somehow need to do the following.  
> a. grep for test then move onto building the cp query. the cp query should look like..  
> cp -rp /u01\_p /pristine u01/oradata/test  
> cp -rp /u02\_p /pristine u02/oradata/test
> 
> for i in `cat dbmap|awk '{for (i=2; i\<=NF; ++i) print $i}'`;  
> do  
> cp -p -r ${i} $i  
> done

1. what's 'dbmap'?
2. how's the '/u01\_p' derived in your 'cp' sample?
3. given sample file inputs [mapfile.lst and prestine.txt] , what the sample desired output?

Please try to explain the input and the desired output as clearly as you possibly can with the sample data.

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 28, 2005, 3:33pm UTC](https://community.unix.com/t/another-shellscript-question/157575/3 "2005-11-28T15:33:36Z")

</div>

1. dbmap is the mapfile.lst file which holds the following data.  
test /u01:/u02:/u03:/u04  
dev /u05:/u06:/u07:/u08

2. I dont now how to derive the '/u01\_p' mountpoint. There are 8 mountpoints with that for source and target. the source is always static but the target is could be /u01..u08 or /u11..u18 or /u21..u28  
'/u01\_p'  
'/u02\_p'  
...  
'/u08\_p'

the cp example i gave "cp -rp /u01\_p /pristine u01/oradata/test" is what i need to derive from the routine...  
ex.. the desired output should be...

cp -rp /u01\_p /pristine u01/oradata/test  
cp -rp /u02\_p /pristine u02/oradata/test  
....................  
cp -rp /u08\_p /pristine u08/oradata/test

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 28, 2005, 3:47pm UTC](https://community.unix.com/t/another-shellscript-question/157575/4 "2005-11-28T15:47:50Z")

</div>

> [@jigarlakhani](#):
>
> 1. dbmap is the mapfile.lst file which holds the following data.  
> test /u01:/u02:/u03:/u04  
> dev /u05:/u06:/u07:/u08
> 
> 2. I dont now how to derive the '/u01\_p' mountpoint. There are 8 mountpoints with that for source and target. the source is always static but the target is could be /u01..u08 or /u11..u18 or /u21..u28  
> '/u01\_p'  
> '/u02\_p'  
> ...  
> '/u08\_p'
> 
> the cp example i gave "cp -rp /u01\_p /pristine u01/oradata/test" is what i need to derive from the routine...  
> ex.. the desired output should be...
> 
> cp -rp /u01\_p /pristine u01/oradata/test  
> cp -rp /u02\_p /pristine u02/oradata/test  
> ....................  
> cp -rp /u08\_p /pristine u08/oradata/test

given your mapfile....

nawk -f jig.awk mapfile

with jig.awk containing:

```plaintext
{
  n=split($2, arr, ":")
  for(i=1; i <= n; i++)
    printf("cp -rp %s_p /pristine %s/oradata/%s\n", arr, arr, $1)
}

```

produces

```plaintext
cp -rp /u01_p /pristine /u01/oradata/test
cp -rp /u02_p /pristine /u02/oradata/test
cp -rp /u03_p /pristine /u03/oradata/test
cp -rp /u04_p /pristine /u04/oradata/test
cp -rp /u05_p /pristine /u05/oradata/dev
cp -rp /u06_p /pristine /u06/oradata/dev
cp -rp /u07_p /pristine /u07/oradata/dev
cp -rp /u08_p /pristine /u08/oradata/dev

```

Is that something that you're after?

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 28, 2005, 5:10pm UTC](https://community.unix.com/t/another-shellscript-question/157575/5 "2005-11-28T17:10:15Z")

</div>

definately. some questions. How are you catting the mapfile in the code?  
the output you got was the following  
code:  
cp -rp /u01\_p /pristine /u01/oradata/test  
cp -rp /u02\_p /pristine /u02/oradata/test  
cp -rp /u03\_p /pristine /u03/oradata/test  
cp -rp /u04\_p /pristine /u04/oradata/test  
cp -rp /u05\_p /pristine /u05/oradata/dev  
cp -rp /u06\_p /pristine /u06/oradata/dev  
cp -rp /u07\_p /pristine /u07/oradata/dev  
cp -rp /u08\_p /pristine /u08/oradata/dev  
code:  
but what if I just want to build a copy statement for test and omit dev? I somehome wanted to incorporate $SID value which should equal to test.

thanks

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 28, 2005, 5:14pm UTC](https://community.unix.com/t/another-shellscript-question/157575/6 "2005-11-28T17:14:27Z")

</div>

> [@jigarlakhani](#):
>
> definately. some questions. How are you catting the mapfile in the code?  
> the output you got was the following  
> code:  
> cp -rp /u01\_p /pristine /u01/oradata/test  
> cp -rp /u02\_p /pristine /u02/oradata/test  
> cp -rp /u03\_p /pristine /u03/oradata/test  
> cp -rp /u04\_p /pristine /u04/oradata/test  
> cp -rp /u05\_p /pristine /u05/oradata/dev  
> cp -rp /u06\_p /pristine /u06/oradata/dev  
> cp -rp /u07\_p /pristine /u07/oradata/dev  
> cp -rp /u08\_p /pristine /u08/oradata/dev  
> code:  
> but what if I just want to build a copy statement for test and omit dev? I somehome wanted to incorporate $SID value which should equal to test.
> 
> thanks

nawk -v sid="${SID}" -f jig.awk mapfile

```plaintext
sid == $1 {
  n=split($2, arr, ":")
  for(i=1; i <= n; i++)
    printf("cp -rp %s_p /pristine %s/oradata/%s\n", arr, arr, $1)
}

```

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 11:05am UTC](https://community.unix.com/t/another-shellscript-question/157575/7 "2005-11-29T11:05:37Z")

</div>

Thanks..i tried testing the code you gave me...but the output was different. What am I doing wrong? I am not good with awk/shell programming. the other thing is that the output should be like the following..

cp -rp /u09\_p/pristine /u09/oradata/test  
cp -rp /u10\_p/pristine /u10/oradata/test \<---/u10 is not being grabbed

not  
cp -rp /u09\_p/pristine /u09/oradata/test  
cp -rp /u01\_p/pristine /u01/oradata/dev

thanks again,

mapfile  
test:/u09:/u10  
dev:/u01:/u02

awk -v sid="${SID}" -f 3.sh dbmap2

cat 3.sh  
sid == $1  
{  
n=split($2, arr, ":")  
for(i=1; i \<= n; i++)  
printf("cp -rp %s\_p /pristine %s/oradata/%s\n", arr[i], arr[i], $1)  
}

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 29, 2005, 11:08am UTC](https://community.unix.com/t/another-shellscript-question/157575/8 "2005-11-29T11:08:30Z")

</div>

this is NOT the mapfile sample you gave before.  
WHICH one do you need?

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 11:09am UTC](https://community.unix.com/t/another-shellscript-question/157575/9 "2005-11-29T11:09:18Z")

</div>

sorry, I need it for the last 1 I posted?

thanks.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 29, 2005, 11:48am UTC](https://community.unix.com/t/another-shellscript-question/157575/10 "2005-11-29T11:48:48Z")

</div>

nawk -F: -v sid="${SID}" -f jig.awk mapfile

jig.awk:

```nohighlight
sid == $1 {
  for(i=2; i <= n; i++)
    printf("cp -rp %s_p /pristine %s/oradata/%s\n", $i, $i, $1)
}

```

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 12:07pm UTC](https://community.unix.com/t/another-shellscript-question/157575/11 "2005-11-29T12:07:23Z")

</div>

> [@vgersh99](#):
>
> nawk -F: -v sid="${SID}" -f jig.awk mapfile
> 
> jig.awk:
> 
> ```plaintext
> sid == $1 {
> for(i=2; i <= n; i++)
> printf("cp -rp %s_p /pristine %s/oradata/%s\n", $i, $i, $1)
> }
> 
> ```

not getting anything back..  
jbld@tabloo:/home/oracle/scripts/a\> awk -F: -v sid="${SID}" -f jig.awk mapfile  
jbld@tabloo:/home/oracle/scripts/a\>  
jbld@tabloo:/home/oracle/scripts/a\> export SID=test  
jbld@tabloo:/home/oracle/scripts/a\> awk -F: -v sid="${SID}" -f jig.awk mapfile

jbld@tabloo:/home/oracle/scripts/a\> cat mapfile  
test:/u09:/u10  
dev:/u01:/u02

jbld@tabloo:/home/oracle/scripts/a\> cat jig.awk  
sid == $1 {  
for(i=2; i \<= n; i++)  
printf("cp -rp %s\_p /pristine %s/oradata/%s\n", $i, $i, $1)  
}

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 29, 2005, 12:12pm UTC](https://community.unix.com/t/another-shellscript-question/157575/12 "2005-11-29T12:12:30Z")

</div>

I'm sorry - I'm bad - use 'nawk' instead of 'awk':

```nohighlight
sid == $1 {
  for(i=2; i <= NF; i++)
    printf("cp -rp %s_p /pristine %s/oradata/%s\n", $i, $i, $1)
}

```

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 12:15pm UTC](https://community.unix.com/t/another-shellscript-question/157575/13 "2005-11-29T12:15:47Z")

</div>

We dont have nawk in HPUX unless I dont know the path.... We have awk though..pls help....

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 29, 2005, 12:30pm UTC](https://community.unix.com/t/another-shellscript-question/157575/14 "2005-11-29T12:30:14Z")

</div>

> [@jigarlakhani](#):
>
> We dont have nawk in HPUX unless I dont know the path.... We have awk though..pls help....

ok, try it with awk as is.

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 2:17pm UTC](https://community.unix.com/t/another-shellscript-question/157575/15 "2005-11-29T14:17:09Z")

</div>

> [@vgersh99](#):
>
> nawk -F: -v sid="${SID}" -f jig.awk mapfile
> 
> jig.awk:
> 
> ```plaintext
> sid == $1 {
> for(i=2; i <= n; i++)
> printf("cp -rp %s_p /pristine %s/oradata/%s\n", $i, $i, $1)
> }
> 
> ```

Question; how can Incorporate this whole thing in 1 script? assuming the mapfile is an external file.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 29, 2005, 3:12pm UTC](https://community.unix.com/t/another-shellscript-question/157575/16 "2005-11-29T15:12:12Z")

</div>

> [@jigarlakhani](#):
>
> Question; how can Incorporate this whole thing in 1 script? assuming the mapfile is an external file.

you really need to start reading the man pages......

```plaintext
nawk -F: -v sid="${SID}" '
  sid == $1 {
    for(i=2; i <= NF; i++)
      printf("cp -rp %s_p /pristine %s/oradata/%s\n", $i, $i, $1)
  }' mapfile

```

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 29, 2005, 5:37pm UTC](https://community.unix.com/t/another-shellscript-question/157575/17 "2005-11-29T17:37:12Z")

</div>

1 last request pls..I promose to take some scripting classes..I am a dba by profession not a shellscripting expert like you guys are....

```nohighlight
 
> cat newmaptest 

  this is source this is destination 
/s01/oradata/jbld:/u01/oradata/test 
/s02/oradata/jbld:/u02/oradata/test 

```

how can i get the following command  
cp -rp /s01/oradata/jbld /u01/oradata/test  
cp -rp /s02/oradata/jbld /u02/oradata/test

thanks a lot

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/6_449c533f586939b6e5f246632a46421f.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [November 30, 2005, 3:09am UTC](https://community.unix.com/t/another-shellscript-question/157575/18 "2005-11-30T03:09:05Z")

</div>

```nohighlight
> cat newmaptest 
/s01/oradata/jbld:/u01/oradata/test 
/s02/oradata/jbld:/u02/oradata/test 

```

```nohighlight
> sed -e 's/^/cp -rp /g;s/:/ /' newmaptest
cp -rp /s01/oradata/jbld /u01/oradata/test
cp -rp /s02/oradata/jbld /u02/oradata/test

```

---

<div class="post-metadata">

**Author:** ![jigarlakhani](https://community.unix.com/letter_avatar/jigarlakhani/32/6_449c533f586939b6e5f246632a46421f.png) [@jigarlakhani](https://community.unix.com/u/jigarlakhani)\
**Post date:** [November 30, 2005, 9:50am UTC](https://community.unix.com/t/another-shellscript-question/157575/19 "2005-11-30T09:50:02Z")

</div>

Thanks Matrix Madhan and vgersh99. That really helped.

-Jigar
