LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Copy files (http://www.linuxquestions.org/questions/programming-9/copy-files-4175448147/)

ankitpandey 02-01-2013 06:39 AM

Copy files
 
Hello Team,

I am trying to copy file from source to target on basis of argument provided by user but it seems i am not trying correct. Please help on how to proceed.

Below is what i tried. I am sure its wrong.

Code:

#!/bin/sh

echo "Enter Source path : $1"

echo "Enter file Name : $2"

echo "Enter Destination path : $3"

cd $3
mv $2 $2_bkp

cp $1/$2 $3

Thanks

acid_kewpie 02-01-2013 06:43 AM

care to actually tell us what's not working? Error messages?

ankitpandey 02-01-2013 06:45 AM

Below is the error message :

Quote:

Enter Source path :
Enter file Name :
Enter Destination path :
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
mv [-f] [-i] f1 ... fn d1
mv [-f] [-i] d1 d2
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

acid_kewpie 02-01-2013 06:49 AM

oh, right so... you expect it to get these values from where?? if you're not going to put them in as command line parameters then all you're doing is printing the values out in the echo command. You'd use a read command to get input from the user:

echo "Enter Source path :"

read source_path

echo $source_path


etc

jpollard 02-01-2013 07:24 AM

It would also likely be very useful to validate the input - make sure the source file exists, and that the destination file doesn't exist, and verify that an overwrite is what was wanted.

ankitpandey 02-01-2013 07:34 AM

Thanks acid_kewpie , that is what i was missing. Now i am able to copy it.

Hello jpollard, how can i confirm if overwrite happened or not ? Is there any build in command foer that or i need to compare time.

jpollard 02-01-2013 08:33 AM

Before doing it, look to see if the destination file exists...

The bash manpage has a number of functions capable of checking files listed.

ankitpandey 02-01-2013 10:40 AM

Thanks all


All times are GMT -5. The time now is 01:36 AM.