Help with file copy script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with file copy script
# 1  
Old 11-10-2016
Help with file copy script

Hello, I am still pretty new at writing shell scripts and may have bitten off more than I can chew. I am trying to write a script that figures out if a file exists (a known name) in directory a, then copy it to my selected destination, otherwise, look in directory b and check there. If it is in neither place, I'd like an output saying that it was not copied/doesn't exist. I need to be able to do this for multiple files that may be in either directory a or b.

I am currently trying to pass my user requested inputs into an array and have a for loop operate on each member of the array but I am not sure I am doing either part quite right and I'd like to know if what I'm doing will even work before I pour tons of hours into it. Currently, my script copies the first file I list (happens to be in directory a), then gives the error, "Bad escape character" and lists the file path for directory b with the already copied file from directory a. My code is not on this pc, so I'd have to manually transcribe it and I will if I must.

Any help is greatly appreciated.

---------- Post updated at 10:44 PM ---------- Previous update was at 10:04 PM ----------

Code:
read -a files 
 for i in $files
 do
 if
 test -e /directory a/$files
 then
 for i in $files
 do
 cp /directory a/$files /directory
 done
 elif
 test -e /directory b/$files
 then
 for i in $files
 do
 cp /directory b/$files /directory
 done
 fi
 done


Last edited by r34lj4k3; 11-10-2016 at 03:00 AM..
# 2  
Old 11-10-2016
Code:
read -a $files

That $ should not be there as you are reading ( like affectation...) but forgive me if Im wrong Im just got up ( waken by my daughter who cant print pfff...) not properly awake yet
Then bad idea to give variable names in lower case as its confusing for old guys like me...
This User Gave Thanks to vbe For This Post:
# 3  
Old 11-10-2016
That was actually a transcription error, but, thank you, none the less!
# 4  
Old 11-10-2016
The trick with shell programming (like with programming in general) is to "divide and conquer": make several smaller (simple) problems from one big complex problem and solve these.

Applying this general advice to your task means:

Quote:
script that figures out if a file exists (a known name) in directory a, then copy it to my selected destination, otherwise, look in directory b and check there. If it is in neither place, I'd like an output saying that it was not copied/doesn't exist. I need to be able to do this for multiple files that may be in either directory a or b.
Now let us break that down: let us suppose we had a command which does exactly what you want for one file. Let us further suppose we already have a (fixed) list of files we want to test. Then the script would be easy:

Code:
#! /bin/ksh

typeset afFile[1]="file.foo"            # my list of fixed files
typeset afFile[2]="file.bla"
typeset afFile[3]="file.other"

typeset -i iCnt=0                        # a counter to process the array

