I can't believe I'm here for such a trivial issue, but I honestly have no idea what's going on. I've been writing shell scripts for 20 years now and never encountered an issue like this.
I've got a section of code where I'm looking for a match, forming some variables, and then spitting them out to a CSV. The overall script works, but the output to CSV isn't acting as it should. I've added a debug statement or two, and those are acting weird as well.
else
# We assume there was no direct match, let's see if we can remove the CID from the asset name
NewDevName=`echo $DEVNAME | perl -pe "s/^(\d{4})\-//"`
echo -ne "Trying $NewDevName..."
if grep -q $NewDevName sl1_registry.csv;
then
echo "Found match with modified name"
IP=`grep $NewDevName sl1_registry.csv | cut -f3 -d\, | head -1`
CID=`grep $NewDevName snow_cmdb_asset_ci.csv | cut -f2 -d\, | head -1`
COMPANY=`grep $NewDevName sl1_registry.csv | cut -f7 -d\, | head -1`
REGION=`grep $NewDevName sl1_registry.csv | cut -f11 -d\, | head -1`
echo "Debug Values: Name:$NewDevName, IP:$IP, Company:$COMPANY"
echo "Test ${NewDevName},${IP},${COMPANY},${CID},${REGION},${DEVNAME} Foobar "
echo "$NewDevName,$IP,$COMPANY,$CID,$REGION,$DEVNAME" >> tempdb.csv
else
echo "$DEVNAME,NULL,NULL,NULL,NULL " >> tempdb.csv
echo "No Match Found"
fi
fi
I've had to scrub a lot of this, but I tried to do it in such a way that you can still see what's going on. The output is as follows:
Searching 1059-XYZ-123...Trying XYZ-123...Found match with modified name
Debug Values: Name:XYZ-123, IP:100.X.35.252, Company:ACME
,1059-XYZ-123 Foobar 5.252,ACME,1059,US
I don't seem to be able to format in color, but please note the word "Foobar" and where it appears in the output vs where it appears in the code statement.
Also note, the word "Test" (beginning of the second echo statement) is missing completely.
Lastly, you can see the last bit of the IP (100.X.35.252) is included as part of the device name in the output (5.252)
As far as I can guess, there has to be some sort of weird unicode/escape thing going on that is messing up 'something', but I have no idea what that could be. Nothing makes sense with this. I thought maybe it was an issue with how my Mac is running the code, but I also tried it on Ubuntu with the exact same results.