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: 925
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: 198
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: 925
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: 198
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: 925
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: 198
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: 925
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2019-11-16 12:29

It looks perfect too.
I got mine working but I'll keep your solution in mind because python is also a possibility in the future!
I'm thinking about investing some money in some blockchain online courses I see around (MIT and others) where python is a common presence.

Thanks a lot for the help.

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

Re: command issued from within bash script not working

Postby Dai_trying » 2019-11-16 13:28

You're welcome, I find python a great tool for many scripting jobs where bash can be a bit limited and so tend to lean towards it for anything other than basic scripts.
Dai_trying
 
Posts: 925
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2020-06-02 21:43

Hello.

I'm back to my script.

It's already working but I need to add a new fucntion that will replace some of the code already present in the script.

So what I need to do, is to extract a values from a field of a JSON object returned by a specific command.

To make it more clear, the proccess is:

I run the following command:

Code: Select all
bitcoin-cli listunspent


This command is supposed to return an array of JSON objects, like:

Code: Select all
[
    {
     "field 1": val1,
     "field 2": val2,
     "field 3": val3,
     "field 4": val4,
     "field 5": val5
    },
    {
     "field 1":  val6,
     "field 2":  val7,
     "field 3":  val8,
     "field 4":  val9,
     "field 5": val10
    }
]


And I need to add (sum), let's say, all values of 'field4' fields.

I'm trying to use the 'jq' tool to achieve this but I'm not being able to make the liasom between the output of 'jq' with the bash code to save 'j' output.

I can get the correct output in stdout with the following command:

Code: Select all
bitcoin-cli listunspent | jq '.[].field4'


But what I need, is to save this output, which are 2 lines in this case, into 2 variables, or add them on the fly and save the result in a variable.

How can I possibly save 'jq' result individually in bash variables?
User avatar
PsySc0rpi0n
 
Posts: 198
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby Dai_trying » 2020-06-02 22:26

Tbh I definitely wouldn't be using a bash script at this point, but you could look at parsing that json into an array (or arrays, 1 for each set of variables) or maybe into another json object organised how you want it for easy calculation.

but have only used arrays a little in bash scripts and don't recall handling jsons at all, and I find python lists and dicts are so much easier to create/use that I would definitely use them instead.

Maybe a better bash scripter could help more... Anyone?
Dai_trying
 
Posts: 925
Joined: 2016-01-07 12:25

Re: command issued from within bash script not working

Postby ruwolf » 2020-06-02 23:01

You can read output of program by e.g. enclosing it in grave accent character "`" (ASCII code: 0140, 96, 0x60):
Code: Select all
myvar=`bitcoin-cli listunspent | jq '.[].field4'`

Next, you can separate lines in $myvar e.g. to array by Tutorial Kart: Bash Split String.
User avatar
ruwolf
 
Posts: 410
Joined: 2008-02-18 05:04
Location: Slovakia, Banovce nad Bebravou, Matice slovenskej 1260/4-7

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2020-06-03 19:42

Dai_trying wrote:Tbh I definitely wouldn't be using a bash script at this point, but you could look at parsing that json into an array (or arrays, 1 for each set of variables) or maybe into another json object organised how you want it for easy calculation.

but have only used arrays a little in bash scripts and don't recall handling jsons at all, and I find python lists and dicts are so much easier to create/use that I would definitely use them instead.

Maybe a better bash scripter could help more... Anyone?


I know you are in favour of python. I totally understand, but I want to make this in bash for now.
I'm using python for another purpose though. I'm trying to replicate this script in python but using a 3rd party API (actually I haven't started it yet, but will start soon). :)
User avatar
PsySc0rpi0n
 
Posts: 198
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby PsySc0rpi0n » 2020-06-03 19:43

ruwolf wrote:You can read output of program by e.g. enclosing it in grave accent character "`" (ASCII code: 0140, 96, 0x60):
Code: Select all
myvar=`bitcoin-cli listunspent | jq '.[].field4'`


Next, you can separate lines in $myvar e.g. to array by Tutorial Kart: Bash Split String.


Ok, seems to be a great plan. It seems too simple, to be honest... heheh. Going to try.
I need those 2 output later as floating point numbers so that I can add them.
User avatar
PsySc0rpi0n
 
Posts: 198
Joined: 2012-10-24 13:54
Location: Portugal

Re: command issued from within bash script not working

Postby arzgi » 2020-06-04 16:53

I have nothing to add to what ruwolf and Dai_trying have said, but you know bash only supports integers?

If you know how long would the floating number would be, you could multiply it to get to an integer number.

Above, and the lack of return value in functions are the reasons why I use bash only for starter scripts and like.

Python in my mind too is better for general programming.
arzgi
 
Posts: 636
Joined: 2008-02-21 17:03
Location: Finland

PreviousNext

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable