How to store Mysql Database Field value to Shell Script variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xtremebass
    New Member
    • Nov 2008
    • 38

    How to store Mysql Database Field value to Shell Script variable

    Hi Bytes.. i have need to assign Mysql Database DataField value to shell script variable .

    is it possible ? i have tried like this .. got error..
    Code:
    #!bin/sh
    
    password=xxx
    mysql -u root -p$password -h 192.168.1.8 << y
    use dbssys;
    d ="`select mem from profile` ";
    echo $d
    quit
    y
    Got Output like this..

    test.sh: command substitution: line 1: syntax error near unexpected token `from'
    test.sh: command substitution: line 1: `select mem from profile'
    ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd =" "' at line 1
    Last edited by Nepomuk; Jan 5 '09, 11:23 PM. Reason: The code tags are [code], not <code>.
  • micmast
    New Member
    • Mar 2008
    • 144

    #2
    The problem is that the backticks will execute the code within and select mem from profile is no valid linux bash/shell command. Try escaping them with a \

    Comment

    • xtremebass
      New Member
      • Nov 2008
      • 38

      #3
      i cant get you clearly.. if you dont mind..can you please correct that particular line and send me ..

      Comment

      • micmast
        New Member
        • Mar 2008
        • 144

        #4
        micmast@plato:~ $ d="\`select mem from profile\`";
        micmast@plato:~ $ echo $d
        `select mem from profile`

        Comment

        • xtremebass
          New Member
          • Nov 2008
          • 38

          #5
          Hi i didnt solve the problem yet.. after applying changes that you have said.. any other solution for this..

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Why canyou use isql or fsql to connect to the DB and get the values to the variable you expect?

            raghu

            Comment

            • xtremebass
              New Member
              • Nov 2008
              • 38

              #7
              i am not using fsql or isql for my project. actually i am doing Linux system oriented project and storing the values in Mysql . i need to compare the values that stored in mysql and recent values get from the linux .. so thats why i am looking for a solution? can u suggest some hint ? method /way proceed further??

              Comment

              • ashitpro
                Recognized Expert Contributor
                • Aug 2007
                • 542

                #8
                Try using this line...

                result=$(mysql -u $user_name $db_name -p$password -sN -e “select mem from profile”)

                Comment

                • stmarc23
                  New Member
                  • Dec 2009
                  • 1

                  #9
                  In a script just write this, and play with the mysql options to get what you want.

                  SHELLVARIABLE=` mysql --skip-column-names dataBase -e "SELECT COUNT(*) from TableAAA"`

                  and then u can do somethink like

                  ehco $SHELLVARIABLE

                  This is a tip.

                  Bye.

                  Comment

                  Working...