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.
Hi, need to process files within multiple directories and transfer these to a remote server. What I had in mind was to use arrays for this; code pasted below (explains it better):
Code:
#set the arrays
array_A=( fileA* pathtodestA hostA passwordA )
array_B=( fileB* pathtodestB hostB passwordB )
array_c=( fileC* pathtodestC hostC passwordC )
# set variables within the loop with elements from each array at the time
# and call an ftp function with the variables as arguments
for arrays in array_A array_B array_C ; do
# this is where I get stuck
file=${arrays[0]}
path=${arrays[1]}
host=${arrays[2]}
user=${arrays[3]}
pass=${arrays[3]}
# call ftp function
transfer_func $file $path $host $user $pass
So actually handle each array at a time.
I'am fairly new to this and guess my approach doesn't make any sense at all.
By the way, even better, if possible, would be to move the setting of the array to inside the loop along with the rest of it. Then you'd only need to use one array at a time. If the each input is a line in a file, for example:
Code:
while read inputstring ; do
array=( $(echo $inputstring) )
file=${array[0]}
path=${array[1]}
host=${array[2]}
user=${array[3]}
pass=${array[3]}
done <inputfile.txt
for some reason it doesn't work. i'll go and try to grasp what you guys have put up. and hopefully I can figure this out on my own
Some (maybe) useful notes:
1. arrays in bash are 0-based
2. array's element separator in array assignment is $IFS, not comma
3. also there is an error in the C-style for-loop definition.
Maybe this works (check the differences to see the flaws):
Some (maybe) useful notes:
1. arrays in bash are 0-based
2. array's element separator in array assignment is $IFS, not comma
3. also there is an error in the C-style for-loop definition.
Maybe this works (check the differences to see the flaws):
IFS=$' \t\n' # normal IFS
while read -u 4 LINE; do
# these are just optional. you can directly access the variables if you want
set -- $LINE # or eval "set -- $LINE"
file=$1 path=$2 host=$3 user=$4 pass=$5
# or
array=($LINE)
file=${array[0]} path=${array[1]} host=${array[2]} user=${array[3]} pass=${array[4]}
# do the work here
: ...
# optionally ask user for an input before continuing to the next
: echo press any key to continue
: read -n1 # please check if -n1 is the proper option
done 4< list.txt
Last edited by konsolebox; 09-05-2009 at 02:01 AM.
Reason: code
This at least makes the loop independent of the source, e.g. you could just as easily put your transfers in a CSV and cat it into the loop.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.