command issued from within bash script not working

Need help with C, C++, perl, python, etc?

Re: command issued from within bash script not working

Postby Dai_trying » 2019-09-16 21:33

You are confusing parameters with quantity of transactions, a parameter is information passed to a command, if you look at the api reference for bitcoin you will see that you have to submit parameters with the command (up to 8 for the sendmany option) so the number of parameters is static, it is the quantity of entries inside of each parameter that changes.

Looking through the api reference (https://bitcoin.org/en/developer-refere ... -core-apis) it would seem like you could use the SendToAddress to send an individual transaction, and it looks like it might be simpler to build the command. To do this you would read each line from your file and build the command from that one line, send it (using SendToAddress and then move to the next line etc, etc, etc...
Dai_trying
 
Posts: 783
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2019-09-17 18:27

Dai_trying wrote:You are confusing parameters with quantity of transactions, a parameter is information passed to a command, if you look at the api reference for bitcoin you will see that you have to submit parameters with the command (up to 8 for the sendmany option) so the number of parameters is static, it is the quantity of entries inside of each parameter that changes.

Looking through the api reference (https://bitcoin.org/en/developer-refere ... -core-apis) it would seem like you could use the SendToAddress to send an individual transaction, and it looks like it might be simpler to build the command. To do this you would read each line from your file and build the command from that one line, send it (using SendToAddress and then move to the next line etc, etc, etc...


The quantity of parameters may change. Some of them are optional. But to make things simpler, I just use them all to avoid having to deal with checking if mandatory and optional are preset or not.

I understand I might have misused the correct words. Anyway, what I mean is that I need to find a way of dynamically insert the items inside those 2 parameters which are lists of items.
And also, I need to figure out out to distinguish parameters from wrapping brackets, braces, colons and etc.

What about "eval" command? Would it be useful here?

I don't have problems with "sendtoaddress". That one is working nice. The thing with using "sendmany" is that it allows to pay way less in fees for each transaction. So, I have all the interest in using this command over the other. The other is only to be used in exceptions.
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby Dai_trying » 2019-09-17 20:15

PsySc0rpi0n wrote:I need to figure out out to distinguish parameters from wrapping brackets, braces, colons and etc.


I can see how it could be difficult to fully comprehend these things and I cannot say I do myself really, but this is one of the reasons I said I would use Python for this kind of script as you can easily create json "objects" which take care of formatting for you, I will take a look and see if I can put something together when I have more time but am a little limited at the moment.

PsySc0rpi0n wrote:What about "eval" command? Would it be useful here?


I really try to avoid using eval, I have used it in the past but only because I was pushed at the time, and eventually i found another way but even then I'm not sure it would work.

PsySc0rpi0n wrote:I don't have problems with "sendtoaddress". That one is working nice. The thing with using "sendmany" is that it allows to pay way less in fees for each transaction. So, I have all the interest in using this command over the other. The other is only to be used in exceptions.


I can see why it is better for you to do it this way now and will keep that in mind.


I should be able to look into doing a python script over the next day or two and will post something when I have it.
Dai_trying
 
Posts: 783
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2019-09-17 22:19

Dai_trying wrote:
PsySc0rpi0n wrote:I need to figure out out to distinguish parameters from wrapping brackets, braces, colons and etc.


I can see how it could be difficult to fully comprehend these things and I cannot say I do myself really, but this is one of the reasons I said I would use Python for this kind of script as you can easily create json "objects" which take care of formatting for you, I will take a look and see if I can put something together when I have more time but am a little limited at the moment.

PsySc0rpi0n wrote:What about "eval" command? Would it be useful here?


I really try to avoid using eval, I have used it in the past but only because I was pushed at the time, and eventually i found another way but even then I'm not sure it would work.

PsySc0rpi0n wrote:I don't have problems with "sendtoaddress". That one is working nice. The thing with using "sendmany" is that it allows to pay way less in fees for each transaction. So, I have all the interest in using this command over the other. The other is only to be used in exceptions.


I can see why it is better for you to do it this way now and will keep that in mind.


I should be able to look into doing a python script over the next day or two and will post something when I have it.



Ok, thank you a lot.

I'm looking to 'jq' command which allows one to filter data from a JSON object. Despite the fact that what I want is the other way around, I don't know if I can pull something out from here. I'll stick to bash script until you get something out of python. Because if I'm "forced" to switch to python, I'll have to re-learn the basics again. I did some basic learning in the past but then I had to quit because time was scarce!

What I'm thinking now is to write a file in JSON format using my already done printf with the complete command (which is seen as a single long string) and then try to format it with 'jq' and maybe be able to build the command from here.

I think the JSON things here in this command are just the 2 parameters that are wrapped in {} and in []. The other parameters I think I can just append them in a later stage right before issuing the complete command from the script!
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby Dai_trying » 2019-09-18 10:30

I had some time this morning to look at this and although this might not work I thought I would let you see what I have so far as it's not too far off.

First I created a quick bash script to act as bitcoin-cli simply to interpret the parameters

Code: Select all
#!/bin/bash

echo "Param 1 = $1"
echo "Param 2 = $2"
echo "Param 3 = $3"
echo "Param 4 = $4"
echo "Param 5 = $5"
echo "Param 6 = $6"
echo "Param 7 = $7"

I included 1 more parameter than you would be using in case there was some extra info being sent.

Next the file with codes
Code: Select all
1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX
1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz

I named it BitcoinList.txt and you would need to edit the script to reflect the actual file you use.

Then the Python script:
Code: Select all
import json
import subprocess

a = 0
d = {}
e = []

with open('BitcoinList.txt') as input_file:
    for i in input_file:
        a += 1
        b = a/100
        d[i.strip()] = '%.2f' % b
        e.append(i.strip())

r = json.dumps(d)
s = json.dumps(e)

subprocess.check_call(['/path/to/bin/bitcoin-cli', 'sendmany', '""', r, '1', '"Description comment"', s])

which gives me the result :
Code: Select all
Param 1 = sendmany
Param 2 = ""
Param 3 = {"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX": "0.01", "1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz": "0.02"}
Param 4 = 1
Param 5 = "Description comment"
Param 6 = ["1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX", "1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz"]
Param 7 =


What is left to do is adding the escapes (\) and maybe some extra quotes but I'm out of time for today so thought I would get this out in case you can tweak it, it is quite a simple script so should not be too difficult to do.

HTH
Dai_trying
 
Posts: 783
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2019-09-18 20:30

Dai_trying wrote:I had some time this morning to look at this and although this might not work I thought I would let you see what I have so far as it's not too far off.

First I created a quick bash script to act as bitcoin-cli simply to interpret the parameters

Code: Select all
#!/bin/bash

echo "Param 1 = $1"
echo "Param 2 = $2"
echo "Param 3 = $3"
echo "Param 4 = $4"
echo "Param 5 = $5"
echo "Param 6 = $6"
echo "Param 7 = $7"

I included 1 more parameter than you would be using in case there was some extra info being sent.

Next the file with codes
Code: Select all
1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX
1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz

I named it BitcoinList.txt and you would need to edit the script to reflect the actual file you use.

Then the Python script:
Code: Select all
import json
import subprocess

a = 0
d = {}
e = []

with open('BitcoinList.txt') as input_file:
    for i in input_file:
        a += 1
        b = a/100
        d[i.strip()] = '%.2f' % b
        e.append(i.strip())

r = json.dumps(d)
s = json.dumps(e)

subprocess.check_call(['/path/to/bin/bitcoin-cli', 'sendmany', '""', r, '1', '"Description comment"', s])

which gives me the result :
Code: Select all
Param 1 = sendmany
Param 2 = ""
Param 3 = {"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX": "0.01", "1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz": "0.02"}
Param 4 = 1
Param 5 = "Description comment"
Param 6 = ["1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX", "1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz"]
Param 7 =


What is left to do is adding the escapes (\) and maybe some extra quotes but I'm out of time for today so thought I would get this out in case you can tweak it, it is quite a simple script so should not be too difficult to do.

HTH


Hello...

Many thanks for the effort. It looks like very much to what I need. I'm going to try another approach in my bash script to see what comes out.

I'm thinking about to do what I'm already doing about building the command, but instead of trying to issue it, I'll right the output to a file already formatted and then will try to load only the JSON objects I need to send to the command already formatted!

If even like this it doesn't work, I'll consider to migrate the rest of the code and this task to python.

Thanks
Psy
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby Dai_trying » 2019-09-22 18:55

Had a bit of time tonight and came up with this, it seems to work, that is it gives the right characters as parameters to bitcoin-cli, I know you don't want a python script but look how short and relatively simple it is.

Code: Select all
import json
import subprocess
import re

a = 0
d = {}
e = []

with open('BitcoinList.txt') as input_file:
    for i in input_file:
        a += 1
        b = a/100
        d[i.strip()] = b
        e.append(i.strip())

r = json.dumps(d)
s = json.dumps(e)

escapedr = str(r).translate(str.maketrans({'"':  r'\"'}))
escapeds = str(s).translate(str.maketrans({'"':  r'\"'}))

subprocess.check_call(['/path/to/bin/bitcoin-cli', 'sendmany', '""', '"' + escapedr + '"', '1', '""', '"' + escapeds + '"'])


The output it gives me is this
Code: Select all
Param 1 = sendmany
Param 2 = ""
Param 3 = "{\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\": 0.01, \"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\": 0.02}"
Param 4 = 1
Param 5 = ""
Param 6 = "[\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\"]"

and although there are a few spaces in there where you had none, I see no reason for this to fail, let me know if it works for you.

NOTE: Don't forget to change the path to bitcoin-cli (Line 22) and the filename to be used (Line 9)
Dai_trying
 
Posts: 783
Joined: 2016-01-07 12:25

Previous

Return to Programming

Who is online

Users browsing this forum: techsavvy and 4 guests

fashionable