我想用shell脚本执行mysql数据库操作,然后把结果返回一个变量  例如
  例1:    
  #! bin/sh
  # 当然一下脚本是错误的,我只是想表达一下我的意思  test=mysql -e "use DA_NAME; select id from table_name"  # 里面就一行数据
  echo $test
  例2:
  
 
  #! bin/sh
  # 当然一下脚本是错误的,我只是想表达一下我的意思  test=mysql -e "use DA_NAME; select id from table_name"  # 里面有多行数据
  echo $test
  例3:  
  #! bin/sh
  # 当然一下脚本是错误的,我只是想表达一下我的意思  test=mysql -e "use DA_NAME; select id,name from table_name"  # 返回是的2列
  echo $test   

解决方案 »

  1.   

    这样是传不到变量$test里头的。
    不过,可以这样模拟着做:
    mysql -e "use DA_NAME; select 'export test=' + id from table_name" > export_test.sh
    source export_test.sh
    echo $test
      

  2.   


    #!/usr/bin/env python
    #coding=utf-8
    ###################################
    # @author migle
    # @date 2010-01-17
    ##################################
    #MySQLdb ʾÀ
    #
    ##################################
    import MySQLdb#½¨bºÍýµͳµÄ¬½Ó
    conn = MySQLdb.connect(use_unicode = 1,charset='utf8',host='localhost', user='root',passwd='123')#»ñÙ÷ê
    cursor = conn.cursor()
    #ִÐSQL,´´½¨һ¸öݿâ
    #cursor.execute("""create database python """)
    cursor.execute('use python')
    cursor.execute('select * from t1')
    rows=cursor.fetchall()
    print rows
    for row in rows:
            print row[0],row[1]
    cursor.close()
    conn.close()用PYTHON可以这么写。
      

  3.   

    python简直是万能器啊。呵呵。
      

  4.   

    不用也可以的
    rf='mktemp'
    mysql ..... -e"select.."  > $rf 2 > &1jio=`grep keyword $rf | cut -d: -f2`