The shell script reads the values of each data item in the MySQL result set

Source: Internet
Author: User

Under Linux, a shell script reads the values of each data item in the MySQL result set, reads the SQL results in rows, and reads the results of the SQL execution into the shell variable, which can then be processed.

Hostname= "172.16.xx.xx" #数据库信息
Port= "3306"
Username= "Root"
password= "Root"

Dbname= "Log" #数据库名称
Tablename= "Student"


Command1= "Mysql-h${hostname}-p${port}-u${username}-p${password} ${dbname}
Method One:
While Read-a row
Do
echo ". ${row[0]}.. ${row[1]}.. "
done< < (echo "SELECT ID, name from student;" | ${command1})


Method Two:
While read a B
Do
echo ". ${a}.. ${B}: "
done< < (echo "SELECT ID, name from student;" | ${command1})

Note: Notice "done< < ", the first "<" to and "done" no space between, two "<" there is a space between,"<" and "(" there is no space between.

The shell script reads the values of each data item in the MySQL result set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.