0

It can not generate two temp-files! Why not? Please help!! The input-file has two columns for the file names.

But it fails to copy the TWO-files "fn1", "fn2" into "Data1.dat", "Data2.dat"

What is wrong here??

#!/bin/bash

FF1=$1
while IFS=" " read -r line1  line2; 
do
  fn1=$line1
  fn2=$line2

  cp $fn1   Data1.dat
  cp $fn2   Data2.dat

...other ext-scripts!...

done  < $FF1

This code failed copying, so as to generate those two temporary data-files! There are STILL the"error-messages for cp-operation lines"

Please help to make the two files correctly. Thx.

David
  • 1
  • 2
  • done > $FF1 -- you are writing to that file, not reading from. – glenn jackman Oct 05 '19 at 03:52
  • Could you be more specific?? I'm a real-beginner of this kind of script, starting today. The problem is that this code can not copy those "Data1.dat", "Data2.dat" files...!! P.S) I'm doing an iterative works using this code to run ext-script by reading line by line from another file~! – David Oct 05 '19 at 04:09
  • @glenn The error-message: "cp: target 'Data1.dat' is not a directory; cp: missing destination file operand after 'Data2.dat'" – David Oct 05 '19 at 04:39
  • @glenn . Thanks. I got your point~! but the problem is the copying the two-files~! – David Oct 05 '19 at 04:47
  • Currently, the error-message is in cp-error : "No such file or directory~!" I confimred that the two-file names have been transfered well into "$fn1", "$fn2" from the original-list file. But still failing to generate "Data1.dat" and "Data2.dat" – David Oct 05 '19 at 05:38
  • double-quote your variables when you use them. e.g. fn1="$line1" and cp "$fn1" Data1.dat. See Why does my shell script choke on whitespace or other special characters? – cas Oct 05 '19 at 06:23
  • Can you add this line to the top of your script and let us know if there's any output? awk '{ if (NF != 2) print "bad line: " $0 }' < "$1" – Mark Plotnick Oct 06 '19 at 02:21
  • @cas: Thanks. Yes. correct! It seems a bit different in the shell-script in using the quotes than in gnuplot-script. Usually, it made errors in gnuplot in my cases. – David Oct 07 '19 at 03:27
  • @ Mark: Thanks for the new script~! What is the meaning of the line?? I guess it means if the files are not 2, then print something in the list file?? == I found nothing wrong~! That may mean my file-ist is OK!? It seems that there are something related problems with other gnuplot-script related with this script. – David Oct 07 '19 at 03:47

1 Answers1

2

I reproduced your kind of loop with:

while IFS="  " read -r line1 line2; do echo "cp $line1 $line2"; done <in

This produces (with a simple "in" file):

cp 111 aaaa
cp 222 bbb
cp 3333 ccccc

But if say the third line contains three names (i.e. a name with a space), you get

cp 33 33 ccccc

and now suddenly cp "switches" to the "copy several files into a directory" mode, but fails, because target "ccccc" (or 'Data1.dat') is not a directory.

cp: missing destination file operand after 'Data2.dat'

This happens after cp onefile. Something is mixing up your arguments. You have to do some tests...

The input-file has two columns for the file names.

Are you sure?

  • Many thanks for the comments. Yes, the main-problem is CP command is "Not copying into other files" now~!! Sure, I check the original list file's contents by printing each column it should correctly be containing the original file names. I will see/check your comments now! – David Oct 05 '19 at 06:09
  • btw: since you humbly classify yourself as "today's real beginner"...maybe you should outline your (iterative?) idea a bit more clearly? There are so many things to consider in a shell "project" like that! A good description is half the solution --- PS thanks too for the feedback –  Oct 05 '19 at 06:54
  • OK~! I'm a novice for the shell script's grammar. Actually, I have made numerous Monte-Carlo simulations with my C-codes, so usually, I dealt with the final data with gnuplot-scripts to generate figures. In this case, I should have to use an automatic-process for the few hundred files. Today I listed file names with conditions and paste them into a pair of files to do the more efficient fitting using the shell script. Finally, I found the way for solution~!! According to your comments,I found the reason was the delimiter problem during paste. Many thanks to your great-suggestion~!! – David Oct 05 '19 at 07:44