Execute MYSQL in a bash script

0

Greetings community I hope you are well.

I have a problem, I want to run a few MYSQL statements but as BASH , I need to run this on ubuntu.

I have created a file called scripts.sh where I have placed the sentences to execute:

  #!/bin/bash   
  mysql -u Prueba -p123456 <<EOF 
  use BDPrueba; 
  delete from positions where servertime between '2017-09-01' and '2017-09-30';  
  EOF

The problem is that when I try to execute this, he says:

"No such file or directory"

Seeing that I have other bash-type scripts for other operations, I decided to try changing the #! / bin / bash to #! / bin / sh and when I did it it says:

"Can not open"

I have already read thousands of forums and thousands of ways, but the truth is that I see that the way to do it is how I have done it. I hope you can help me see what is wrong.

Thank you.

    
asked by jose angarita 17.12.2018 в 22:51
source

3 answers

4

I think there is a problem with permissions. You need chmod +x escripta.sh to access and execute the script.

But, to use mySQL statements in a bash script, I think you should write the sentences on a different side.

For example

example.sql

use BDPrueba; 
delete from positions where servertime between '2017-09-01' and '2017-09-30';  

script.sh

#!/bin/sh 
mysql -u Prueba -p 123456 BDPrueba < ejemplo.sql

E

And to use the script, you need to type the command in the terminal like this:

./script.sh

    
answered by 17.12.2018 в 23:18
3

The problem here is that you're trying to run mysql commands but in bash.

In the first line of your script you put:

mysql -u Prueba -p123456 <<EOF 

With what I imagine you are thinking that now the following commands would be executed within the console of mysql , which is something wrong.

The commands bash or sh only execute those commands that can be run on your terminal, and not within other applications, for this the applications must offer a way to execute these commands from terminal, and the mysql client offers it:

mysql --user="Prueba" --password="123456" --database="BDPrueba" --execute="delete from positions where servertime between '2017-09-01' and '2017-09-30';"
    
answered by 19.12.2018 в 16:22
0

If you mention the error "No such file or directory" is as it is, because your shell interpreter does not find the file or directory you wrote.

If you put something like:

$ mi_programa

Before a command is executed, a redirect is performed (with ""). Then following the sequence used by the shell, which is this.

  • Find the alias.
  • Parameter expansion, command substitution, arithmetic expansion and elimination of quotation marks before being assigned to the variable.
  • Check if it's a shell function mi_programa(){...}
  • Check if it's a BUILTIN.
  • Check if you are in the hash tables . hash | less
  • Check the PATH variable (if it does not find route elements in its command name)
  • If all else fails, the error message of the style "command not found" appears, which seems to be the error that appears to you.

    Now, if we focus on point 6 of the order of execution, you have to have your command (or a symbolic link) mi_programa in a folder in variable PATH (revise with echo $PATH ) as long as This command has no route elements , which are '/', '.', '..'. If you have route elements, look for that route, for example.

    $ ls -la 
    rwxrwxrwx 1 u1 u1 512 Nov 24 19:27 .
    rwxrwxrwx 1 u1 u1 512 Nov 24 19:27 ..
    rwxrwxrwx 1 u1 u1 512 Nov 24 19:27 mi_programa
    $ pwd
    /home/u1/carpeta1
    $ /home/u1/carpeta1/mi_programa
    (se ejecuta lo que tenga tu programa)
    

    In this case, you executed your program because you wrote it with the route elements that are the diagonals' /'.

    Now, in case you use the route elements like the point '.' or the two points '...' which are names that are linked with hard links to the folder itself and to its ancestor, respectively. In other simplistic words, '.' refers to the current folder and '..' to its ancestor. Then you can run your program in the same way with

    $ ./mi_programa
    

    Or with.

    $ ../carpeta1/miprograma
    

    They would have to do the same to you.

    With these two things it should be enough to fix this problem, but I had to explain the entire preamble first to explain why the error message.

    All this is assuming they have execution permissions. That is, if writing ls to my_program, it has something of the style.

    $ ls -la mi_programa
    -r-xr-xr-x 1 u1 u1  25 Dec 18 21:59 mi_programa
    # Con "r" de permisos de lectura (read) para que la shell lo pueda leer y con
    # "x" de permisos de ejecución para que tu shell pueda permitir utilizarlo como
    # ejecutable.
    

    In case you do not have permissions, you can use something in one of the ways.

    $ chmod a+x mi_programa
    $ chmod u+x mi_programa
    $ chmod +x mi_programa
    $ chmod 755 mi_programa
    

    But I doubt that I have this problem but I would give you an error of type "permission denied", but not "file not found".

    Clarification with the shebang

    What you did to change the shebang just changed the program that the shell would run with.

    That is to say, when putting the first line #!/programa you indicate to the shell with what program you are going to execute the content of that file.

    For example. Suppose you have the files a1 and a2 in the same folder, a2 has as content.

    contenido
    de archivo a2
    

    And in a1 you have this.

    #!/bin/cat a2
    

    Modifying the permissions so that it is executed.

    $ chmod u+x a1
    $ ./a1
    contenido
    de archivo a2
    #!/bin/cat a2
    

    Here, with the shebang of #!/bin/cat you made use of the cat program. On the other hand, in the case of bash or sh, you use /bin/bash and /bin/dash , this is dash , it is not a typo.

    In other words, changing the shebang to your program does not affect anything if the error you throw is "No such file or directory".

        
    answered by 19.12.2018 в 06:23