我想在bash(.bashrc)中使用类似的方式,因此要根据用户登录的comp设置别名。我不知道如何从10.0.0.210中获取210,然后再通过最佳方式列表“user = xxx”

$radek ='210'
$mike ='209'


#SSH_CLIENT='10.0.0.210 53039 22'  <--- system variable
$user = based on the 4th part of IP so
   $user = radek if 210
   $user = mike if 209

alias sites='cd /var/lib/code/'+$user

所以最终的别名看起来像

'cd/var/lib/code/ radek '(如果从 210 计算机登录)

'cd/var/lib/code/ mike '(如果从 209 计算机登录)

最终代码归功于@Dennis Williamson
users[210]=radek
users[209]=mike

octet=($SSH_CLIENT)    # split the value on spaces
#octed=${octet[0]##*.}        # extract the last octet from the ip address
alias sites='cd /var/lib/code/'${users[${octet[0]##*.}]}

最佳答案

试试看:

users[210]=radek
users[209]=mike

octet=($SSH_CLIENT)    # split the value on spaces
octet=${octet[0]##*.}  # extract the last octet from the ip address
alias sites='cd /var/lib/code/'${user[octet]}

分配用户的另一种方法:
names=(bob jim anne kelly rick)
octet=211
for name in ${names[@]}
do
    users[octet++]=$name
    if (( octet > 255 ))
    then
        echo "Error: range limit exceeded"
        break
    fi
done

08-28 05:30