LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-07-2007, 12:41 PM   #1
fopetesl
Member
 
Registered: Jan 2005
Location: Yorkshire Dales, UK
Distribution: Ubuntu 5.10; Mandriva 2007; Debian Lenny
Posts: 147

Rep: Reputation: 15
Unhappy BASH: FOR control with two variables?


I'm trying to write a script which loops copying dual files to another directory but (even having RTFM) I can't find a way to do it.
There doesn't seem to be an AND control I can use.
Code:
for file in *.dta
do
cp -f $file /home/mystuff/datafile.dta
done
works fine for every single file in the current directory.

However what I actually need to do is something like this
Code:
for fileA in *F.dta AND fileB in *R.dta
do
cp -f $fileA /home/mystuff/datafileF.dta
cp -f $fileB /home/mystuff/datafileR.dta
done
Doesn't throw any warnings except the $fileB copy fails with "can't find directory"

In C it would be a piece of cake so there must be a way in BASH. No?
 
Old 12-07-2007, 01:03 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Can you guarantee that for every *F.dta there is a corresponding *R.dat?

If so, you can iterate over the list of all *F.dta, but for each iteration perform a second copy operation replacing the F.dat ending with R.dat. In this example I check to see if there is a corresponding file, and do the copy of both only if this is true:
Code:
for f_file in *F.dta; do
    d_file="${f_file%F.dta}D.dta"
    if [ -a "$d_file" ]; then
        echo "replace this with some copy operation which you like"
    else
        echo "there is no file $d_file corresponding to $f_file"
    fi
done
 
Old 12-07-2007, 01:21 PM   #3
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Thumbs up

Quote:
Originally Posted by fopetesl View Post
Code:
for fileA in *F.dta AND fileB in *R.dta
do
cp -f $fileA /home/mystuff/datafileF.dta
cp -f $fileB /home/mystuff/datafileR.dta
done
Doesn't throw any warnings except the $fileB copy fails with "can't find directory"
That, of course doesn't work. (What it does is assign to fileA in sequence: All matches for *F.dta, then 'AND', then 'fileB', then all matches for *R.dta. fileB never becomes a variable)

I'm not sure, though what you actually intended to do. Is it
  1. Copy all *F.dta files and all *R.dta?
    Code:
    for file in *F.dta *R.dta ; do ...
    would do that.
    But so would a simple
    Code:
    cp *F.dta *R.dta /home/mystuff
  2. Only if there are corresponding *F.dta and *R.dta then copy both of them?
    Just iterate over one of them, then inside the loop check if the corresponding file exists, then copy both:
    Code:
    for fileA in *F.dta ; do
            fileB="${fileA%F.dta}R.dta"
            if [ -f "$fileB" ] ; then
                    cp ...
    PS: Which is basically what Matthew already wrote. Didn't see that at first. (But you don't need to guarantee they both exist, as you're checking for that inside the loop)

Last edited by rupertwh; 12-07-2007 at 01:26 PM.
 
Old 12-08-2007, 03:07 AM   #4
fopetesl
Member
 
Registered: Jan 2005
Location: Yorkshire Dales, UK
Distribution: Ubuntu 5.10; Mandriva 2007; Debian Lenny
Posts: 147

Original Poster
Rep: Reputation: 15
Question Maybe the answer?

Maybe I have an answer here. However, to clarify:
I should have made it clearer that files named *F.dta need to be copied to a specific filename, always the same.
So a file originally named, say, "20071208134214F.dta" (note the date and time) would have to be copied to "AlwaysUseSameFileName_F.dta".
Similarly for the 'R' file.
There are always matched pairs of *.dta files.

I have a C program which processes the data from the two resultant files, "AlwaysUseSameFileName_F.dta" and "AlwaysUseSameFileName_R.dta"

So My original (incorrect) code should have read:
Code:
for fileA in *F.dta AND fileB in *R.dta
do
cp -f $fileA /home/mystuff/AlwaysUseSameFileNameF.dta
cp -f $fileB /home/mystuff/AlwaysUseSameFileNameR.dta
cd /home/mystuff
./ProcessTheTwoSiblingDataFiles
cd /home/logfiles
# go round this loop until all pairs of *.dta files are processed
done
Thanks, guys. Will check out your idea(s) later (have vehicle problems, sigh).

Thanks, Matthew. Thanks, Rupert. Works just great.
What I don't understand is why it works.
For instance
Code:
d_file="${f_file%F.dta}D.dta"
    if [ -a "$d_file" ]; then
has me totally bemused.
I can't see how the parameter from $f_file is taken and converted into D.dta and hence into $d_file. Is it an increment on $f_file to find D.dta?
Whatever, it works

Last edited by fopetesl; 12-08-2007 at 12:04 PM.
 
  


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
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
variables in bash haddad Linux - General 6 09-22-2004 05:29 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:38 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