Solved

how to pass mysql command in  a shell variable

Posted on 2013-12-26
3
581 Views
Last Modified: 2013-12-26
for i in "${cmd_array[@]}"
        do
                ${i} >> $logFile
        done

THe variable cmd_array has many elements . One of the element is a mysql command.
It is defined as :
cmd_array[1]='mysql -uroot -prootDB -e "show full processlist" | grep Query  | grep -v "show full processlist'

THis command fails to run. When i executed the script using "sh -x " , i am able to see his command is being executed as
mysql -uroot -prootDB -e '"show' full 'processlist"' '|' grep Query '|' grep -v '"show' full processlist

LIMITATION: I can't handle this command separately inside the for loop, as it is a generic code. How do i form the mysql query so that it gets executed.

I have another command [default command] which has no issues:
cmd_array=('cat /proc/${process_pid}/status')
0
Comment
Question by:pvinodp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39739822
Try prefixing the command with "eval":

cmd_array[1]='eval mysql -uroot -prootDB -e "show full processlist" | grep Query  | grep -v "show full processlist"'

The last double quote is missing in your post - I assume that's a typo here and not in the original code?

If the above doesn't work for whatever reason you could prefix all commands with "eval". The commands which already run without issues will also run with "eval".

...
         do
                eval ${i} >> $logFile
        done
...
0
 

Author Comment

by:pvinodp
ID: 39739884
yes that was a typo. But my code does have the double quotes.
Thanks it worked. THere was no need to have the eval inside for loop.
0
 

Author Closing Comment

by:pvinodp
ID: 39739890
thanks
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question