Skip to main content
Solved

convert string from "dos" to Unix

  • September 5, 2013
  • 6 replies
  • 0 views

Forum|alt.badge.img+12
  • Valued Contributor
  • 139 replies

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#

Best answer by zmkaylor1

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

View original
Did this topic help you find an answer to your question?

6 replies

Forum|alt.badge.img+9
  • Employee
  • 18 replies
  • September 5, 2013

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...


Forum|alt.badge.img+6
  • Contributor
  • 10 replies
  • Answer
  • September 5, 2013

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


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • September 5, 2013

@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>"

Forum|alt.badge.img+9
  • Employee
  • 18 replies
  • September 5, 2013

How about doing tr -d ' '?

Beaten to the punch...


Forum|alt.badge.img+12
  • Author
  • Valued Contributor
  • 139 replies
  • September 5, 2013

Both zmkaylor, and mm2270 suggestions worked.

Thank you!


Forum|alt.badge.img+12
  • Contributor
  • 312 replies
  • September 6, 2013

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings