We know we make the entry of variable as using read , I want to make the entry in file same as read, Means When I execute the script , it should ask the file to make entry, I copy paste the entries in file , further any this file execute in mysql
Normal step we do
> file.sql
vim file.sql --- make entry of query
then in script mysql -e "source file.sql"
Want to skip the manualy entry in file.sql ,
sh script.sh
ask for entry in file.sql
Make entry in file.sql
mysql -e "file.sql"
And programming which part of it do you have problems with? Show the code you've written so far to resolve this. Also, specify if the SQL file name is supposed to be fixed (unchangeable) or also provided as user input, and if you intend to put only a single SQL query or multiple SQL queries in the file.
I am guessing your script must look something like this:
#!/bin/bash
echo "Please enter your SQL commands. Press Ctrl-D when finished:"
cat > file.sql
echo "Executing SQL commands in file.sql..."
mysql -e "source file.sql"
The problem with entering any multi-line input at the terminal is that only the last line is editable (using BackSpace). If you make any error in the input which is not corrected immediately, you have to Ctrl-C out of the whole thing and start over.
I would recommend writing a script that validates user input, and constructs the appropriate commands from that. You can make menus for the type of command, table names, and column names dynamically by querying the database itself.
Well, you might want to open an editor from the script and paste into that space. Then you can edit and save.
Or you might populate the file using cat and then open vi to edit the file.
Or you might read all the lines into the file via an do - while shell cycle.
Or, if the file you want to use us mostly the same, you just need to edit some values in it, you can have a template file and edit the values via sed or awk. Having more complex situations, you can use Jinja templating in Python or Ansible.
If you give some more info, I can give some more specific answer.