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?