Get MySQL cell in shell script

I would like to execute the MySQL command in a script / cron shell job that returns a dynamic number of rows in which I can access a specific field from these rows. Then I would like to skip this execution of additional commands in these field entries.

Here are my two questions:

  • How to return a set of rows (ideally only one cell in each row) into a script shell variable?

  • Can I write a PHP script that returns the information I need and then save it in a script shell variable? If so, how do I run a PHP script from the shell and return it?

+3
source share
4 answers
echo 'select some_column from some_table' | mysql -uusername -ppassword some_db | tail -n+2

A few important things to consider here:

  • -p . , , . : , , -ppassword - . , , , , , FreeBSD.

  • "" , . , +2.

  • "count rows" , , "select count (*) from some_table".

+4

PHP script? /path/to/php mycron.php cronjob shellscript.

+1

- , bash:

mysql -b -e "SELECT one_column FROM tables" | {
  read
  while [ ! -z "$REPLY" ]
  do
    # do something useful with the $REPLY variable here
    read
  done
}

(, )

, xargs:

mysql -b -e "SELECT one_column FROM tables" | xargs usefulcommand

.

+1

, mysql: Expect

script, script, , .

0

Source: https://habr.com/ru/post/1716271/


All Articles