Bash: How to invoke command and store the result i

2020-02-08 07:12发布

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?

标签: mysql bash shell
2条回答
唯我独甜
2楼-- · 2020-02-08 07:56
$ A=$(mysql -uanon -ppwd -db mydb -e "select count(*) from table1")
$ echo $A

In other words, use the $() syntax.

查看更多
SAY GOODBYE
3楼-- · 2020-02-08 08:01

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";)
查看更多
登录 后发表回答