1

Description

Hello,

I'm trying to loop over certain commands and save their outputs into a file, and while looping through those commands, also check the file which we saved their outputs so we can compare them from the file while looping the commands at the same time. In the end, check if the looped commands' outputs match with the previously saved outputs in that file. (Also check if the file doesn't contain the output and add it into the file so we can later use it to compare again)

This is my main script which loops through the said commands which are located inside /usr/local/bin/ so I can run them directly from the shell.

#!/bin/bash
wallets=`find /usr/local/bin/ -iname '*-cli'`

for i in $wallets; do
    current_blocks=`$I getblockcount`
    coin_name=${i:15:-4} # I use `:15:-4` here to cut the path and the last 4 characters. (For example it's `/usr/local/bin/bitcoin-cli` so I change it to `bitcoin` only
    echo $coin_name $current_blocks
    echo $coin_name $current_blocks >> blocks.log
done

And this echo gives us exactly this (assuming there are 2 items in the $wallets;

bitcoin 1457824
litecoin 759345

And this is the while loop I will -presumably- be using to read from the file;

while read line ; do
    set $line
    echo $1 $2
done < blocks.log

Which will also gives us this output when we run it;

bitcoin 1457824
litecoin 759345

So since I have these 2 code bits, now I want to merge them into a single script so I can both use the first code bit to loop through commands and also compare them with the blocks.log file. (Again, also check if the file doesn't contain the output and add it into the file so we can later use it to compare again).

My first (and failed) approach;

for i in $wallets; do

    current_blocks=`$i getblockcount`
    coin_name=${i:15:-4}

    while read line; do
        set $line
        if [ "$1" == "$coin_name" ]; then
            echo "File contains the coin_name, compare the blocks now"
            if (( "$current_blocks" >= "$2" )); then
                echo "Current blocks are greater than the saved blocks"
                echo "Saving the new blocks count now"
                sed -i "s/$1/$1 $current_blocks/" blocks.log
            else
                echo "Current blocks are less than or equals to saved blocks"
            fi
        else
            echo "File does not contain the coin_name, adding it now"
            echo "$coin_name $current_blocks" >> blocks.log
        fi
    done < blocks.log

done

My second (another failed) attempt;

for i in $wallets; do

    current_blocks=`$i getblockcount`
    coin_name=${i:15:-4}

    read line < blocks.log
    set $line
    if [ "$1" == "$coin_name" ]; then
        echo "File contains the coin_name, compare the blocks now"
        if (( "$current_blocks" >= "$2" )); then
            echo "Current blocks are greater than the saved blocks"
            echo "Saving the new blocks count now"
            # sed -i "s/$1/$1 $current_blocks/" blocks.log
        else
            echo "Current blocks are less than or equals to saved blocks"
        fi
    else
        echo "File does not contain the coin_name, adding it now"
        echo "$coin_name $current_blocks" >> blocks.log
    fi

done

What am I doing wrong?

  • Can't you use your simple script and then post-process the data in the file with another simple script? – Kusalananda Feb 19 at 21:58
  • I don't know, can I? – Marry Jane Feb 19 at 22:00
  • @Kusalananda, why did you delete your answer? – Crypteya Feb 19 at 23:56
  • I don't currently have time to work on it, and since it doesn't fulfil your needs anyway, I deleted it. I may pick it up in the future to make it better match your original ideas, but not now. – Kusalananda Feb 19 at 23:59
  • It's just asking for trouble to be modifying a file that you are currently reading. This should be done in two steps as @Kusalananda suggests. In place is for avoiding making a second copy, not for doing things in parallel. I'm having a hard time visualizing what's in blocks.log. It's not sorted, so how do you know the entry you're looking for will be the one you see when you read each line? In your second attempt, line will always start with the first line in blocks.log. Basically, you need to make a very simple example and run it through by hand on paper to see what it does. – Joe Feb 23 at 5:31

Your Answer

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

Browse other questions tagged or ask your own question.