-1

I am writing my .zshrc. I want my prompt to show my battery level which is stored in /sys/class/power_supply/BAT0/capacity I wrote a function that will take the value from the file and change the color of the digits (green if it is greater than 50, orange if greater than 20, and red otherwise.

The problem is I get this:

/home/user/.zshrc:5: parse error: condition expected: $getPower

My zshrc shows this for the $getPower function

3 function getPower ()
4 {
5     cat /sys/class/power_supply/BAT0/capacity
6 }
7 function batteryLeft  ()
8 {
9     if [[ getPower > 50 ]]; then
10        echo "Phrase"
11    fi
12}

On ZSH Documentation the first 2 code samples have 2 different ways to declare a function, with a function keyword and one without. Not sure what the problem is.

Tim Palmer
  • 46
  • 8
  • 2
    Is that function definition at or around line 5 of your .zshrc? If not, please [edit] your question add whatever is at line 5 (and nearby, the actual problem may be elsewhere). If your .zshrc is relatively short, you should just post the whole thing. If it's long, you should make a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) that shows this problem. – 8bittree Oct 25 '16 at 19:20
  • Not sure if it is important, but your message says `$get power` instead of `$getPower`. – Mad Physicist Oct 25 '16 at 19:24
  • Also, do you need parens after the function name in ZSH as in Bash? – Mad Physicist Oct 25 '16 at 19:25
  • Thanks guys, I'm typing this out on my phone, so there are a few typos – Tim Palmer Oct 25 '16 at 19:27
  • @MadPhysicist The parens are optional in zsh. – 8bittree Oct 25 '16 at 19:30
  • 1
    I just did a quick test, recreating this function exactly and ran it without any problems in zsh 5.1.1 (well, cat complained about a missing file with that exact path, but that's not the problem here). So the problem must be elsewhere. It may be related to you referring to `getPower` as `$getPower` (you don't normally use a `$` to call a function). To know for sure, I'd need to see more of your code. – 8bittree Oct 25 '16 at 19:36
  • So how should I call the function? – Tim Palmer Oct 25 '16 at 19:39
  • Just `getPower`. If there are paremeters, then it would be `getPower foo bar baz` – 8bittree Oct 25 '16 at 19:40
  • I am using this function as a variable in a conditional... e.g. if `[[ getPower > 7 ]]; then` – Tim Palmer Oct 25 '16 at 19:47
  • Post your actual code. The error message is obviously not refering to the code you posted. Posting different code is wasting your and our time. – Gilles 'SO- stop being evil' Oct 25 '16 at 19:55
  • You should absolutely apply Charles Duffy's changes. However, even after playing around with various forms of what you've added, I still cannot reproduce the same error you reported. – 8bittree Oct 25 '16 at 20:18
  • @TimPalmer, `if [[ getPower > 7 ]]` doesn't actually run the function; it's comparing the *string* `"getPower"` to the string `"7"`. As such, the results of that comparison are constant (within a given locale's collation order; whether digits are sorted before or after letters can vary from locale to locale), no matter what the actual power level is. – Charles Duffy Oct 26 '16 at 20:11

1 Answers1

1

Notably, nothing in this code is zsh-specific -- all the extensions used below are present in ksh93 and bash as well.

getPower() { cat /sys/class/power_supply/BAT0/capacity; }

batteryLeft() {
    if (( $(getPower) > 50 )); then
        echo "Phrase"
    fi
}
  • The function keyword makes your code incompatible with baseline-POSIX shells while adding no advantage over the compatible syntax. Avoid it as a matter of good practice.
  • To run your function, and thus be operating on its output, you need a command substitution, such as $().
  • To be running a numeric comparison rather than a string comparison, you need to either use (( )) in place of [[ ]], or use -gt in place of >. (This is incompatible with baseline POSIX -- you'd need to use [ "$(getPower)" -gt 50 ] to work with other shells -- but also has compensating advantages).
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • One `zsh` extension that *would* be useful here is to replace `cat` with `<` to read the file without starting a new process. – chepner Oct 26 '16 at 18:39
  • As in `echo < file.ext?` – Tim Palmer Oct 26 '16 at 20:30
  • @TimPalmer, no, just `< file.ext` as the entire body of the function; thus, `getPower() { < /sys/class/power_supply/BAT0/capacity; }` -- unlike bash, zsh has builtin behavior equivalent to `cat` in that case, but (as chepner says) without the startup overhead of using an external process. Redirecting content to the stdin of `echo` doesn't do anything useful, because `echo` doesn't read from its stdin. – Charles Duffy Oct 26 '16 at 20:50