Operating System - HP-UX
1825139 Members
4922 Online
109679 Solutions
New Discussion юеВ

Failed to copy file when give file name with complete path

 
Khalid Shakoor
Regular Advisor

Failed to copy file when give file name with complete path

Hi Guruz,

I have a small script which can create the backup file with date and time stamps. Just execute the script and give the file name its create the directory name VersnCntrl and copy the file into that directory with date and time on same location.

Now I have one issue, if I run the script and just pass the file name its work fine for me. But when I give the complete path of same file like /home/user/filename it├в s give me error. I don├в t know what is I am missing.I really appreciated if someone help me.

I am attaching the log and scripts for more details

Thanks
Khalid
7 REPLIES 7
Torsten.
Acclaimed Contributor

Re: Failed to copy file when give file name with complete path

Look at the message:

cp: cannot create regular file `/home/tesysop/VersnCntrl//home/tesysop/schange.sh_20101201_101800': No such file or directory


Do you see the "//" (double slash)???


The script sets the path and do it too ...


/home/tesysop/VersnCntrl//home/tesysop



Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Torsten.
Acclaimed Contributor

Re: Failed to copy file when give file name with complete path

Sorry, there is a problem with my keyboard ...


The script sets the path and you did it again

/home/tesysop/VersnCntrl//home/tesysop



The part /home/tesysop appears now twice.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Khalid Shakoor
Regular Advisor

Re: Failed to copy file when give file name with complete path

Thanks Torsten for your prompt response.

I want to use both option,with name and complete path.what are the possibilities.

yes i notice that double path but i don,t know how tu handle this as l am not tu good in scripting :-(

Thanks
Khalid
Torsten.
Acclaimed Contributor

Re: Failed to copy file when give file name with complete path

Your script basically does a copy and a change mode (-x).


Options are
- modify your script to accept the full path
- don't use the script, do it manually
- don't use the full path

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Torsten.
Acclaimed Contributor

Re: Failed to copy file when give file name with complete path

Finally I think there is likely a reason why the script does what it does - doing something else may break things anywhere.

So don't do anything and use the script as intended (without full path).

Ask the author!

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Matti_Kurkela
Honored Contributor

Re: Failed to copy file when give file name with complete path

If you wish to modify the script to accept full pathnames, you might need the commands "basename" and "dirname".

The command "basename" takes a pathname as input and gives only the last component of the pathname as output. The pathname is not required to actually exist.

Examples:

$ basename /home/tesysop/schange.sh
schange.sh

$ basename /home/tesysop
tesysop

$ basename schange.sh
schange.sh

The command "dirname" takes a pathname as input and gives only the directory path part as output. If the input does not contain a directory path, the command outputs "." (=current directory). As with "basename", the input pathname is not required to actually exist.

Examples:

$ dirname /home/tesysop/schange.sh
/home/tesysop

$ dirname /home/tesysop
/home

$ dirname schange.sh
.

In the script, the first argument is simply stored in the variable "ipfilename":

ipfilename=$1

Instead, you could split it into components like this:

ipfile=$(basename $1)
ipdir=$(dirname $1)

Now, if you want to use the original pathname (as a source for a copy operation, or for testing its existence), you can replace "$ipfilename" with "$ipdir/$ipfile".

If $1 is a simple filename, for example "schange.sh", then "$ipdir/$ipfile" becomes "./schange.sh" when variables are expanded... and that's a valid pathname to a file located in the current directory, so it works.

On the parts of the script where you need just the filename (e.g. when you're using it to refer to the destination file), you can replace "$ipfilename" with just "$ipfile".

This would cause the script to create all backup copies in the $VCDIR directory - it would never attempt to create or use any subdirectories under $VCDIR. Is this what you wanted?

MK
MK
Khalid Shakoor
Regular Advisor

Re: Failed to copy file when give file name with complete path

Thanks Guruz,
I just add one more step first copy then move into target directory and its seem its working fine for me.
the changes are below.

ipfilename=$1
if [ -f "$ipfilename" ];
then
if [ -d "$VCDIR" ]; then
cp -p "$ipfilename" "$ipfilename"_$ydt
mv "$ipfilename"_* "$VCDIR"/
chmod -x "$VCDIR"/"$ipfilename"
echo "$ydt :: Version Controlled Successfull for $ipfilename " >>$CLOG
echo "Version Controlled Successfull "
else


Thanks MK but this changes are work fine for me thats why i did not follow your suggestion but Thanks.

Khalid