2

I have a short bash script to replace a uuid in a line in a file:

#!/bin/sh

alpha="0-9A-F"
uuidPtn="[$alpha]{8}-[$alpha]{4}-[$alpha]{4}-[$alpha]{4}-[$alpha]{12}"
ProductCode="\"ProductCode\" = \"8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}\""

newguid=`uuidgen`
newguid="${newguid^^}"

cmd="echo $ProductCode | sed -r s/$uuidPtn/$newguid/"

echo "$ProductCode"
eval "$cmd"

It produces almost correct output, but with the quotation marks omitted:

"ProductCode" = "8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}"
ProductCode = 8:{A4B1D092-1C56-44F3-B096-34B67A5F39B1}

How can I include the quotation marks?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
stack user
  • 835
  • 1
  • 9
  • 28
  • Would using single quotes in your `ProductCode='"ProductCode" = ... 2}"'` declaration change anything ? – Aserre Jan 24 '17 at 14:24
  • ProductCode has the correct value, as can be seen from the output. – stack user Jan 24 '17 at 14:25
  • 1
    I tested you code, the problem was that you needed single quotes around the `cmd` declaration, otherwise the quotes will be expanded twice (one during the declaration, once during the eval) – Aserre Jan 24 '17 at 14:29

3 Answers3

1

Glad you got it working! Here's another way, which does not involve eval (since eval is evil):

#!/bin/bash

alpha="0-9A-F"
uuidPtn="[$alpha]{8}-[$alpha]{4}-[$alpha]{4}-[$alpha]{4}-[$alpha]{12}"
ProductCode="\"ProductCode\" = \"8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}\""

newguid=`uuidgen`
newguid="${newguid^^}"

#cmd="echo "$ProductCode" | sed -r s/$uuidPtn/$newguid/"  ## Not this

echo "$ProductCode"
#eval "$cmd"                                       ## Not this either

#                    v                    v whole pattern quoted
changedcode=$(sed -r "s/$uuidPtn/$newguid/" <<<"$ProductCode")
#           ^^         command substitution                  ^
#                    here-strings for input ^^^^^^^^^^^^^^^^^
echo "$changedcode"

Output:

"ProductCode" = "8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}"
"ProductCode" = "8:{6094CF73-E23E-4655-B4A8-DAA57BE7EF72}"
Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • Replace `[$alpha]` with the POSIX character class `[[:xdigit:]]` and you should be default always use single quotes around the right side of assignments unless you NEED double quotes for some reason and then in addition to it's other benefits you won't need to escape the double quotes in your variable assignments: `ProductCode='"ProductCode" = "8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}"'`. Also, you use `$(..)` in some places - always use it instead of deprecated backticks, and sed -r is GNU-specific, use sed -E instead for portability to both GNU and OSX seds. – Ed Morton Jan 24 '17 at 15:56
1

This is a sh version

#!/bin/sh

alpha="0-9A-F"
uuidPtn="[$alpha]{8}-[$alpha]{4}-[$alpha]{4}-[$alpha]{4}-[$alpha]{12}"
ProductCode="\"ProductCode\" = \"8:{0059DDB5-D384-46F9-BBFD-0004A8C39732}\""

newguid=`uuidgen`
newguid=$(echo "${newguid}" | tr a-z A-Z)

ChangedCode=$(echo "$ProductCode" | sed -r s/$uuidPtn/$newguid/)

echo "$ProductCode"
echo "$ChangedCode"
mug896
  • 1,777
  • 1
  • 19
  • 17
-1

I solved my own problem by changing the cmd= line to this:

cmd='echo $ProductCode | sed -r "s/$uuidPtn/$newguid/"'

thanks for the eyes on though folks.

stack user
  • 835
  • 1
  • 9
  • 28