Help with file copy script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with file copy script
# 8  
Old 11-11-2016
Quote:
Originally Posted by r34lj4k3
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?
Yes and no: programming is first and foremost about planning and you first need to define (in a painstakingly precise way) what exactly you do want: do you want to enter the user file names? Or filename-masks (like i.e. "enter abc and the script should process all files named abc[something].txt"). Or every file in a certain path when a directory is entered. (Maybe only files with a certain ending in said directory.) .....

You see, the possibilities are endless and as long as you do not explain exactly here (instead of only in general terms) what it is you want to achieve i can only help you in a general way.

Quote:
Originally Posted by r34lj4k3
Still toying with this trying to get it to work. Is the red a typo?
First off: good attitude! Thecode is meant to play around with it. Taking it apart and trying to understand how it works is how you learn the trade.

Second: no, that is not a typo! To help me keep track of my variables i acquired the habit of using "Hungarian Style Notation": a system where a set of prefixes to the variable name explains what the content is (or should be). In this case "a" stands for "array" and "f" stands for a path name (from "file"), so "afFile" is an array of path names named "File".

You do not need to keep this convention and there are a lot of very good programmers who do not use this or use it differently. For me it is helpful, so i use it. As you go along you will acquire your own style at some point, but the one thing you should always try to achieve is: be consistent, at least with yourself. If you do something in a certain way, do it always this way. If you use Hungarian Style Notation then use it always - or not at all! This is what will your ode make easy to grasp and that will make it easier to maintain it.

I hope this helps.

bakunin
# 9  
Old 11-11-2016
Apologies for how I went about this, I thought I could get away with just getting help with a part of it because I really do want to do this myself. I did not come asking for help lightly, but I was so far in the weeds on this one that I figured it would be best.

The entirety of the script's job is:

1: Prompt user from which machine on the network they would like to pull files

2: ssh to that machine and ls -lthr directory a and b

3: copy files chosen by the user from directories a and or b on the remote host to a given directory (/tmp) on the localhost

I've been attempting to modify the script as needed to do the ssh'ing and making the commands work on the remote machine. Like I said, I was trying to do as much as possible myself so that I would learn. That being said, I think this was harder than I anticipated it would be.

---------- Post updated at 09:44 PM ---------- Previous update was at 09:40 PM ----------

As for code, I have:

Code:
 echo "From which machine would you like to pull files?"
  
 read machine
  
 ssh -q $machine ls -lthr /directorya /directoryb
  
 echo "Which files would you like to copy?"
  
 read -a files

and then I've entered the code you wrote earlier in the thread but edited to ssh :

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

if   ssh $machine [ -e "${fPathA}/$fFile" ] ; then
     ssh $machine scp "${fPathA}/$fFile" "($hostname|cut -d. -f1):/$fFile"
elif ssh $machine [ -e "${fPathB}/$fFile" ] ; then
     ssh $machine scp "${fPathB}/$fFile" "($hostname|cut -d. -f1):/$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

---------- Post updated at 09:45 PM ---------- Previous update was at 09:44 PM ----------

I have tried to get them to work together playing around with it, but I haven't been able to get it to work.

Last edited by r34lj4k3; 11-11-2016 at 03:29 AM..
# 10  
Old 11-11-2016
Quote:
Originally Posted by r34lj4k3
My question is, given that the files aren't static names, how do I pass along the user's input into this loop?
Quote:
Originally Posted by r34lj4k3
Still toying with this trying to get it to work. Is the red a typo?
If you call your variable afFile, why should it be a typo to refer it in this way?

However, your typeset statements are odd, and I don't quite get, what exactly you want to achieve.

Since the filenames are varying, you first decide, how the user should pass the names to your script. In your script, you did it with a

read -a files

which is technically possible, but interactively asking the user for a list of filenames is maybe not very convenient.

But, if you want to do it, go ahead; you will then have the names of the files stored in an array called 'files', and you could access the individual elements by ${files[0]}, ${files[1]} etc. However, accessing the files by index won't be necessary in your case, because you want to loop over these files and process each file separately, and the best way to process this array would be like this

