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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With