The scripting is driving me mad 
I put commands into the terminal and they work, I put them into a script and they don’t. For example, if I put each of these lines into the terminal they work and I’m left with an empty file called public-ip.txt
/file remove “public-ip.txt”
/file print file=“public-ip.txt”
/file set “public-ip.txt” contents=“”
However, if I create and run a script with exactly the same commands, only the first two lines actually appear to run and I’m left with a file called public-ip.txt with 390b .
Also, if I run this command from terminal
/tool fetch url=“http://api.ipify.org/\3Fformat=text” dst-path=“/public-ip.txt” mode=http
It works perfectly and my public-ip.txt file contains my public ip, however if I do the same from a script - nothing happens.
What’s going on!!
Writes to the HDD happen about every 1.5 seconds, and scripts are much faster than that.
So whenever you add/change/remove a file, you should follow that up with “:delay 2s” (the extra 500ms being there “just in case”) if you plan to then read that same file immediately after.
Thanks # boen_robot. That was the information I needed.
This is a little script (my first working one ever with my Routerboard) the purpose is to detect my dynamic public ip (not my router wan ip which is inside a second nat) and then update a hairpin nat rule for that so that users inside my lan are not routed out of the network when they type the url of a server that is within the lan. Note: I'm using /system cloud to update my ddns host names within my domain dns records.
Forgive the untidyness, my first goal was to get it working - tomorrow I'll make it a bit more professional. But, this is what I got.
Update DDNS Cloud Service
/ip cloud force-update
Remove old and prepare new file
/file remove "public-ip.txt"
:delay 2s
/file print file="public-ip.txt"
:delay 2s
/file set "public-ip.txt" contents=""
:delay 2s
Get Public IP
/tool fetch url="http://api.ipify.org/\3Fformat=text" dst-path="/public-ip.txt" mode=http
:delay 2s
Write Public IP to file
:global "publicIP" [/file get [/file find name="public-ip.txt"] contents];
Change Hairpin NAT rule
/ip firewall nat set [ find where comment="hairpin" ] dst-address=$publicIP;
Time for a beer
I did find another solution which was to resolve my Mikrotik cloud client and then apply that to the nat rule. Perhaps I'll have a go that way next week.