Basically I want to be able to invoke a given command, in this case mysql -uanon -ppwd -db mydb -e "select count(*) from table1"
. And then take this commands result (the count on that table) and place it in a variable in bash script. What is the simplest way to achieve this?
views:
216answers:
3
+1
A:
Use backticks to execute command:
variable=`mysql -uanon -ppwd -db mydb -e "select count(*) from table1"`
Ivan Nevostruev
2010-04-28 15:17:10
This is how its done
kSiR
2010-04-28 16:48:17
[`$()` is preferred](http://mywiki.wooledge.org/BashFAQ/082)
Dennis Williamson
2010-04-29 07:42:13
+1
A:
$ A=$(mysql -uanon -ppwd -db mydb -e "select count(*) from table1")
$ echo $A
In other words, use the $() syntax.
unwind
2010-04-28 15:17:51
+3
A:
You most likely want to use batch mode (-B) and disable column names (--disable-column-names) for non-interactive mysql output:
out=$(mysql -B -db mydb -uanon -ppwd --disable-column-names -e "select count(*) from table1";)
Jürgen Hötzel
2010-04-28 15:33:08