Code:
for file in ${files[@]}
do
        ... code to process file $file
done

BTW, if you really want to request the files using the read -a command, you should add in your prompt some instruction to the user, how the files need to be input; for example: "Please enter the filenames to be processed, separated by spaces".

One disadvantage of this solution is, that it doesn't work, if the file path entered by the user contains spaces.
# 11  
Old 11-11-2016
Quote:
Originally Posted by rovf
If you call your variable afFile, why should it be a typo to refer it in this way?
Because didn't we say it was called fFile when we said:

Code:
 typeset fFile="$1"

?

Quote:
However, your typeset statements are odd, and I don't quite get, what exactly you want to achieve.

Since the filenames are varying, you first decide, how the user should pass the names to your script. In your script, you did it with a

read -a files

which is technically possible, but interactively asking the user for a list of filenames is maybe not very convenient.
In this case, all the user must do is double click and then middle click because the ls -lthr from earlier in the code makes it quick and easy to pick and choose the files you want and there's not really a good way to pick them script wise, until we have AI, a human will have to do this part.

Quote:
But, if you want to do it, go ahead; you will then have the names of the files stored in an array called 'files', and you could access the individual elements by ${files[0]}, ${files[1]} etc. However, accessing the files by index won't be necessary in your case, because you want to loop over these files and process each file separately, and the best way to process this array would be like this

Code:
for file in ${files[@]}
do
        ... code to process file $file
done

BTW, if you really want to request the files using the read -a command, you should add in your prompt some instruction to the user, how the files need to be input; for example: "Please enter the filenames to be processed, separated by spaces".
Noted, but I'm still not sure how to make it so that it checks if the file exists, ssh's to the machine, copies the files back to local host if it exists or simply tries the same stuff in the second directory, elsewise, it reports that the file doesn't exist.

Quote:
One disadvantage of this solution is, that it doesn't work, if the file path entered by the user contains spaces.
Didn't know this, thank you. The array will still work, though. No spaces in the file names.
# 12  
Old 11-11-2016
Quote:
Originally Posted by r34lj4k3
Because didn't we say it was called fFile when we said:

Code:
 typeset fFile="$1"

?
Yes, but in the last posting, there was a variable named afFile, so I though the name is OK. Of course you can name a variable as you like.

In any case, I wonder why you use typeset here. You are allowed to do it, but you can equally well write tFile="$1" .

---------- Post updated at 10:21 AM ---------- Previous update was at 10:18 AM ----------

Quote:
Originally Posted by r34lj4k3
Noted, but I'm still not sure how to make it so that it checks if the file exists, ssh's to the machine, copies the files back to local host if it exists or simply tries the same stuff in the second directory, elsewise, it reports that the file doesn't exist.
Well, this is an unrelated issue, and I think you should make several discussion threads out of this, lest the conversation will be confusing.

So, for now, you have broken down the problem to the point where you have a single file name, which you need to process somehow. Now you have new problems: Checking the existence of a file. Copying the file. And what else more. Each of these problems is independent, so make a new posting out of each.
# 13  
Old 11-11-2016
Quote:
Originally Posted by rovf
---------- Post updated at 10:21 AM ---------- Previous update was at 10:18 AM ----------

Well, this is an unrelated issue, and I think you should make several discussion threads out of this, lest the conversation will be confusing.
This piece was the entire reason for this post in the first place.
# 14  
Old 11-14-2016
Quote:
Originally Posted by r34lj4k3
This piece was the entire reason for this post in the first place.
This was not clear to me, because you initially asked about a specific error message you got, and then the thread focused on this problem.

Still now, it is not clear to me what exactly you would like to now. In any case, all the other points you mention - copying, checking and so on - should better go to different threads, because each of them can be discussed separately (and, depending on your environment, might have non-trivial aspects to discuss).

I suggest to start separate threads for each item, and also indicate what exactly is your problem in each case.
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