(( iCnt = 1 ))
while [ $iCnt -le ${#afFile[@]} ] ; do
     pWorkFile "${afFile[$iCnt]}"
     (( iCnt += 1 ))
done

exit 0

The counter "iCnt" in the while-loop is just taking one number (1, 2, ...) after the other as long as there are corresponding array elements with such a number. When we run out of array elements the loop is left and we end.

But of course there is no such thing as "pWorkFile" which gets the name of one file and does what you want, so we have to write that now. But because it has to do only one file instead of a list, it is easier to do that:

Code:
#! /bin/ksh

pWorkFile ()
{
typeset fFile="$1"                       # $1 holds the parameter with which it was called
typeset fPathA="/some/where"
typeset fPathB="/else/where"

if   [ -e "${fPathA}/$fFile" ] ; then
     cp "${fPathA}/$fFile" "${fPathB}/$fFile"
elif [ -e "${fPathB}/$fFile" ] ; then
     cp "${fPathB}/$fFile" "${fPathA}/$fFile"
else
     print - "Neither ${fPathA}/$fFile nor ${fPathB}/$fFile exists."
fi

return 0
}


# -------------------------------------- start of main program
typeset afFile[1]="file.foo"            # my list of fixed files
typeset afFile[2]="file.bla"
typeset afFile[3]="file.other"

typeset -i iCnt=0                        # a counter to process the array

(( iCnt = 1 ))
while [ $iCnt -le ${#afFile[@]} ] ; do
     pWorkFile "${afFile[$iCnt]}"
     (( iCnt += 1 ))
done

exit 0


And in the same way you finally include code for a variable (instead of a fixed) list of filenames, which i leave for you as i do not want to take all the fun of programming from you.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 11-10-2016
I said shell scripts in my first post, but I meant bash scripts. I am completely unfamiliar with ksh and only kinda sorta understand how that translates.
# 6  
Old 11-10-2016
Quote:
Originally Posted by r34lj4k3
I said shell scripts in my first post, but I meant bash scripts. I am completely unfamiliar with ksh and only kinda sorta understand how that translates.
No problem, as what i wrote above is tru for both ksh and bash alike.

Change:

Code:
#! /bin/ksh

to

Code:
#! /path/to/your/bash



which is presumably /bin/bash or /usr/bin/bash, depending on your system and change the "typeset" statements to "local". This should work.

I hope that helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 11-10-2016
Thank you so much for your help thus far! I do have an issue still, though. The files I will be checking for have names that vary (they are time-stamped, along with other related info) and thus they do not have static names. That is why I was trying to put them into an array and operate on them individually in my original code.

I have a section of code that gives me the lists of files in these two locations and then I prompt the user for which of the files they would like to copy. Again, this is only problematic because they could be in one of two known locations.

My question is, given that the files aren't static names, how do I pass along the user's input into this loop? Was I on the right track with read -a to put it into an array?

---------- Post updated at 07:25 PM ---------- Previous update was at 05:52 PM ----------

Still toying with this trying to get it to work. Is the red a typo?

Code:
while [ $iCnt -le ${#afFile[@]} ] ; do
     pWorkFile "${afFile[$iCnt]}"

This is my first time working with typeset,

Last edited by r34lj4k3; 11-10-2016 at 10:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for File Copy

Hello All, This is my first post to unix.com. I have requirement to copy multiple *.dmp files from one server to other server. I have that other server NFS filesystem mapped to source server. Dump files are so huge almost 20TB. There are around 15-20 files each of 1.1TB. I want to copy these... (6 Replies)
Discussion started by: Amit Bansode
6 Replies

2. Shell Programming and Scripting

File copy script

Looking for help on a script that does the following. Reads a text file with ~5000 lines that look something like this: /folder/path/path2/pathX/file.lua,d/1.2699.gz /folder/path/path4/pathX/file2.lua,d/1.2699.gz /folder/path/path2/pathX/data/file3.lua,d/1.2699.gz And copies the file from... (8 Replies)
Discussion started by: kwalan
8 Replies

3. Shell Programming and Scripting

Shell script to copy file

Dear all, I have a database with thousands of files with the structure of name is: Filename_hour_year.abc Filename_hour_year_1.abc .............. So what I need is how to write a script that all file with contain the character "_1" will copy to "_2" For example: file name:... (7 Replies)
Discussion started by: hainguyen1402
7 Replies

4. UNIX for Dummies Questions & Answers

Script to copy file small interval file

HI Guys whenever i post a transaction , abcd.in file is stored in the temp and vanishes when the transaction is compleated . the abcd.in file stays for 1 sec in temp is der any solution to capture the abcd.in file to another directory cp -r /tmp/abcd.in /tmp/smith.in when i used the... (1 Reply)
Discussion started by: kalyankalyan
1 Replies

5. Shell Programming and Scripting

Match File and Copy File Script (Homework, Closed)

Can you please help on this? I am looking for the shell script which does following:- step 1: It should open the file /u/manish/input/FileIndex.dat and read line by line step 2: Once first line is read (for ex: File1), we have to find a file, that contains this matching... (4 Replies)
Discussion started by: teteguru1
4 Replies

6. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

7. Shell Programming and Scripting

copy file from script file to remote computer

hi, i want copy one or group of file from a computer to others, but i have some problem that do not allow me to do this. i do this by scp command like this : scp <file name> root@cpName:destinationAddress but the problem is that it do not it automatically. it means when it is connecting to... (4 Replies)
Discussion started by: MShirzadi
4 Replies

8. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

9. Shell Programming and Scripting

Script look for file ,sort and copy to other direcotry

Hi I am in a situation to write a shell script that looks for the flat files continuosly and if files exists in that directory sort on the files and get the latest file(File comes with timestamp at the end) and copy the latest file to the other directory and again copy the next one to the same... (0 Replies)
Discussion started by: reddi22
0 Replies

10. Shell Programming and Scripting

Help:Copy file from one to other using script

Hi Frineds, Through my DTS process I am generating one file at one directory like, /sqlsrvr_demo/dts/put/transaction_fact.csv now I want to copy this files(only when the files size is greater than 0 kb) to some other directory like /sqlsrvr_demo/dts/get/transaction_fact.csv Can... (2 Replies)
Discussion started by: sunnysunny
2 Replies
Login or Register to Ask a Question