I'm writing shell scripts where quite regularly some stuff is written
to a file, after which an application is executed that reads that file. I find that through our company the network latency differs vastly, so a simple sleep 2
for example will not be robust enough.
I tried to write a (configurable) timeout loop like this:
waitLoop()
{
local timeout=$1
local test="$2"
if ! $test
then
local counter=0
while ! $test && [ $counter -lt $timeout ]
do
sleep 1
((counter++))
done
if ! $test
then
exit 1
fi
fi
}
This works for test="[ -e $somefilename ]"
. However, testing existence is not enough, I sometimes need to test whether a certain string was written to the file. I tried
test="grep -sq \"^sometext$\" $somefilename"
, but this did not work. Can someone tell me why?
Are there other, less verbose options to perform such a test?
There are few ways to make your shell (eg Bash) execute faster. Try to use less of external commands if Bash's internals can do the task for you. Eg, excessive use of sed , grep , awk et for string/text manipulation. If you are manipulating relatively BIG files, don't use bash's while read loop.
Using avg RTT (Round Trip Time) field to determine NFS latency. The average Round Trip Time (avg RTT) in milliseconds is a good measurement for NFS latency. In below example, The avg RTT (Round Trip Time) column is the average latency of the connection in ms.
Shell loops are slow and bash's are the slowest. Shells aren't meant to do heavy work in loops. Shells are meant to launch a few external, optimized processes on batches of data.
You can set your test variable this way:
test=$(grep -sq "^sometext$" $somefilename)
The reason your grep
isn't working is that quotes are really hard to pass in arguments. You'll need to use eval
:
if ! eval $test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With