Solved

how to pass mysql command in  a shell variable

Posted on 2013-12-26
3
558 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
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now