To develop a scheduled task today, then join the cron list. But there is a problem in front of the script in the execution of the need to enter the database password:
[Plain]View Plain copy
- Mysql-u root-p << SQL
- Use DB;
- Set names UTF8;
- ' Cat Task.sql '
- Sql
After running the above code, you will be prompted:
[Plain]View Plain copy
- Enter Password:
After searching, we found that expect can help us to do this thing:
Save the above code in a file: db.sh, and write the following code:
[Plain]View Plain copy
- #!/usr/bin/expect-f
- Spawn db.sh
- Expect "Enter Password:"
- Send--"password\n"
- Interact
Then we will go into the database interface after we run it.
The first line indicates which program the script will use to run;
The second line is the focus, under the expect launched a program we want to implement;
The third line is also the focus, we want to tell expect, when what information appears need expect help us to complete the interaction;
The Forth line is the input password prompt, the content to send;
The meaning of line five is to keep the interaction and don't quit. Without this sentence, the script will exit immediately after it has successfully logged into the database.
http://blog.csdn.net/topasstem8/article/details/17920127
Examples of interactive behaviors such as Automatic password entry for Linux under the background program