开发者

Bash: How to invoke command and store the result in a variable?

开发者 https://www.devze.com 2022-12-29 00:28 出处:网络
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 co开发者_如何学编程mmands result (the count on th

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 co开发者_如何学编程mmands result (the count on that table) and place it in a variable in bash script. What is the simplest way to achieve this?


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";)


$ A=$(mysql -uanon -ppwd -db mydb -e "select count(*) from table1")
$ echo $A

In other words, use the $() syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消