LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script which will check the target file and folder exists and copy it (https://www.linuxquestions.org/questions/programming-9/shell-script-which-will-check-the-target-file-and-folder-exists-and-copy-it-4175525440/)

agent_mach 11-14-2014 08:34 PM

Shell script which will check the target file and folder exists and copy it
 
Hi All,

I am a beginner in this and trying to write a shell script which will :

1. Ask for a file name and check if its exists.
2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists.
3. If target folder exists it will copy that file in to it.

I have written the below code and it's working fine if file and folder exists but not working properly when any of them is not exists.



Code:

#!/bin/bash

read -p "Enter file name : " filename
echo "Please wait checking if $filename exists ..."

if [ -f "${filename}" ]

then

        echo "File $filename exist."
        read -p "Enter location to copy $filename : " location

else

        echo "$0: $filename not found."

fi
                if [ -d "${location}" ]

                then

                        echo "Target location found. Initiating file copy to $location."
                        cp -v "$filename" "$location"
                        echo "$filename copied to $location."

                else

                        echo "$0: $location not found."

                fi


Please let me know if i missed something.

Thanks,
Ashish

a4z 11-15-2014 12:25 AM

either you exit if the file was not found
or you move up the if directory part into the if branch where the file exists

ondoho 11-15-2014 05:36 AM

duplicate of this solved topic: http://www.linuxquestions.org/questi...it-4175525444/


All times are GMT -5. The time now is 07:02 AM.