如何通过mxpy与Shell脚本向智能合约发送字符串?
在Shell脚本中使用mxpy向智能合约传入字符串参数的解决方案
问题根源在于Shell会自动按空格拆分参数,直接使用str:"My string to send"时,Shell会把它拆成str:"My、string、to、send"四个独立参数,导致mxpy无法正确解析。以下是几种可靠的解决方式:
1. 用单引号包裹完整的字符串参数
单引号在Shell中会屏蔽所有特殊字符和空格拆分,是最稳妥的方式,适合不需要变量替换的固定字符串:
mxpy contract call erd1qqqqqqqqqqqqqpgqxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --function yourFunctionName \ --arguments 'str:"My string to send"' \ --sender erd1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --gas-limit 5000000
2. 用双引号转义内部引号(适合变量替换场景)
如果需要在字符串中插入Shell变量,用双引号包裹整个参数,同时转义内部的双引号:
USER_INPUT="Hello from Shell script" mxpy contract call erd1qqqqqqqqqqqqqpgqxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --function yourFunctionName \ --arguments "str:\"$USER_INPUT\"" \ --sender erd1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --gas-limit 5000000
3. 传递多个参数的正确格式
如果需要同时传递字符串和其他类型参数(比如数字、地址),每个参数都要单独作为一个整体传递:
mxpy contract call erd1qqqqqqqqqqqqqpgqxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --function multiParamFunction \ --arguments 'str:"First string"' 'u64:12345' 'erd1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \ --sender erd1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --gas-limit 5000000
Shell脚本中的常见坑点
- 不要省略外层引号:直接写
--arguments str:"My string"会被Shell拆分,必须用单/双引号把整个str:"..."包裹起来 - 变量替换时注意引号嵌套:如果用双引号包裹,内部的双引号必须加反斜杠转义,否则Shell会提前闭合引号导致语法错误
内容的提问来源于stack exchange,提问作者Tiberiu Dodita
相关产品推荐
相关产品推荐

