Need a one-liner to change one digit in a variable
Good day.
I need advice with assigning a partner hostname to a variable based on the current hostname, using standard tools like awk, sed, cut or tr? Ie: if the current hostname is LM5SE-J00C01H0, I would like to assign a partner hostname to a new variable, ie: $PARTNER becomes LM5SE-J00C02H0, and visa versa, if $HOSTNAME=LM5SE-J00C02H0, $PARTNER becomes LM5SE-J00C01H0. Thanks in advance, Bjoern |
What have you tried?
|
Quote:
As mentioned above, what have you tried? Apart from that, are these the only two possible hostnames? If so, you don't need any of the above mentioned tools, just a conditional statement. If there are more possible hostnames, your question does not provide enough details. |
Quote:
When I log into any of our systems, I use a virtual IP address to access either of 2 nodes depending which one is the master node at the time. There are times I would like to ssh to the partner node and therefore I have to determine which node I'm on before accessing the partner. However, the partner isn't clearly defined in the /etc/hosts file. Also, the hostnames could be any combination of letters and numbers around the '01' and '02' with the 'H0' on the end the only unchanged pair. For example LM5SE-J00C01H0 and LM5SE-J00C02H0 could be SH2VF-K00C01H0 and SH2VF-K00C02H0 on another system. I think I did find a solution (seems a weekend R & R helps sorting the brain cells): Code:
PREFIX=`hostname|cut -c1-11`;THISNUM=`hostname|cut -c 12`;case "$THISNUM" in 1 ) PARTNUM=2 ;; * ) PARTNUM=1 ;; esac ; PARTNER=$PREFIX$PARTNUM"H0" ; ssh $PARTNER |
All times are GMT -5. The time now is 05:23 AM. |