So I am fairly new to bash scripting, but I want a bash job that will:
Create a post-dated timestamp. (either as a var, or to a file)
Read lines from a file named feeds.txt.
Separate the number string from the whole string into two variables $feedNum and $wholeString.
Then execute a cat command to create or append a new file named $wholeString append the contents of timestamp.txt to that file.
Lastly, execute a wget command to insert $feedNum into the url string, and output the response of the wget to the previously created $wholeString file.
Here's what I have.
feeds.txt looks like (it will eventually be longer):
8147_feed_name.xml
19176_nextfeed_name.xml
And the script I've cobbled together looks like this.
#Changes the directory to the correct location
cd /home/scripts
# Inserts the proper timestamp in the file , postdating for one hour.
date -d '1 hour ago' "+%m/%d/%Y %H:%M:%S" >/home/scripts/timestamp.txt
#Loop Logic
for feedNum in `cat feeds.txt |cut -d _ -f 1`;do
for wholeString in `cat feeds.txt`; do
cat /home/scripts/timestamp.txt >>/home/scripts/tmp/$wholeString ;
wget https\://some.url.com/reports/uptimes/${feedNum}.xml?\&api_key=KEYKEYKEYKEY\&location=all\&start_date=recent_hour -O ->>/home/scripts/tmp/$wholeString;
done
done
My issue is, that it runs 4 times, and so if I ditch cat and wget and replace them with a simple echo, like this,
#Changes the directory to the correct location
cd /home/scripts
# Inserts the proper timestamp in the file , postdating for one hour.
date -d '1 hour ago' "+%m/%d/%Y %H:%M:%S" >/home/scripts/timestamp.txt
#Loop Logic
for feedNum in `cat feeds.txt |cut -d _ -f 1`;do
for wholeString in `cat feeds.txt`; do
echo $feedNum $wholeString
done
done
I get an output like this, where the top and bottom lines are correct. The middle two are a mix-mash.
8147 8147_feed_name.xml
8147 19176_nextfeed_name.xml
19176 8147_feed_name.xml
19176 19176_nextfeed_name.xml
I understand why it is doing this, but not how to fix it.
grepit from the file in the inner loop. Or, reorganize your loop to read feeds.txt line-by-line and cut out the two pieces you want inside one for loop.