LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need a one-liner to change one digit in a variable (https://www.linuxquestions.org/questions/linux-general-1/need-a-one-liner-to-change-one-digit-in-a-variable-4175572114/)

brh1 02-12-2016 02:27 PM

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

Habitual 02-12-2016 02:34 PM

What have you tried?

sycamorex 02-12-2016 03:01 PM

Quote:

Originally Posted by brh1 (Post 5499292)
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


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.

brh1 02-15-2016 10:10 AM

Quote:

Originally Posted by sycamorex (Post 5499311)
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.

Thanks for the replies. I will supply more information:

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
Bjoern


All times are GMT -5. The time now is 05:23 AM.