How to print last character of hostname and assign to a variable ?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
How to print last character of hostname and assign to a variable ?

Hi

How to pass echo output to a variable ?

Does below awk command will get the last character of hostname and assign to a variable - "svr" ?

Code:
svr=$( echo `hostname` | awk '{print substr($0,length,1)}' )

Thanks.
Moderator's Comments:
Mod Comment Please use CODE tags when dsplaying code segments, sample input, and sample output.

Last edited by Don Cragun; 04-16-2019 at 10:37 AM.. Reason: Add CODE and ICODE tags.
# 2  
Yes it works.
Without the echo command it simplifies to
Code:
 svr=$( hostname | awk '{ print substr($0,length,1) }' )

or apply two variable modifiers
Code:
h=$(hostname); t=${x%?}; svr=${h#$t}

% chops from the end. ? is one character.
# chops from the beginning. $t is from the previous assignment.
# 3  
Hello Lim,

Welcome to forums, the BEST way to know any command's output is run it, you could have run it in your box.
You were close, try following to get last character of hostname.

Code:
hostname | awk '{print substr($0,length($0))}'

To save it in a variable use(note that backticks are depreciated so use $ instead to store values into variables too):
Code:
var=$(hostname | awk '{print substr($0,length($0))}')

Thanks,
R. Singh
# 4  
Most if not all awk versions have length() default to $0.
So you can say length() and even length.
Likewise, in substr() the 3rd parameter defaults to "unlimited", so can be omitted here, because there is only 1 character left.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Quote:
Originally Posted by MadeInGermany
... or apply two variable modifiers
Code:
h=$(hostname); t=${x%?}; svr=${h#$t}

% chops from the end. ? is one character.
# chops from the beginning. $t is from the previous assignment.
No need to use temp variable:

Code:
$ HN=$(hostname)
$ echo ${HN#${HN%?}}

This User Gave Thanks to RudiC For This Post:
# 7  
Quote:
Originally Posted by Lim
Thanks all
Hello Lim,

Appreciate that your are trying to encourage people by thanking them in a post, very good. For THANKING people we do have a mechanism called "THANKS" button at side of each posts's left corner, so you could HIT it for any post which you LIKE/which was helpful.

Thanks for posting in UNIX & LINUX forums, keep learning and keep sharing on this great site, cheers.

Thanks,
R. Singh
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Remove a character and assign result to a variable
KathyB148
I am reading lines from a file that contain a number sign (#) before a three or four digit number: #1043 #677 I can remove the '#' and get just the number. However, I then want to assign that number to a variable and use it as part of a path further on in my program: /mydir/10/1043 for...... Shell Programming and Scripting
5
Shell Programming and Scripting
Assign a variable the nth character of a string.
klarue
I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype.... Shell Programming and Scripting
10
Shell Programming and Scripting
print ip to hostname with array
netxus
Hi Anyone can help me??i writing a perl using array to translate ip address to the hostname my script are below.this line print "$myhost{$ipaddr}\n"; blank :mad:hostname dint come out:mad::mad: #!/usr/local/bin/perl @ipaddr=("192.1.168.2","172.25.1.13","129.1.2.5"); %myhost = { 192.1.168.2...... Shell Programming and Scripting
4
Shell Programming and Scripting
Print the first four characters of hostname
yxian
Hey, I'm trying to print the first four characters of the hostname of a computer. I can get it from using: hostname -s | sed 's/...........$//'" but this is when I know how many characters are in the computer name. I dont understand why some like: hostname -s | sed '/..../p' wont...... Shell Programming and Scripting
7
Shell Programming and Scripting
read and assign each character from the string to a variable
Tek-E
How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you!... Shell Programming and Scripting
1
Shell Programming and Scripting