Operating System - HP-UX
1753901 Members
9324 Online
108810 Solutions
New Discussion юеВ

Re: New script (to copy files)

 
Intothenewworld
Advisor

New script

I would like to have a new script to perform the below task .


I have a file called dummy.txt , the content of this file is as below .

aaa.prn
bbb.prn
ccc.prn

I would like to have a script which can do

1) check dummy.txt
2) then copy these files ( but with other extension .xls ) from a specify directory to another server
   eg. scp the files /tmp/aaa.xls , /tmp/bbb.xls , /tmp/ccc.xls to /tmp of another server


can advise what can i do ?

thanks in advance.

3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: New script (to copy files)

You could do something like:

for file in $(< dummy.txt); do

   base=${file%.prn}   # strip suffix

   scp /tmp/$base.xls remote:/tmp

done

Intothenewworld
Advisor

Re: New script (to copy files)

it works , but may I have two additional requirement .

1) actually , the file name in dummy.txt have two type of prefix , one is xxx ( eg. xxxfile.prn ) , another one is yyy ( yyyfile.prn ) , if the file is xxx , then copy to /tmp of  another server , if the file is yyy then copy to /tmp1 of another server , can advise what can i do ?

2) scp works fine to copy to remote server , can advise how to check and display if the scp is successful on the screen or not ?

thx

Dennis Handly
Acclaimed Contributor

Re: New script (to copy files)

>1) the filename in dummy.txt have two type of prefix, one is xxx (eg. xxxfile.prn), another one is yyy (yyyfile.prn), if the file is xxx , then copy to /tmp of another server, if the file is yyy then copy to /tmp1 of another server?

>2) can advise how to check and display if the scp is successful on the screen or not?

 

Did you want to remove that prefix?  I'm not doing it here.

 

for file in $(< dummy.txt); do

   base=${file%.prn}   # strip suffix

   if [[ $base = xxx* ]]; then

      server=server1

      tmp=/tmp

   elif [[ $base = yyy* ]]; then

      server=server2

      tmp=/tmp1

   else

      echo "Not sure where to send $file" 1>&2

      continue

   fi

   scp /tmp/$base.xls $server:$tmp

   if [ $? -ne 0 ]; then

      echo "scp failed to copy $file" 1>&2

   fi

done