LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2014, 09:07 PM   #1
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Rep: Reputation: 0
Question 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
 
Old 11-14-2014, 10:18 PM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
I did not test your code, but there obvious error:
You put the second if-else-fi construction in a wrong place.
Try this:

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
        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
else
        echo "$0: $filename not found."
fi
 
1 members found this post helpful.
Old 11-17-2014, 07:50 AM   #3
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Hi Teufel. thanks for the help.

Now i am trying to do :

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, if doesn't exits it will create one with with the $location value.
3. It will copy that file in to it.


I have made below changes in to above script. Its working but i facing minor issue in it:

If i am trying to copy existing file in to non existing folder it's working fine and throwing below message

Code:
/etc/inittab copied to /OOO.
But if i am copying existing file in to existing folder i am not getting above message.

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

    if [ -d "${location}" ]
    then
        echo "Target location found. Initiating file copy to $location."
        cp -v "$filename" "$location"
    else
        echo "$location not found ! Creating..."
        mkdir -p "$location"
        echo "$location folder created. Copying $filename to $location ..."
        cp -v "$filename" "$location"

        if [ $? -eq 0 ]
        then
                echo "$filename copied to $location."
        else
                echo "ERROR while copying $filename to $location. Please check write permission or disk space and try again !!!"
        fi

     fi

else
    echo "$filename not found."
fi

I will really appreciate if you point out needed changes.

Thanks,
Ashish
 
Old 11-17-2014, 08:15 AM   #4
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Lightbulb

Issue resolved after :


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

    if [ -d "${location}" ]
    then
        echo "Target location found. Initiating file copy to $location."
        cp -v "$filename" "$location"
    else
        echo "$location not found ! Creating..."
        mkdir -p "$location"
        echo "$location folder created. Copying $filename to $location ..."
        cp -v "$filename" "$location"
    fi
        if [ $? -eq 0 ]
        then
                echo "$filename copied to $location."
        else
                echo "ERROR while copying $filename to $location. Please check write permission or disk space and try again !!!"
        fi

else
    echo "$filename not found."
fi
 
Old 11-17-2014, 08:30 AM   #5
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Quote:
Code:
    if [ -d "${location}" ]
    then
        echo "Target location found. Initiating file copy to $location."
        cp -v "$filename" "$location"
    else
        echo "$location not found ! Creating..."
        mkdir -p "$location"
        echo "$location folder created. Copying $filename to $location ..."
        cp -v "$filename" "$location" 
    fi
if-else-fi block is excessive. It might be much more simpler:
Code:
mkdir -p "$location"
cp -v "$filename" "$location" && echo "$filename copied to $location."
-p option suppresses error message if directory already exists.

Last edited by Teufel; 11-17-2014 at 08:35 AM.
 
Old 11-18-2014, 01:33 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I recommend http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

Also, in your case,
Code:
if [ -d "${location}" ]
the {}'s are redundant

For debugging, try
Code:
#!/bin/bash
set -xv
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script which will check the target file and folder exists and copy it agent_mach Programming 2 11-15-2014 05:36 AM
[SOLVED] shell script to copy files into folder that matches the file name daveant Linux - Newbie 5 12-07-2011 07:28 AM
How to write a shell script which will check a file name in a dir and copy it siris Programming 3 04-08-2010 07:54 PM
check if directory exists using shell script v333k Programming 9 04-23-2009 09:29 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration