[英]Use bash alias name in a function that was called using that alias
I'm playing a game in a terminal . 我在码头玩游戏 。 It uses ssh connection and have a different pair of user and password for each level.
它使用ssh连接,并且每个级别都有不同的用户名和密码对。
I use my bash aliases to store those pairs, ie I have an alias for each level where I call a bash function with 2 parameters: password and number of level. 我用我的bash别名存储这些对,即每个级别都有一个别名,在这里我用两个参数调用bash函数:密码和级别数。
function log.as.bandit() {
sshpass -p $1 ssh bandit$2@bandit.labs.overthewire.org
}
alias bandit14="log.as.bandit secretPass 14"
alias bandit15="log.as.bandit differentSecretPass 15"
It would be even easier for me if I could pass as a parameter only password and take the username from an alias that I used. 如果我可以仅将参数作为密码传递并从使用的别名中获取用户名,那对我来说将更加容易。
Question: 题:
Is that possible to use alias name in a function that has been called by that alias? 可以在别名已调用的函数中使用别名吗?
In the example: 在示例中:
function log.as.bandit() {
sshpass -p $1 ssh $HERE_I_DEREFERENCE_THE_ALIAS_NAME@bandit.labs.overthewire.org
}
alias bandit14="log.as.bandit secretPass"
alias bandit15="log.as.bandit differentSecretPass"
As far as I know: no, you can't do that with aliases. 据我所知:不,您不能使用别名来做到这一点。
But what you can do is: 但是您可以做的是:
function log.as.bandit() {
sshpass -p "$1" ssh "${FUNCNAME[1]}"@bandit.labs.overthewire.org
}
bandit14() { log.as.bandit secretPass; }
bandit15() { log.as.bandit differentSecretPass; }
ie use functions instead of aliases. 即使用函数代替别名。
FUNCNAME
is an array containing the names of the currently executing functions. FUNCNAME
是一个数组,其中包含当前正在执行的函数的名称。 ${FUNCNAME[0]}
is the name of the current function itself ( log.as.bandit
); ${FUNCNAME[0]}
是当前函数本身的名称( log.as.bandit
); ${FUNCNAME[1]}
is the name of the calling function ( bandit14
or bandit15
). ${FUNCNAME[1]}
是调用函数的名称( bandit14
或bandit15
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.