2

I just wanted to execute this bash command in expect script:

mysql -u root -h localhost -proot dbTest < temp.sql

I added spawn to the beginning but it is not working. I think "<" symbol means nothing in expect!

Can anyone help me to solve this issue?

Kh.Taheri
  • 946
  • 1
  • 10
  • 25

3 Answers3

2

spawn does not support the < direction but you can do like this:

spawn sh -c "mysql -u root -h localhost -proot dbTest < temp.sql"

Seems like you want to run mysql in the non-interactive way so you can also use Expect's system command:

system "mysql -u root -h localhost -proot dbTest < temp.sql"

or Tcl's exec command:

exec mysql -u root -h localhost -proot dbTest < temp.sql >@ stdout 2>@ stderr

You may need to put the whole system or exec command in a catch block in case the mysql fails:

catch {system "mysql ..."} catched
# or
catch {exec mysql ...} catched
pynexj
  • 19,215
  • 5
  • 38
  • 56
1

People tend to use sqldump and mysqldump. I like the quote in the below link saying rubbish with phpmyadmin:

how to import a very large query over phpmyadmin?

Concerning cron or expect ...

I know cron has trouble say with dates and often what works at a command line has to be shoved in a bash script for easier work. Then cron runs the .sh script versus embedded string.

Also it is a duplicate question of yours from 2 hours ago.

Community
  • 1
  • 1
Drew
  • 24,851
  • 10
  • 43
  • 78
  • Thank you so much again, yes I wrote it again in better way. well, you are right, I can make a .sh file with command and call it from expect. It is actually long way but I will try it. – Kh.Taheri Jul 06 '15 at 01:19
  • Look into tcl braces {} maybe around the `<` sign I don't know. Good luck – Drew Jul 06 '15 at 01:22
  • Unfortunately, it is not working. However, I got two more correct answers to this question. thanx again – Kh.Taheri Jul 06 '15 at 01:58
0

I have now found a solution for this problem. I avoided the "<" symbol, so we can use this command instead:

spawn mysql -u root -h localhost -proot dbTest -Bse "source temp.sql"
Kh.Taheri
  • 946
  • 1
  • 10
  • 25