I have this command which returns an IP successfully:
user@laptop:~$ systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/, 4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}'
172.22.0.1
I am trying to concatenate and export an environmental variable DISPLAY
using a script with this content:
LOCAL_IP=$(systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/, 4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}')
export DISPLAY=$LOCAL_IP:0
But after this script runs, DISPLAY
doesn't look like expected:
user@laptop:~$ echo $DISPLAY
:02.22.0.1
I was expecting an answer 172.22.0.1:0
. What went wrong?
CodePudding user response:
LOCAL_IP
appears to have a trailing \r
; od -c <<< "${LOCAL_IP}"
should show the value ending in a \r
One fix using parameter substitution:
$ export DISPLAY="${LOCAL_IP//$'\r'/}:0"
$ echo "${DISPLAY}"
172.22.0.1:0
Another option would be to add an additional pipe on the end of OP's current command, a couple ideas (dos2unix
, tr -d '\r'
); 3rd option modifies the awk
script to remove the \r
:
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/, 4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}' | dos2unix
# or
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/, 4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}' | tr -d '\r'
# or
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/, 4p' | egrep --word-regexp '\[01\]:' | awk '{gsub(/\r/,"");print $2}'
Another option would be to replace the sed/egrep/awk/tr
with a single awk
call. If OP wants to go this route I'd recommend asking a new question, making sure to provide the complete output from systeminfo.exe
to better understand the parsing requirements.