简体   繁体   中英

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.

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.

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[0]} is the name of the current function itself ( log.as.bandit ); ${FUNCNAME[1]} is the name of the calling function ( bandit14 or bandit15 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM