I have a command I run to check if a certain db exists.
I want to do it locally and via ssh on a remote server.
The command is as so:
我有一个命令,我检查是否存在某个数据库。我想在本地进行,并通过远程服务器上的ssh进行。命令如下:
mysqlshow -uroot | grep -o $DB_NAME
My question is if I can use the same command for 2 variables,
the only difference being ssh <remote-server>
before one?
Something along the lines of !!
variable expansion in the CLI:
我的问题是,如果我可以对2个变量使用相同的命令,唯一的区别是ssh
LOCAL_DB=mysqlshow -uroot | grep -o $DB_NAME
REMOTE_DB=ssh <remote-host> !!
2 个解决方案
#1
1
something like this perhaps?
也许这样的事情?
cmd="whoami"
eval $cmd
ssh remote@host $cmd
eval
will run the command in the string $cmd
locally
eval将在本地字符串$ cmd中运行该命令
also, for checking tables, it's safer to ask for the table name explicitly via a query
另外,对于检查表,通过查询显式地请求表名更安全
SHOW TABLES LIKE 'yourtable';
and for databases:
对于数据库:
SHOW DATABASES LIKE 'yourdb';
#2
0
You can create a function in .bashrc
something like:
您可以在.bashrc中创建一个函数,例如:
function showrdb() {
ssh remote@host "$1"
}
export -f showrdb
and then source .bashrc
and call the function like;
然后源.bashrc并调用函数;
showrdb "command you want to run on remote host"
Or alternately you can create a shell script contains the same function(or only the ssh
line) and call the script as
或者,您可以创建一个包含相同功能(或仅包含ssh行)的shell脚本,并将脚本调用为
./scriptname "command to execute of remote host"
But the level of comfort for me is more in first approach.
但对我来说,舒适程度更多的是第一种方法。
#1
1
something like this perhaps?
也许这样的事情?
cmd="whoami"
eval $cmd
ssh remote@host $cmd
eval
will run the command in the string $cmd
locally
eval将在本地字符串$ cmd中运行该命令
also, for checking tables, it's safer to ask for the table name explicitly via a query
另外,对于检查表,通过查询显式地请求表名更安全
SHOW TABLES LIKE 'yourtable';
and for databases:
对于数据库:
SHOW DATABASES LIKE 'yourdb';
#2
0
You can create a function in .bashrc
something like:
您可以在.bashrc中创建一个函数,例如:
function showrdb() {
ssh remote@host "$1"
}
export -f showrdb
and then source .bashrc
and call the function like;
然后源.bashrc并调用函数;
showrdb "command you want to run on remote host"
Or alternately you can create a shell script contains the same function(or only the ssh
line) and call the script as
或者,您可以创建一个包含相同功能(或仅包含ssh行)的shell脚本,并将脚本调用为
./scriptname "command to execute of remote host"
But the level of comfort for me is more in first approach.
但对我来说,舒适程度更多的是第一种方法。