LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2011, 11:30 AM   #1
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Rep: Reputation: 17
Bash for loop


I am having some trouble with this script I am writing. I need this script to copy all of the files from one dir to an other. I have to use the "TO" and "FROM" variables for the rest of the script so I thought the "for" loop was the best solution. The problem I am having is I need the for loop to do somthing like this:
Code:
#!/bin/bash
FROM="
/home/test1/hey
/home/test1/hey1
/home/test1/hey2
/home/test1/hey3
/home/test1/hey4
/home/test1/hey5
"
TO="
/home/test2/hey
/home/test2/hey1
/home/test2/hey2
/home/test2/hey3
/home/test2/hey4
/home/test2/hey5
"

for FILE in ${FROM} DEST in ${TO}
do
   cp -v ${FILE} ${DEST}
done
Obviously this will not work but I need something similar to do the same thing.

Thanks

Last edited by edwardcode; 08-24-2011 at 12:42 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-24-2011, 11:40 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Unless I am misunderstanding the point here, you seem to be needlessly overcomplicating this process. This would work just fine:

Code:
cp /home/test1/* /home/test2/
 
1 members found this post helpful.
Old 08-24-2011, 11:42 AM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Something like this in case you want to pass the source and destination directories as arguments?

Code:
#!/bin/sh

SRCDIR=$1
DESTDIR=$2

cp -v $SRCDIR/* $DESTDIR/

exit 0
Quote:
Originally Posted by szboardstretcher View Post
...you seem to be needlessly overcomplicating this process.
Hehe, I needlessly over-complicate stuff like this all the time before I realize what I've done.

Last edited by weibullguy; 08-24-2011 at 11:46 AM.
 
1 members found this post helpful.
Old 08-24-2011, 11:47 AM   #4
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
well this is just a test script for what is going to be used in a different environment. The real environment will have all of the files in different locations and different destinations.

Sorry for the miss understanding.
 
0 members found this post helpful.
Old 08-24-2011, 11:49 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
I believe I see what you are trying to do here. You are trying to increment *both* variables as you go through the loop. Correct?
 
1 members found this post helpful.
Old 08-24-2011, 11:58 AM   #6
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
yes
 
0 members found this post helpful.
Old 08-24-2011, 12:24 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When you're working with sequences, such as lists of filenames, you should generally use arrays instead. Remember, a variable is a single string, while an array is a string of strings (a list).

So something like this:
Code:
#!/bin/bash
FROM=(
/home/test1/hey
/home/test1/hey1
/home/test1/hey2
/home/test1/hey3
/home/test1/hey4
/home/test1/hey5
)

TO=(
/home/test2/hey
/home/test2/hey1
/home/test2/hey2
/home/test2/hey3
/home/test2/hey4
/home/test2/hey5
)


for i in "${!FROM[@]}"; do
   cp -v "${FROM[i]}" "${TO[i]}"
done
As long as the index numbers for the files match in both arrays, you should get your desired behavior.

BTW, remember to always quote your variable expansions!

Edit: Just thinking, depending on your purposes, you may be able to shorten the above with parameter substitution. You could use it to modify the directory path directly, so you'd need only one array.

Code:
#!/bin/bash
FILES=(
/home/test1/hey
/home/test1/hey1
/home/test1/hey2
/home/test1/hey3
/home/test1/hey4
/home/test1/hey5
)
NEWPATH="/home/test2"

for i in "${!FILES[@]}"; do
   cp -v "${FILES[i]}" "$NEWPATH/${FILES[i]##*/}"
done

Last edited by David the H.; 08-24-2011 at 12:38 PM. Reason: as stated
 
3 members found this post helpful.
Old 08-24-2011, 12:48 PM   #8
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
If it is not to much to ask I would like to ask you a few questions so I understand what is going on in your script.

1. In the statement below what dose the "!" do?
for i in "${!FROM[@]}"; do

2. In the statement below what dose the "[@] do?
for i in "${!FROM[@]}"; do

3.in the statement below how is the "[i]" used?
cp -v "${FROM[i]}" "${TO[i]}"


Thanks
 
0 members found this post helpful.
Old 08-24-2011, 12:50 PM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
2. In the statement below what dose the "[@] do?
for i in "${!FROM[@]}"; do
@ references all the members in the array
 
0 members found this post helpful.
Old 08-24-2011, 01:38 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The links I gave above should explain most of it, and the rest should be easily locatable in the bash man page or on the web. But anyway:

Quote:
1. In the statement below what dose the "!" do?
for i in "${!FROM[@]}"; do
${!array[@]} outputs a list of all existing array indexes. This is quite useful for iterating over the array in a for loop. Stick an echo in front of it and see what you get.

Quote:
2. In the statement below what dose the "[@] do?
for i in "${!FROM[@]}"; do
As mentioned above, it references the entire array. @ considers each entry as a separate element (where applicable), particularly when quoted. There's also *, which references the entire array as a single unit. Again, try echoing them and see what you get.

Quote:
3.in the statement below how is the "[i]" used?
cp -v "${FROM[i]}" "${TO[i]}"
i is simply the for loop's variable. It contains each index number from "${!FROM[@]}" in turn, so it's "${FROM[0]}" first, then "${FROM[1]}", then "${FROM[2]}", etc.

Note that in regular arrays the [] index field has an arithmetic context, so the only things allowed in it are numbers, mathematical operators, and variable names, which are automatically expanded, even without $ in front of them.
 
1 members found this post helpful.
Old 08-25-2011, 08:00 AM   #11
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
thanks
 
Old 08-26-2011, 08:51 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
For more information, see "man bash" and search for "Parameter Expansion", by typing this:

Code:
/Parameter Expansion
and pressing Enter.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Bash for loop rhlee Linux - Software 3 01-12-2010 03:59 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
BASH - while loop snorkytheweasel Linux - Desktop 2 03-26-2008 09:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 AM.

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