0

Here is simple.sh

#!/bin/bash

sum=0
count=0

test() {
   if [ $count -gt 10 ]
   then
      sum=$[sum+1]
      count=0
   fi
   
   echo "Sum= "$sum" | "$count
}

while true; do
   echo "%{c} $(test)"
   count=$[count+1]
   sleep 1
done

I then pipe this to lemonbar. The code initially works but when the if clause comes into being count is set to zero and stays at zero (why?), so the sum is not updated after another 10 secs. How can I alter this?

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
Philip
  • 21
  • 4
  • Please [edit](https://stackoverflow.com/posts/64878552/edit) your question and format the code. – 0stone0 Nov 17 '20 at 15:49
  • 1
    `test` is being run in a subprocess, which means it cannot modify variables that are local to the calling process. – chepner Nov 17 '20 at 15:55
  • @chepner Please don't answer in comments. Thanks. – Asteroids With Wings Nov 17 '20 at 15:55
  • 1
    Top tip: I debugged this by writing `#!/bin/bash -x` - it lets you track exactly what's going on at each step. – Asteroids With Wings Nov 17 '20 at 15:56
  • @AsteroidsWithWings I didn't consider that an answer, as I gave no advice on what to do instead. – chepner Nov 17 '20 at 15:57
  • @chepner It explains the problem, so it's an answer. It does not go in the comments section, where it cannot be peer reviewed. SO is a Q&A not a forum. Thanks and have a great day. – Asteroids With Wings Nov 17 '20 at 15:58
  • @Philip, note that `$[ ]` is [obsolescent pre-POSIX syntax](https://wiki.bash-hackers.org/scripting/obsolete); standard-compliant shells aren't guaranteed to support it at all. Use `$(( ))` instead. – Charles Duffy Nov 17 '20 at 16:11
  • @AsteroidsWithWings, sure. That said, it's an answer already given on preexisting duplicate questions, so there's no reason to add a new answer on this one. – Charles Duffy Nov 17 '20 at 16:12
  • @CharlesDuffy Comments are still the wrong place for answers. If you think the question shouldn't be answered (with which I don't necessarily disagree), then writing one anyway but putting it in the wrong place is _double_ bad. – Asteroids With Wings Nov 17 '20 at 16:14
  • @AsteroidsWithWings, fair 'nuff. I sometimes comment on questions I'm closing as duplicate with guidance about which answer on the proposed duplicate question I recommend as most useful, or _why_ the questions are identical in substance if it isn't obvious, but in this particular case that guidance isn't particularly essential, as the primary/accepted answer is directly on-point. – Charles Duffy Nov 17 '20 at 16:22
  • I looked at the link! I added 'return $count' in test(). Then in while function added var="$(test)" then count=$? The count now works! How would I return 2 values (sum) as well? – Philip Nov 17 '20 at 16:22
  • 1
    @Philip, if you want to return more than one value, I'd stop using `var=$(test)` and instead (renaming your function from the reserved name `test` to the less-disruptive name `myfunc`) do something like the following: `myfunc() { count=1; sum=2; }` and then just call `myfunc`, not `something=$(myfunc)`. – Charles Duffy Nov 17 '20 at 16:25
  • @Philip, ...that said, it's also worth looking into namevars and indirect references -- see [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006#Assigning_indirect.2Freference_variables). Using indirect assignment you can pass the variable names to assign results to as arguments to your function. See also [Dynamic variable names in bash](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash); just avoid following any answer that suggests `eval` -- [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) goes into why. – Charles Duffy Nov 17 '20 at 16:25
  • Hi Charles - I've looked at your comment but alas have no real idea of what you mean! Where does myfunc() { count=1; sum =2; } go? Is this the definition or in the while loop. – Philip Nov 17 '20 at 17:23
  • That's a sample function definition (of a function that assigns hardcoded values to two variables with hardcoded names). The point is just to prove that its possible for a function to assign values to any number of global variables when called, when that call isn't in a subshell (such as the ones commands substitution creates). – Charles Duffy Nov 17 '20 at 21:21
  • Re: 'var="$(test)" then count=$?', that's extremely unreliable. `$?` isn't guaranteed to be able to store numbers that are more than 7 bits; you can't use it for arbitrary values. – Charles Duffy Nov 17 '20 at 21:24

0 Answers0