2

Each line of /etc/trueuserdomains contains a username and its associated domain. I am trying to do ln -s /home/$user /var/www/vhosts/$domain for each line. I'm able to define two arrays with the required information. However, I am having trouble iterating through the array in the proper order.

#!/bin/bash

domain=(`cat /etc/trueuserdomains | cut -d: -f1`)
user=(`awk '{print $2}' /etc/trueuserdomains`)

for x in "${domain[@]}"
 do
  for y in "${user[@]}"; do
   ln -s /home/$y /var/www/vhosts/$x
  done
done

This repeats a single domain for each username, i.e.:

domain1.com user1
domain1.com user2
domain1.com user3

Close, but I need:

domain1.com user1
domain2.com user2
domain3.com user3

So basically loop through the domain array using the user array in the proper order. Any ideas? It sounds like associative arrays may help but unfortunately this server has only Bash 3, which does not support them. Thanks in advance!

3
  • Yet another one of those seemingly simple tasks that take far too much shell black magic. Is switching to Perl or Python an option for you? Commented Nov 5, 2011 at 19:05
  • Wow thanks for the quick response. I also was just looking at stackoverflow.com/questions/3288206/… and came up with this that works as well: domain=(cat /etc/trueuserdomains | cut -d: -f1) user=(awk '{print $2}' /etc/trueuserdomains) for x in "${!domain[@]}" do ln -s /home/"${user[$x]}" /var/www/vhosts/"${domain[$x]}" done Commented Nov 5, 2011 at 19:08
  • See my reply: you don't need all those external commands for this task. Commented Nov 5, 2011 at 19:09

2 Answers 2

5

Am I missing something:

while IFS=:\  read -r d u; do
  echo ln -s /home/"$u" /var/www/vhosts/"$d"
done < /etc/trueuserdomains

Remove echo when happy.

Sign up to request clarification or add additional context in comments.

Comments

3

Perhaps it is simplest to use an index:

for (( i=0; i<${#domain[*]}; i=i+1 ))
do
    ln -s "/home/${user[$i]}" "/var/www/vhosts/${domain[$i]}"
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.