3

Can I run multiple commands at once in geth's JavaScript console?

Can I do something like > api1.command1() && api2.command2("arg2") && api1.command3()?

1

Can you separate them with ; ?

  • Yes! This works too :) Since this is the closest to what I'm looking for, I'm going to accept this answer, but thanks for all the help. – jeff Dec 20 '16 at 15:55
  • 1
    Note that separating with ; does not work fully. If you try web3.fromWei(eth.getBalance("0x9535b2e7faaba5288511d89341d94a38063a349b"), "ether"); web3.fromWei(eth.getBalance("0x88a80b35014fe7c33b4568cb818f2e16ba799b23"), "ether");, only a single result 1.01029574 is returned. There are ways to get around it, eg: var v1=web3.fromWei(eth.getBalance("0x9535b2e7faaba5288511d89341d94a38063a349b"), "ether"); var v2=web3.fromWei(eth.getBalance("0x88a80b35014fe7c33b4568cb818f2e16ba799b23"), "ether");console.log(v1 + " " + v2); will return the result 17.350627444329232008 1.01029574 – The Officious BokkyPooBah Dec 20 '16 at 15:59
3

Here's how I run my multiple command scripts in Linux and OS/X.

I've created a file $HOME/bin/multipleCommands.sh with the following contents:

#!/bin/sh

geth attach << EOF | grep "RESULT:" | sed "s/RESULT: //"

var balance1 = web3.fromWei(eth.getBalance("0x9535b2e7faaba5288511d89341d94a38063a349b"), "ether");
console.log("RESULT: balance1=" + balance1);
var balance2 = web3.fromWei(eth.getBalance("0x88a80b35014fe7c33b4568cb818f2e16ba799b23"), "ether");
console.log("RESULT: balance2=" + balance2);

EOF

I then set the executable bit for this file using the command

chmod 700 $HOME/bin/multipleCommands.sh

I run the following command to produce the output:

$HOME/bin/multipleCommands.sh
balance1=17.431747444329232008
balance2=1.042500854

In Ubuntu, the $HOME/bin subdirectory will be added to your $PATH once you log out and then log in. You can then run commands in the $HOME/bin subdirectory without specifying the $HOME/bin/ prefix to your command. So the following will work:

multipleCommands.sh
balance1=17.431747444329232008
balance2=1.042500854

If you are running Windows, the commands above should work if you install the Cygwin shell.

While you are writing your script and debugging your geth commands, use the following statement in your script file so you can see the progress of the geth statements being executed:

# geth attach << EOF | grep "RESULT:" | sed "s/RESULT: //"
geth attach << EOF

instead of:

geth attach << EOF | grep "RESULT:" | sed "s/RESULT: //"
  • This is really cool ! But while debugging, when I run geth attach << EOF | grep "RESULT:" | sed "s/RESULT: //" I can't see what is executed, I get an infinite number of > until I ^C. What should I do next ? – Teleporting Goat Jan 6 '17 at 16:11
  • @TeleportingGoat - use the loadScript(...) in the other answer if you need to use the console. Use the geth attach << EOF if you don't need to use the console and are still debugging. Then use the geth attach << EOF | grep "RESULT:" | sed "s/RESULT: //" when you have finished debugging and just have to run the command repeatedly. – The Officious BokkyPooBah Jan 6 '17 at 16:15
  • Thanks. I'm getting Fatal: Unable to attach to remote geth: dial unix /path/to/geth.ipc: connect: no such file or directory. The path wasn't initially correct, so I changed geth attach<<EOF to geth attach ipc:/correct/path/geth.ipc, and I get the same error, but with the correct path (I think). Makes me remember why I had given up on attach after I discovered it... – Teleporting Goat Jan 6 '17 at 16:25
  • What. I found the correct path with find / -name "eth.ipc" but now find returns nothing. Did it get deleted by running attach ? – Teleporting Goat Jan 6 '17 at 16:28
  • Ooooh ok I understood now, I feel really stupid. You need an running instance of Geth to attach to, and geth.ipc exists only when geth is running. This in incredibly useful ! – Teleporting Goat Jan 6 '17 at 16:32
1

I propose you to put them in a JavaScript file and use

LoadScript ('/path/myjs.js').

https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#loadscript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.