The shell script and mysql interaction Method Summarize the shell script to import data files to mysql. Of course, mysqlimport is used. example script: 01 #! /Bin/sh02 # import test_table. SQL into test_database.test_table03 # content in test_table. SQL like "123, abc, 334; N, ddd, 999 "04 2.1605 ### define Login = 192.168.0.6907MYSQL _ USER = root08MYSQL_PW = root09MYSQL_DATABASE = login = test_table11 12 function import_from_file () 13 {14 echo" start importing <$ {MYSQL_TABLE }. SQL> to <$ {MYSQL_IP}/$ {MYSQL_DATABASE}>... "15 echo-n-e" \ t "16 mysqlimport-h $ {MYSQL_IP}-u $ {MYSQL_USER}-p $ {MYSQL_PW} -- fields-terminated-by = ', '-- lines-terminated-by ='; '-L $ {MYSQL_DATABASE }$ {MYSQL_TABLE }. SQL
17 if [0-eq $? ]; Then18 echo "Import completed! "19 www.2cto.com else20 echo" An error occurred while importing. The error code is: $? "21 exit22 fi23 echo24} 25 26 # call function27import_from_file shell script using mysql Command Method 1: 1 #! /Bin/sh2CONNECT_MYSQL = "mysql-h 192.168.0.123-uroot-p123456 test_database" 3SQL = "SELECT * FROM TEST_TABLE" 4 5 echo "$ {SQL}" | $ {CONNECT_MYSQL} method 2: 1 #! /Bin/sh2 www.2cto.com CONNECT_MYSQL = "mysql-h 192.168.0.123-uroot-p123456 test_database" 3SQL = "SELECT * FROM TEST_TABLE" 4 5 echo $ {CONNECT_MYSQL} <MYSQLEOF6 $ {SQL} 7 MYSQLEOF