bash loop mysql output multiple to variables -
new programming , need little help.
i need access mysql database , loop thru ‘ftpuser’ , ‘domain’ column give me output below
echo 2>&1 " 3cmd put -r /home/$ftpuser/backups/ s3://bucket-backup/$domainname/"
when run shell script below outputs ever user each domain
3cmd put -r /home/user1/backups/ s3://bucket-backup/domain1.com/ 3cmd put -r /home/user1/backups/ s3://bucket-backup/domain2.com 3cmd put -r /home/user1/backups/ s3://bucket-backup/domain3.com/ 3cmd put -r /home/user2/backups/ s3://bucket-backup/domain1.com/ 3cmd put -r /home/user2/backups/ s3://bucket-backup/domain2.com/ 3cmd put -r /home/user2/backups/ s3://bucket-backup/domain3.com/
and on
the result im looking
3cmd put -r /home/user1/backups/ s3://bucket-backup/domain1.com/ 3cmd put -r /home/user2/backups/ s3://bucket-backup/domain2.com/ 3cmd put -r /home/user3/backups/ s3://bucket-backup/domain3.com/ 3cmd put -r /home/user4/backups/ s3://bucket-backup/domain4.com/
here code
!/bin/bash
filelines=username=$(mysql -h localhost -u root -p -ss -e "use users; select ftpuser users; ") filelines2=username=$(mysql -h localhost -u root -p -ss -e "use users; select domain users; ") echo start username in $filelines ; domainname in $filelines2 ; echo 2>&1 " 3cmd put -r /home/$username/backups/ s3://bucket-backup/$domainname/" done done
store elements of filelines
, filelines2
in separate bash arrays , loop once through index shown.
arr1=($filelines) arr2=($filelines2) in ${!arr1[@]} echo 2>&1 " 3cmd put -r /home/${arr1[$i]}/backups/ s3://bucket-backup/${arr2[$i]}/" done
output:
3cmd put -r /home/user1/backups/ s3://bucket-backup/domain1/ 3cmd put -r /home/user2/backups/ s3://bucket-backup/domain2/ 3cmd put -r /home/user3/backups/ s3://bucket-backup/domain3/ 3cmd put -r /home/user4/backups/ s3://bucket-backup/domain4/
you can refer looping on arrays, printing both index , value understanding looping syntax.
Comments
Post a Comment