我目前正在编写一个简单的expect
脚本来打开以太坊geth console
并执行以下命令:
#!/usr/bin/expect
spawn /usr/bin/geth --testnet console
expect ">"
send "personal.unlockAccount('0xdc85a8429998bd4eef79307e556f70bb70d8caf1','X');r"
expect "true"
expect ">"
send "var mortalContract=web3.eth.contract([{constant:!1,inputs:[],name:'kill',outputs:[],type:'function'},{constant:!1,inputs:[],name:'cashOut',outputs:[],type:'function'},{inputs:[],type:'constructor'}]),mortal=mortalContract['new']({from:'0xdc85a8429998bd4eef79307e556f70bb70d8caf1',data:'60606040525b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b61016e8061003f6000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b514610044578063793cd71e1461005357610042565b005b6100516004805050610062565b005b61006060048050506100f6565b005b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156100f357600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b60003073ffffffffffffffffffffffffffffffffffffffff16319050600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600060028304604051809050600060405180830381858888f19350505050505b5056',gas:47e5},function(f,t){console.log(f,t),'undefined'!=typeof t.address&&console.log('Contract mined! address: '+t.address+' transactionHash: '+t.transactionHash)});r"
expect "undefined"
expect ">"
send "exitr"
expect eof
编译器在第7行(以var mortalContract
开头的行)有问题。我搜索了一下,发现双引号中的双引号会干扰期望,所以我将双引号改为单引号,但它仍然不起作用,并返回以下错误:
extra characters after close-brace
while executing
"send "var mortalContract=web3.eth.contract([{constant:!1,inputs:[],name:'kill',outputs:[],type:'function'},"
(file "expectScript.js" line 7)
需要在每个右括号后面加一个空格}
方括号是Tcl中的特殊语法。它们就像shell中的反引号:执行其中包含的命令并用结果替换。和shell一样,双引号允许命令替换。我将使用Tcl的非内插引号,即花括号:
send {var mortalContract=web3.eth.contract([{constant:... '+t.transactionHash)});}
# ...^...........................................................................^
send "r"