2 added 12 characters in body
source | link

You can use the read builtin and tell it that it should split the line on commas using the IFS (input field separator) shell variable:

$ cat file 
user1,host1,pass1
user2,host2,pass2

$ while IFS="," read -r user host pass; do echo "$user:$host:$pass"; done < file 
user1:host1:pass1
user2:host2:pass2

So, in your script, you would want something like:

while IFS="," read -r user host pass; do 
    if mysql -h $host"$host" -u $user"$user" -p$passp"$pass" -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

The above, however, will break if any of your passwords (or other variables, but I assume only the passwords might have this issue) contain a comma. If that can be a problem for you, you will have to change the separator to something else instead of a comma. Something that will never appear in a password. For example a tab, and then you can do:

while IFS=$'\t' read -r user host pass; do 
    if mysql -h $host"$host" -u $user"$user" -p$passp"$pass" -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

You can use the read builtin and tell it that it should split the line on commas using the IFS (input field separator) shell variable:

$ cat file 
user1,host1,pass1
user2,host2,pass2

$ while IFS="," read -r user host pass; do echo "$user:$host:$pass"; done < file 
user1:host1:pass1
user2:host2:pass2

So, in your script, you would want something like:

while IFS="," read -r user host pass; do 
    if mysql -h $host -u $user -p$pass -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

The above, however, will break if any of your passwords (or other variables, but I assume only the passwords might have this issue) contain a comma. If that can be a problem for you, you will have to change the separator to something else instead of a comma. Something that will never appear in a password. For example a tab, and then you can do:

while IFS=$'\t' read -r user host pass; do 
    if mysql -h $host -u $user -p$pass -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

You can use the read builtin and tell it that it should split the line on commas using the IFS (input field separator) shell variable:

$ cat file 
user1,host1,pass1
user2,host2,pass2

$ while IFS="," read -r user host pass; do echo "$user:$host:$pass"; done < file 
user1:host1:pass1
user2:host2:pass2

So, in your script, you would want something like:

while IFS="," read -r user host pass; do 
    if mysql -h "$host" -u "$user" -p"$pass" -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

The above, however, will break if any of your passwords (or other variables, but I assume only the passwords might have this issue) contain a comma. If that can be a problem for you, you will have to change the separator to something else instead of a comma. Something that will never appear in a password. For example a tab, and then you can do:

while IFS=$'\t' read -r user host pass; do 
    if mysql -h "$host" -u "$user" -p"$pass" -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file
1
source | link

You can use the read builtin and tell it that it should split the line on commas using the IFS (input field separator) shell variable:

$ cat file 
user1,host1,pass1
user2,host2,pass2

$ while IFS="," read -r user host pass; do echo "$user:$host:$pass"; done < file 
user1:host1:pass1
user2:host2:pass2

So, in your script, you would want something like:

while IFS="," read -r user host pass; do 
    if mysql -h $host -u $user -p$pass -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file

The above, however, will break if any of your passwords (or other variables, but I assume only the passwords might have this issue) contain a comma. If that can be a problem for you, you will have to change the separator to something else instead of a comma. Something that will never appear in a password. For example a tab, and then you can do:

while IFS=$'\t' read -r user host pass; do 
    if mysql -h $host -u $user -p$pass -e exit; then 
        echo "Connection established"
    else 
        echo "Connection failed" fi; 
done < file