ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.