EDIT : I will leave my old answer for legacy, but user BokkyPooBah gave me an awesome answer to another problem, somewhat related to this one.
His solution solved both, so go give him points on his answer there !
I'll quickly sum it up here:
You need to have an instance of Geth running, and with a bash script instead of running geth --testnet js script.js 2>>geth.log
you'll use geth attach
. You can then write javascript directly, and use $1
and $2
like in any bash script.
#!/bin/bash
arg1=$1
arg2=$2
geth attach << EOF | grep "Data: " | sed "s/Data: //"
<content of your js script>
EOF
This will hide the console output. If you need to see it for debugging, use geth attach << EOF
instead.
You may need to add the path to you ipc file if it's not default, between geth
and attach
. I tried to be short but if you need more detailed explanation see his answer.
My question was not perfectly asked and isn't clear about what I needed. I tried to be general, but since there is "no easy way" to pass arguments to a js script, you have to find a trick that is very dependent on the way you're calling your script (as I said, there are very specific answers for when the script is called from a html).
I found a solution to my problem so I'll post the answer because I think it could be useful to others.
What I was doing was calling a simple bash script from a web page, which executed geth --testnet script.js
, so I already had two levels of scripts : bash and javascript. Js can't take arguments (easily), but bash scripts can.
So here's what I did. The following is my bash script file.
echo "primary = eth.accounts[0]; personal.unlockAccount ...<content of the js script> ... address ='"$1"'); ..." > gethScript.js
geth --testnet js gethScript.js 2>>gethScript.log
The argument is $1 and is written directly in the js script. Then the js script works perfectly as if the argument was hard coded. The bash script can be run from console with bash bashScript "0x00..00"
or in php for example with : exec('./bashScript '.$address);