Brady
Brady

Reputation: 10357

How do I append a colon zero, :0 onto the end of an IP address string?

I have a simple Linux shell script that parses a file generated in windows that is shared with a Linux VM. I would like to obtain an IP address from this file and append a colon 0 (:0) on the end, so as to set the DISPLAY environment variable. When I try to do so, I get strange results.

Here is the script:

#!/bin/bash
LOCAL_HOST=`grep IPv4 /mnt/hgfs/share/localip | awk 'NR ==1 {printf "%s\n", $14 }'`

# for some reason, it overwrites the string from the beginning
COLON_ZERO=':0'
export DISPLAY=${LOCAL_HOST}${COLON_ZERO}
echo $DISPLAY

# This obviously works, but doesnt have the COLON_ZERO on the end
echo "export DISPLAY="$LOCAL_HOST

And here is the output of running this script:

# ./display.sh
:09.107.26.25
export DISPLAY=159.107.26.25

# bash --version
GNU bash, version 3.2.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

And just in case its necessary, here is the result of the grep from the file used in the script:

# grep IPv4 /mnt/hgfs/share/localip
   IPv4 Address. . . . . . . . . . . : 159.107.26.25
   IPv4 Address. . . . . . . . . . . : 192.168.223.1
   IPv4 Address. . . . . . . . . . . : 192.168.132.1

Why do I get this strange result when I append the colon zero onto the end of the IP address string? How can I change the script to get the correct result?

Upvotes: 0

Views: 487

Answers (1)

choroba
choroba

Reputation: 241968

Your input file contains Windows line endings (\r\n). Remove the \r character from the ip address.

Upvotes: 2

Related Questions