0

Some strange bash problem happened to me when i deployed a fabric network

fabric image version:2.2

one of installing chaincode steps in fabric2.x is approveformyorg,and there is a parameter called "--signature-policy".According to the test-network bash file in fabric-sample-2.2,they use a variable called "CC_END_POLICY" to pass the parameter.But when i used in that way,i found some strange thing.

here is the code

CC_END_POLICY="AND ('Org1MSP.peer','Org2MSP.peer')"
if [ $CC_END_POLICY = "NA" ]; then
     CC_END_POLICY=""
else
     CC_END_POLICY="--signature-policy ${CC_END_POLICY}"
fi

and the approveformyorg command code

echo ${CC_END_POLICY}
peer lifecycle chaincode approveformyorg -o orderer.root.example.com:7050  --tls --cafile ${ORDERER_CA} --channelID ${HANNEL_NAME} -n ${CC_NAME} --version ${VERSION} --package-id ${PACKAGE_ID} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} --waitForEvent  >&log.txt

And i got error:

--signature-policy AND ('Org1MSP.peer','Org2MSP.peer')

peer lifecycle chaincode approveformyorg -o orderer.root.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/root.example.com/orderers/orderer.root.example.com/msp/tlscacerts/tlsca.root.example.com-cert.pem --channelID --name businessCC --version 1.0 --package-id businessCC_1.0:2b9b70ef81a7f5389973f5804ac6c40f2304821c1af8f636731fe1fef76a4850 --sequence 1 --init-required --signature-policy AND '('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')' --waitForEvent
Error: invalid signature policy: AND

I'm so confused about the ' and \ in the variable,why it's different when i run peer command. Had anyone ever encountered such problem?

  • 1
    Put a valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) and paste your script at https://shellcheck.net for validation/recommendation. That said, you're trying to do [complex](https://mywiki.wooledge.org/BashFAQ/050) assignment for a command... – Jetchisel Feb 15 '23 at 13:34
  • i was trying to send the value from another file, so i tried it this way. But when i executed it in the command line, it just showed the right value without symbol ' or \ ,i can't figure it out. – tang noodle Feb 15 '23 at 13:58
  • 2
    Debug output from many programs will conventionally have a backslash in the output to indicate a character is literal data that you gave to the program. For example `set -x ; echo "'Hello World'"` will show `+ echo ''\''Hello World'\'''` with the slashes indicating that you gave it quote characters in the argument list. The real problem is described in Jetchisel's link to the BashFAQ, passing arguments using string variables is hard to do in complex cases. – tjm3772 Feb 15 '23 at 14:17
  • This basically boils down to [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050). The shell isn't actually _adding_ anything at all; it's just escaping in the trace logs so that reading them lets you distinguish between literal and syntactic content. – Charles Duffy Feb 15 '23 at 14:28
  • ...and the solution is the one given in that FAQ: _Don't use string-type variables to store argument lists; use arrays instead._ – Charles Duffy Feb 15 '23 at 14:31
  • If I understand the situation correctly, all of the variables contain single arguments or argument options, so the issue here is just missing quotes. Variable expansions (`$var`) usually need to be quoted (`"$var"`) when the variable value contains spaces or various other characters. The safest approach is to quote all variable expansions unless you need not to. See [When to wrap quotes around a shell variable?](https://stackoverflow.com/q/10067266/4154375). – pjh Feb 15 '23 at 14:35
  • Also see the [When Should You Quote?](https://mywiki.wooledge.org/Quotes#When_Should_You_Quote.3F) section of [Quotes - Greg's Wiki](https://mywiki.wooledge.org/Quotes). – pjh Feb 15 '23 at 14:36
  • Note that ALL_UPPERCASE variable names (like `CC_END_POLICY`) are best avoided because there is a danger of clashes with the large number of special ALL_UPPERCASE variables that are used in shell programming. See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375). – pjh Feb 15 '23 at 14:38

0 Answers0