LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Cut adding $ and carriage return characters for MAC addresses (https://www.linuxquestions.org/questions/linux-software-2/cut-adding-%24-and-carriage-return-characters-for-mac-addresses-757642/)

DaveQB 09-24-2009 10:13 PM

Cut adding $ and carriage return characters for MAC addresses
 
I have go a different way with this script, but very curious about this.


First the script I am using
Code:

cat mac.sh
#!/bin/bash -x


ifconfig eth0| grep HWaddr |tr -s ' ' |cut -d' ' -f5
MAC=`ifconfig eth0| grep HWaddr |tr -s ' ' |cut -d' ' -f5`

grep "$MAC"  maclist.txt|tr -s " " |cut -d" " -f1
HO1=`grep "$MAC"  maclist.txt |tr -s " " |cut -d" " -f1`
printf ${HO1}

grep "$MAC"  maclist.txt |tr -s " " |cut -d" " -f2
HO2=`grep "$MAC"  maclist.txt |tr -s " " |cut -d" " -f2`
printf ${HO2}

A='00:23:AE:69:C0:F2'
printf  ${A}

exit 0


And the output:

Code:

./mac.sh
+ ifconfig eth0
+ grep HWaddr
+ tr -s ' '
+ cut '-d ' -f5
00:23:AE:69:C0:F2
++ ifconfig eth0
++ grep HWaddr
++ tr -s ' '
++ cut '-d ' -f5
+ MAC=00:23:AE:69:C0:F2
+ grep 00:23:AE:69:C0:F2 maclist.txt
+ tr -s ' '
+ cut '-d ' -f1
om012274
++ grep 00:23:AE:69:C0:F2 maclist.txt
++ tr -s ' '
++ cut '-d ' -f1
+ HO1=om012274
+ printf om012274
om012274+ grep 00:23:AE:69:C0:F2 maclist.txt
+ tr -s ' '
+ cut '-d ' -f2
00:23:AE:69:C0:F2
++ grep 00:23:AE:69:C0:F2 maclist.txt
++ tr -s ' '
++ cut '-d ' -f2
+ HO2=$'00:23:AE:69:C0:F2\r'
+ printf $'00:23:AE:69:C0:F2\r'
+ A=00:23:AE:69:C0:F2
+ printf 00:23:AE:69:C0:F2
00:23:AE:69:C0:F2+ exit 0


The bit I am curious about is:
+ HO2=$'00:23:AE:69:C0:F2\r'

Anyone handy with text processing know about this?

Thanks

chrism01 09-24-2009 10:51 PM

How did you create maclist.txt, on MSWin?
If so, you need to run dos2unix over it first to cvt MS line endings to Linux ones.

DaveQB 09-24-2009 11:08 PM

chrism01 oh of course. So obvious now.
Our Windows guy here created it, so that explains the carriage return.

But what about the prepended $ ?

Its like a regex start of line, but why would cut add that [or grep]?

DaveQB 09-24-2009 11:16 PM

It might be tr that's adding or such.


Code:

+ grep 00:23:AE:69:C0:F2 /mnt/altiris/maclist.txt
+ tr -s ' '
om012274 00:23:AE:69:C0:F2
++ grep 00:23:AE:69:C0:F2 /mnt/altiris/maclist.txt
++ tr -s ' '
' HO2='om012274 00:23:AE:69:C0:F2
+ printf om012274 $'00:23:AE:69:C0:F2\r'
om012274+ grep 00:23:AE:69:C0:F2 /mnt/altiris/maclist.txt
+ tr -s ' '
+ cut '-d ' -f2
00:23:AE:69:C0:F2
++ grep 00:23:AE:69:C0:F2 /mnt/altiris/maclist.txt
++ tr -s ' '
++ cut '-d ' -f2
+ HO2=$'00:23:AE:69:C0:F2\r'
+ printf $'00:23:AE:69:C0:F2\r'
+ A=00:23:AE:69:C0:F2


DaveQB 09-24-2009 11:39 PM

So I converted the line endings with vi and now the $ is gone too.

So for some reason DOS line endings added $ at the beginning of the MAC address but not the hostname which is at the start of the line.

Weird.


All times are GMT -5. The time now is 07:31 AM.