Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Execute command withing CLI program

Tags:

linux

bash

shell


I tried searching I couldn't get answer to my question, maybe because I am not sure how to ask it correctly, so I apologize in advance.
I am trying to execute chain of commands in bash script eg:

run mysql > type help > exit

root@ubuntu:# mysql
mysql> help
mysql> exit
root@ubuntu:#

How can I achieve this in bash script?

I have tried operands ||, &&, ; all this eg:

#!/bin/bash
mysql || help || exit

Didn't work. It executes commands after each other.

like image 305
Mike S. Avatar asked Sep 16 '26 19:09

Mike S.


2 Answers

You can use a heredoc to pass some text, which the command will read as if it were a file:

mysql <<EOF
help
exit
EOF

Alternatively, in bash you can use a herestring, which achieves a similar effect to piping commands over standard input without creating any additional subshells:

mysql <<< $'help\nexit'
# or on other shells
printf 'help\nexit\n' | mysql

Note that the exit isn't really necessary, as mysql will exit when it runs out of input anyway.

like image 69
Tom Fenech Avatar answered Sep 19 '26 19:09

Tom Fenech


Try the following:

mysql << EOF
help
exit
EOF

This will start the mysql command then pipe the help and exit commands to mysql's stdin which is effectively the same as typing them directly

like image 23
hardillb Avatar answered Sep 19 '26 19:09

hardillb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!