0

The code:

first=true
mountpoint=" "
partlist=`df -h | grep "^/dev"` # get partition info
for i in $partlist              # loop through info
do
  if [[ ${i:0:1} = "/" ]]       # items starting with / are what I'm interested in
  then
    if [[ $first ]]             # The first instance in each pair is the device name
    then
      mountdev=$i
      mountpoint=" "
      first=false
    else                        # The second instance is the device mount point
      mountpoint=$i
      first=true
    fi
    if [[ $mountpoint != " " ]] # If the mountpoint was just set (last to be set
                                #   before printing config)
    then
      # Print config
      echo "${mountpoint} \${fs_size ${mountpoint}"
      echo "USED \${fs_used ${mountpoint}}\${alignr}\${fs_free ${mountpoint}} FREE"
      echo "\${fs_bar 3,300 ${mountpoint}}"
      echo "READ \${diskio_read  ${mountdev}}\${alignr}\${diskio_write  ${mountdev}} WRITE"
      echo ""
    fi
  fi
done

The problem:

The goal is to create a script that will generate my preferred conky config for each of my computers without having to edit the file for each computer. The above is a snippet that generates the section which reports on my disk usage information. Unfortunately I'm having trouble having it output ANYTHING. I throw various debug echos into it and it seems to all be working right except the if statement that actually output the config. I can't figure out what's wrong with it though. Any suggestions or assistance?

Advance thank you :)

FatalKeystroke
  • 2,882
  • 7
  • 23
  • 35
  • possible duplicate of [How to declare and use boolean variables in shell script?](http://stackoverflow.com/questions/2953646/how-to-declare-and-use-boolean-variables-in-shell-script) – Reinstate Monica Please Jul 18 '14 at 23:26

1 Answers1

6

This is the wrong line:

if [[ $first ]]             # The first instance in each pair is the device name

That would always evaluate to true. [[ true ]] or [[ false ]] is synonymous to [[ -n true ]] or [[ -n false ]] which is always correct.

What you probably meant was

if "$first"

Or

if [[ $first == true ]]
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Thank you, that worked :) I feel dumb now that it was that simple. – FatalKeystroke Jul 18 '14 at 16:11
  • Well, using ``"true"`` and ``"false"`` strings is ugly. You can use ``1`` and ``0`` and then check it with ``(( first ))`` or you can use ``1`` and empty string with ``[[ $first ]]`` – Aleks-Daniel Jakimenko-A. Jul 18 '14 at 18:51
  • 2
    @Aleks-DanielJakimenko Why is it ugly? For being less efficient, being less practiced? I certainly would not agree if it's because it's less readable for it's not. – konsolebox Jul 18 '14 at 18:53
  • @konsolebox yes, it is less practiced, it is less efficient, it makes some people think that there is a boolean type in bash, also ``[[ $var == true ]]`` is longer to type and ``if "$var"`` is misleading because it can be any command and not just ``true`` or ``false``, also ``if "$var"`` creates a hole if you forget to initialize variable, also checking ``if [[ $first == true ]]`` and ``if [[ $first == false ]]`` might look correct but both these expressions could be false at the same time if ``$first`` is something else, for example an empty string. How many reasons do you need? – Aleks-Daniel Jakimenko-A. Jul 18 '14 at 18:59
  • 2
    @Aleks-DanielJakimenko Well "being less practiced" doesn't work on me. I'm not a community-hugger. "Being less efficient" can be considered depending on other advantages. `[[ $var == true ]]` may be longer but is more readable. Also, it's not misleading if the whole application has that kind of style. There's an explicit test on it so how can that be misleading? Perhaps besides a stupid assumption that the scripter could have missed assigning a different value to a variable that's supposed to act like a boolean. Like [[ $var ]], (( $var )), are the real forms of dirty shortcut hacks. – konsolebox Jul 18 '14 at 19:12
  • Well, it is not smart to push stuff from other languages into bash. All languages define what is falsy (or similar), so just use this. In bash an empty string works well, and it is quite similar in perl. You don't use 'true' and 'false' strings in perl, do you? Also, it is ``(( var ))`` and not ``(( $var ))``. – Aleks-Daniel Jakimenko-A. Jul 18 '14 at 19:21
  • 2
    See definition of [Boolean](http://en.wikipedia.org/wiki/Boolean): *In most computer programming languages, a Boolean data type is a data type with only two possible values: true or false.* This does not strictly define how such a data type can be implemented. Bash doesn't have those forms so it's up to the programmer/scripter how he would apply it. You don't have to refer other languages here as it doesn't help anything on how it should be done. Also, `var` in `(( var ))` could dereference to another variable. – konsolebox Jul 18 '14 at 19:29
  • @konsolebox I think you're definitely right in using `"boolean"` operators rather than arithmetic comparison. But, your `if "$first"` syntax is still [somewhat flawed](http://stackoverflow.com/a/21210966/3076724) – Reinstate Monica Please Jul 18 '14 at 23:19
  • @BroSlow I don't follow that style actually but I saw it somehow as the likely form that the OP wanted. Nevertheless when enclosed around double-quotes the form may still be safe enough that it could also notify the scripter error messages when a value besides `true` or `false` is assigned to it. – konsolebox Jul 19 '14 at 03:10