• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

how to pass mysql command in a shell variable

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
pvinodp
Asked:
pvinodp
  • 2
1 Solution
 
woolmilkporcCommented:
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
 
pvinodpAuthor Commented:
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
 
pvinodpAuthor Commented:
thanks
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now