<b>I have solved the strange increment problem!</b><br><br>To demonstrate the issue I created this test script:<br><span style="font-family:courier new,monospace">#!/bin/bash</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">#declare an int</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">declare -i INSTANCECOUNT</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "basename: $(basename $0)"</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">echo "raw ps output command hard coded:"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">ps aux | grep test3 | grep -v "grep" | wc -l</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "raw ps output command basename:"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">ps aux | grep $(basename $0) | grep -v "grep" | wc -l</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "Command substitution test"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo $(ps aux | grep $(basename $0) | grep -v "grep" | wc -l)</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">#assign & output 1:</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">INSTANCECOUNT=$(ps aux | grep $(basename $0) | grep -v "grep" | wc -l)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">echo "Count 1: $INSTANCECOUNT"</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">#assign & output 2:</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">INSTANCECOUNT=$(ps aux | grep $(basename $0) | grep -v "grep" | wc -l 2>/dev/null)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "Count 2: $INSTANCECOUNT"</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">#assign & output 3:</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">INSTANCECOUNT=$(ps aux | grep test3 | grep -v "grep" | wc -l)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">echo "Count 3: $INSTANCECOUNT"</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">#assign & output 4:</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">INSTANCECOUNT=$(ps aux | grep $(basename $0) | grep -v "grep" | wc -l | sed -e "s/[^0-9]//g")</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "Count 4: $INSTANCECOUNT"</span><br>
<br>This produced the output:<br><span style="font-family:courier new,monospace">raw output command hardcoded:</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">1</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">raw output command:</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">1</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">Command substitution test</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">2</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">Count 1: 2</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">Count 2: 2</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">Count 3: 2</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">Count 4: 2</span><br style="font-family:courier new,monospace">
<br>When I used another process, say grep for sql rather than $(basename $0) the counts were as expected!<br>Then I twigged, this was an issue of the ps command running using command substitution. In sohort by using command substitution "$()" a sub process was being created this was in turn creating an extra entry within ps which went as soon as the command substitution finished. Having done work with sub processes and simultanious processing under bash before I should have known. For those interested some links:<br>
<ul><li>Subshells (slightly different but same behaviour in ps demonstrated): <a href="http://tldp.org/LDP/abs/html/subshells.html">http://tldp.org/LDP/abs/html/subshells.html</a></li><li>bash internal variables ($BASHPID and $$ useful for looking at processes and sub processes by ID): <a href="http://tldp.org/LDP/abs/html/internalvariables.html">http://tldp.org/LDP/abs/html/internalvariables.html</a></li>
</ul><p>Note, $BASHPID is fairly new, only in bash v4 and up, to test:</p><p><span style="font-family:courier new,monospace">echo $BASH_VERSINFO[0]</span><br></p>If you have over version 4 then you can test with this in a script:<br>
<span style="font-family:courier new,monospace">#!/bin/bash</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">echo "Bash ID: $$ : $BASHPID : $BASH_SUBSHELL"</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">echo $(echo "Bash ID: $$ : $BASHPID : $BASH_SUBSHELL")</span><br style="font-family:courier new,monospace"><br>Interesting results, and worth thinking about if you are writing clever scripts!<br>
<br>So, as a result I'm not 100% trusting of my script and now I understand a little more why!<br><b><br>So, it's still on with the script/library!</b><br>For the sake of sanity I have made the change to my test case.<br>
<br><div>controlLib.sh</div><div>=========<br></div><span style="font-family:courier new,monospace">checkForDuplicateScript () {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> SCRIPTNAME=$(basename $0)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> #declare INSTANCECOUNT as integer </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> declare -i INSTANCECOUNT</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> INSTANCECOUNT=$(ps aux | grep "$SCRIPTNAME" | grep -v "grep" | wc -l 2>/dev/null)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if [ "$INSTANCECOUNT" -gt "2" ]</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> then</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> echo "Extra instance found of script: $SCRIPTNAME : Full ps output: "</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> ps aux | grep "$SCRIPTNAME"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> echo "Instance Count: $INSTANCECOUNT : $BASHPID : $SCRIPTNAME"</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> exit 1</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> fi</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">}</span><br style="font-family:courier new,monospace">
<br>Still produces the same behaviour, so I will presume that bash kindly allows you to have overkill on function declaration (that's how I have been getting away with it all this time, but being lazy will prevent that from now on). <br>
<br>So, the game is still on!<br><br><br><div class="gmail_quote">On 21 December 2011 12:37, David Halliday <span dir="ltr"><<a href="mailto:david.halliday@gmail.com">david.halliday@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'll give that a try but since all my other functions have been working as expected to date with that format I'll put it down as overkill.<br>Functions Doc: <a href="http://tldp.org/LDP/abs/html/functions.html" target="_blank">http://tldp.org/LDP/abs/html/functions.html</a><br>
<br>I'll give that a try to see if it's what is causing the strange behaviour.<div class="HOEnZb"><div class="h5"><br><br><div class="gmail_quote">On 21 December 2011 12:27, Laurence Southon <span dir="ltr"><<a href="mailto:laurence@southon.uk.net" target="_blank">laurence@southon.uk.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 20/12/11 21:32, David Halliday wrote:<br>
> function checkForDuplicateScript(){<br>
<br>
I don't know if it's the reason but that function declaration should be<br>
either:<br>
<br>
function checkForDuplicateScript { ... }<br>
<br>
or<br>
<br>
checkForDuplicateScript () { ... }<br>
<br>
Not the keyword 'function' and '()' at the same time.<br>
<span><font color="#888888"><br>
LS<br>
--<br>
Laurence Southon<br>
Tiger Computing, Bexley<br>
<a href="http://www.tiger-computing.co.uk" target="_blank">www.tiger-computing.co.uk</a><br>
<br>
_______________________________________________<br>
Kent mailing list<br>
<a href="mailto:Kent@mailman.lug.org.uk" target="_blank">Kent@mailman.lug.org.uk</a><br>
<a href="https://mailman.lug.org.uk/mailman/listinfo/kent" target="_blank">https://mailman.lug.org.uk/mailman/listinfo/kent</a><br>
</font></span></blockquote></div><br>
</div></div></blockquote></div><br>