Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

bash script to perform an action

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

bash script to perform an action

#1 Post by PsySc0rpi0n »

Hello.

I'm quite new to bash script and I've been reading the essentials to try to accomplish some task.

The task is to build a command to then run in terminal.

The command is supposed to have a variable number of parameters. Those parameters are inside a file, one parameter per line.

I already have a function to read the lines, store them in an array and count how many lines were read.

The command is something like:

Code: Select all

app-command argument-1 "" "{\"param-1\":0.01,\"param-2\":0.02}" 1 "" "[\"param-3\",\"param-4\"]"
.

In this example, the command will use 4 arguments that were load from the file.

My help request is to know how can I build that command based on the number of arguments read from the file.

I would appreciate some hints!
Thanks
Psy

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#2 Post by phenest »

Here is a bash script:

Code: Select all

#!/bin/bash

param=($(cat $1))

count=${#param[@]}
n=0
while [ $n -lt $count ]
do
    echo ${param[$n]}
    let n++
done
Save as script.sh and then to use:

Code: Select all

Usage: ./script.sh FILE
PS Don't forget to make the script executable.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#3 Post by PsySc0rpi0n »

phenest wrote:Here is a bash script:

Code: Select all

#!/bin/bash

param=($(cat $1))

count=${#param[@]}
n=0
while [ $n -lt $count ]
do
    echo ${param[$n]}
    let n++
done
Save as script.sh and then to use:

Code: Select all

Usage: ./script.sh FILE
PS Don't forget to make the script executable.
Thanks for the reply but that's not close to what I'm looking for.

I'm not sure if I explained my self poorly but my goal is to run a command that will have as many parameters as there are lines in the file. An each line in the file is one of the commands parameter.
I'll try to make an example:

The file contains the following lines:

Code: Select all

param1
param2
param3
The bash script I want to write should be able to run the command with the parameters found inside the file like:

Code: Select all

$app-test <app-argument> {param1; param2; param3}

But the script needs to adapt, I mean, if the file has 30 lines of parameters, the command should run in terminal with those 30 parameters.

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#4 Post by phenest »

PsySc0rpi0n wrote:Thanks for the reply but that's not close to what I'm looking for.
I know. I'm not doing the whole thing for you.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#5 Post by PsySc0rpi0n »

But that's only printing every parameter loaded from the file. I have no problem with that.

What I'm asking here is how to build the command I want to build using all these parameters.

This is related to JSON RPC calls. Not sure it changes anything.

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#6 Post by phenest »

PsySc0rpi0n wrote:The bash script I want to write
I'm trying to avoid doing the work for you. What I've supplied IS close to what you want. VERY close. It was meant as a sample to give you an idea of how to parse parameters in a Bash script.

Other than that one-liner, what research have you done on writing this script? 'Cos, to be honest, what you want can be done in a couple of lines. Maybe one.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#7 Post by phenest »

Code: Select all

command $(cat FILE)
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#8 Post by PsySc0rpi0n »

phenest wrote:

Code: Select all

command $(cat FILE)
Yes, I know what are you trying to say.

I searched "how to read lines from a file".
And I already have a function to load each line of the file into an array.

In my first post I gave an example of the command I need to build. I need to add those {}, [], "", '',\, etc. And these special chars are not in the file. I need to add them while I'm building the command. And depending on how many lines there are in the file, those special chars needs to be moved into the right place.

So, it's not just simply read the commands from the file and parse them to the command. I need to add the special chars in the correct place.
For instance, I'll give 2 different examples.

Let's say this file has 2 long strings that are parameters to the file and 1 other long string that may comes from inside the script itself, the command syntax looks like:

Code: Select all

app-command argument-1 "" "{\"param-1\":val1,\"param-2\":val2}" 1 "" "[\"param-3\",\"param-3\"]"
As you see command has 2 sections, one comprising a "{}" pair and another comprising a "[]" pair. There are other parts also but these are fixed. Never changes. The first "", the middle 1 and the middle "" right after the 1. And inside each pair of {} and [], there are also escaped ", : and ,.

So, for 2 long strings in the file plus that other long string which is not in the file, command needs to place the 2 long string from the file within the "{}" pair, both strings wrapped by the escaped " and followed by the :val and inside "[]" pair must be the other long string.

For 3 long string present in the file, command needs to be like:

Code: Select all

app-command argument-1 "" "{\"param-1\":val1,\"param-2\":val2,\"param-3\":val3}" 1 "" "[\"param-4\",\"param-4\",\"param-4\"]"
Note: param-3 from example 1 and param4 from example 2 are the same. Just had to name them different because I added a 3rd param to the "{}" pair.

I thought trying something with 'sed', but problem is the same.
Now I'm thinking to try with some loop, to add params until they run out but I'm not sure it's the best way. I'll try though.

EDITED;

Well, I did a little bit of experimenting and it's not yet complete but looks good (at least the result)

Code: Select all

send_to_mult_addr(){
   load_addr_data
   x=0 
   val1=0.02
   com_params="\"\" \"{\""
   for i in "${addr_arr[@]}"
   do  
      com_params+="\"$i"
      com_params+="\":"$val1","
   done
   printf "$com_params"
}

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#9 Post by phenest »

PsySc0rpi0n wrote:The task is to build a command to then run in terminal.

The command is supposed to have a variable number of parameters. Those parameters are inside a file, one parameter per line.
So there is a file, which has one parameter per line, and you want to pass all those parameters to one command. And the number of parameters in that file may vary.

Correct?
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#10 Post by PsySc0rpi0n »

phenest wrote:
PsySc0rpi0n wrote:The task is to build a command to then run in terminal.

The command is supposed to have a variable number of parameters. Those parameters are inside a file, one parameter per line.
So there is a file, which has one parameter per line, and you want to pass all those parameters to one command. And the number of parameters in that file may vary.

Correct?
Yes, added to the fact that I need to add a bunch of {}, [], "" and other chars in the correct place! But I got one version already running!

Anyway, I'm all ears to listen better options than the one I posted above!

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#11 Post by phenest »

Could you give an example of file contents and what the parameters should look like after the special characters, etc are added.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#12 Post by PsySc0rpi0n »

phenest wrote:Could you give an example of file contents and what the parameters should look like after the special characters, etc are added.
Sure. This is a bitcoin core command example from bitcoin core docs:

Content of file should be a list of compatible addresses for bitcoin wallets:
1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX
1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz

(wrong wording here)the command must be a JSON object with the following format/syntax:(/wrong wording here)
The command looks like this and must strictly follow the following syntx as it will be interpreted and executed by a JSON RPC server and it will return a JSON object containing info:

Code: Select all

bitcoin-cli sendmany "" "{\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\":0.01,\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\":0.02}" 1 "Description comment" "[\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\",\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\"]"

I already have my version working but its an archaic solution that just concatenates the special caracters and then concatenates the addresses by order with for loops.

Psy
Last edited by PsySc0rpi0n on 2019-09-04 19:10, edited 1 time in total.

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: bash script to perform an action

#13 Post by phenest »

What I'm struggling with is the special characters you're adding. There doesn't appear to be a pattern. That is, if you always have 4 parameters, I can see how they're added. But what about 30 as you suggested? How do they get added then? The parameters in curly brackets have a different syntax to the parameters in square brackets. What happens to the parameters after the 4th one?
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#14 Post by PsySc0rpi0n »

phenest wrote:What I'm struggling with is the special characters you're adding. There doesn't appear to be a pattern. That is, if you always have 4 parameters, I can see how they're added. But what about 30 as you suggested? How do they get added then? The parameters in curly brackets have a different syntax to the parameters in square brackets. What happens to the parameters after the 4th one?
Ok, so in the code I did I firstly added the fixed special chars, then I used a for loop to add the strings there were previously loaded from the file, appending the surrounding brackets and double quotes and always checking when the last parameter comes to change the surrounding special chars.

So, the pseudo-code looks something like this:

- Add/append initial/fixed special chars
- Append file parameters surrounded by the special chars all parameters have, checking when the last parameter is present to change the surrounding special chars.
- Append middle fixed parameters and special chars which are also fixed in number and in place within the command syntax
- Append last set of parameters loaded from the file and check when last parameter is present to change (and close) the last special chars so that the command is complete!

Can you picture my idea?

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: bash script to perform an action

#15 Post by Dai_trying »

I would imagine this is not going to be much different from what you already have but thought I would throw it out anyway, it should work but as I don't use bitcoin-cli I could not test it, If you go over 100 entries in your list you would need to make some alterations to it as the counter would go 0.100 and you might need to go 1.01 and then count as previous, but I don't know what the format should be or if there is any limit. Anyway it might be of some use to you.

Code: Select all

#!/bin/bash
filename="PsySc0rpi0n_test.txt"
a=1
command='"" "{'

while read LINE
do
    if [ $a -gt 1 ]; then
        command+=',\"';
    else
        command+='\"';
    fi
    command+="$LINE"
    command+='\":0.'
    pos=$(printf "%02d" $a)
    command+="$pos"
    ((a++))
done < $filename

command+='}\" 1 "Description comment" "['

b=1
while read LINE
do
    if [ $b -gt 1 ]; then
        command+=',\"';
    else
        command+='\"';
    fi
    command+="$LINE"
    command+='\"'
    ((b++))
done < $filename
command+=']"'

#~ bitcoin-cli sendmany "$command"
echo "$command"
note: The PsySc0rpi0n_test.txt file simply contained the codes you gave in your example.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: bash script to perform an action

#16 Post by PsySc0rpi0n »

Yes, it's something like that what I did too.

And I don't have any counter limit or limit to the codes in my file. I just load all the lines into a variable in memory and count how many lines were loaded and work with that counter just to control when I'm in the last register. That's all!

I thought myself that it would be harder but it wasn't after all!

Thanks
Psy

Post Reply