Search code examples
bashconky

Bash if block doesn't run when it clearly should


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 :)


Solution

  • 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 ]]