convert string from "dos" to Unix

Sonic84
Contributor III

Hello, I'm working on a extension attribute to tell me what version of McAfee Virus scan is installed. I got some unexpected output and tracked the problem down to the source file containing a bunch of DOS carriage returns.

How can I eliminate these characters without needing to install tools like dos2unix?

script:

#!/bin/bash

declare -x PLIST="/etc/cma.d/EPOAGENT3700MACX/config.xml"
declare -x NUMBER="$(cat "/etc/cma.d/EPOAGENT3700MACX/config.xml" | grep "<Version>.*</Version>" | sed -e 's/<Version>(.*)</Version>/1/' | awk '{print $1}' | tr -d '^M' )"

# be sure to enter ^M as ctl+v then ctl+m. the text will be blue in vi.

if [ -f "$PLIST" ] ; then
        declare -x VERSION="$NUMBER"
else
    declare -x VERSION="Not Installed"
    echo "not installed $VERSION"
fi

echo "<results>$VERSION</results>"

exit 0

output:

bash-3.2# ./test.sh 
</results>.6.0.3188

should be:

bash-3.2# ./test.sh 
<results>4.6.0.3188</results>

".. tr -d '^M'.." works from a local script, however the special "^M" does not survive a copy/paste into the JSS for the extension attribute.

here is the file I'm getting the string from, I'm using the "-v" for cat to show all the hidden characters.

bash-3.2# cat -v "/etc/cma.d/EPOAGENT3700MACX/config.xml"
<?xml version="1.0" encoding="UTF-8"?>^M
   <Configuration>^M
    <Path>/Library/McAfee/cma/lib/libagentplugin.dylib</Path>^M
    <SoftwareID>EPOAGENT3700MACX</SoftwareID>^M
<StartCommand>SystemStarter start cma</StartCommand>^M
    <StopCommand>SystemStarter stop cma</StopCommand> ^M
    <IsRunning></IsRunning>^M
    <ProductName>McAfee Agent</ProductName>^M
    <Version>4.6.0.3188</Version>^M
    <Language>0409</Language>^M
    <InstalledPath>/Library/McAfee/cma</InstalledPath>^M
    <UninstallCommand>/Library/McAfee/cma/uninstall.sh</UninstallCommand>^M
    <DebugScript>1</DebugScript> ^M
    <UpdaterDataDir>/Library/McAfee/cma/scratch/update</UpdaterDataDir>^M
    <ServicePackInstallDate> </ServicePackInstallDate>  ^M
    <ServicePackVersion> </ServicePackVersion> ^M
    <HotFixInstallDate > </HotFixInstallDate> ^M
    <HotFixVersions> </HotFixVersions> ^M
    <PluginPkgVersion> </PluginPkgVersion> ^M
    <MIDCabVersion> </MIDCabVersion>^M
    <UpdatePluginPath>/Library/McAfee/cma/lib/libmacbplugin.dylib </UpdatePluginPath>^M
    <keystore_path>/Library/McAfee/cma/scratch/keystore/</keystore_path>^M
    <SharedLibLocation>/Library/McAfee/shared/4.6.0/lib/</SharedLibLocation>^M
     <crypto_mode>2</crypto_mode>^M
     <crypto_keysize>1</crypto_keysize>^M
     <binsphash>[snip]</binsphash>^M
  </Configuration>^M
^M
bash-3.2#
2 ACCEPTED SOLUTIONS

zmkaylor
New Contributor III

I think "tr -d ' '..." will work.

View solution in original post

mm2270
Legendary Contributor III

@Sonic,

We also use EPO here and we have an EA that pulls the version string successfully. We're not cat'ing the xml file, but use grep directly and some simpler sed operations to get the proper column. The problem you're seeing may be directly related to the cat command, since it may be puling in those dos line breaks. No need to cat something if you plan on using grep to grab a specific line entry.

Here's ours, you can give it a try:

#!/bin/sh

EPOVersion=`grep "<Version>" /etc/cma.d/EPOAGENT3700MACX/config.xml | sed 's/</ /g' | sed 's/>/ /g' | awk '{print $2}'`

echo "<result>$EPOVersion</result>"

View solution in original post

6 REPLIES 6

eric_hutter
New Contributor

This is always an annoying problem. In vim you can do ':set fileformat=unix'.

EDIT
I need to read better, that won't help at all...

zmkaylor
New Contributor III

I think "tr -d ' '..." will work.

mm2270
Legendary Contributor III

@Sonic,

We also use EPO here and we have an EA that pulls the version string successfully. We're not cat'ing the xml file, but use grep directly and some simpler sed operations to get the proper column. The problem you're seeing may be directly related to the cat command, since it may be puling in those dos line breaks. No need to cat something if you plan on using grep to grab a specific line entry.

Here's ours, you can give it a try:

#!/bin/sh

EPOVersion=`grep "<Version>" /etc/cma.d/EPOAGENT3700MACX/config.xml | sed 's/</ /g' | sed 's/>/ /g' | awk '{print $2}'`

echo "<result>$EPOVersion</result>"

eric_hutter
New Contributor

How about doing tr -d ' '?

Beaten to the punch...

Sonic84
Contributor III

Both zmkaylor, and mm2270 suggestions worked.

Thank you!

rmanly
Contributor III