If statements (en)

From Conky PitStop

Jump to: navigation, search

If statements

 Language   English   Français   

I have the Disks section of my .conkyrc loaded up with $if_existing variables. The reason I do this is simple – I use a lot of removable media like an external HDD and USB thumbdrives. Since they come and go quite often, I don’t have any of them added to my fstab file, and rare is the occasion when one is plugged in at boot time. I also have a Vista partition on my laptop (don’t blame me, the thing came with Vista pre-installed…) that I access once in a while, and I’d like to keep tabs on space usage, read/write cycles, and things like that – but only when the drive is mounted. Otherwise, I like my Conky to be lean & mean and take up as little real estate as possible.

Let’s start off with how my Disks section looks with only / and ~ mounted


That’s pretty simple and straightforward. But what happens when my Vista partition gets mounted?


And then add a thumbdrive to the mix...


This could go on for a while, as I have enough space in my .conkyrc set aside for not only my Vista partition, but as many as five other devices mounted to /media!

Here’s an idea of how you might accomplish this on your own. Generally, since root and home are always mounted and are the most important parts of your filesystem, people tend to want to monitor those very closely. Here’s a sample line to monitor root without the usage bars or R/W graphs

${color2}Root:$color ${fs_used /}/${fs_size /} - ${alignr}${fs_free_perc /}% Free

What that line will show you is the text part of the readout in my screenshot for the root partition. You see at work the $fs_used and the $fs_size variables telling us how much space is used out of how much space there is total. Next, aligned to the right with $alignr, we see this value expressed as a percentage of free space with $fs_free_perc.

Now, on to the good stuff. Let’s say that after your root and home partitions appear in your Conky display, you want a removable drive to appear/disappear as it is mounted or unmounted at the location /media/disk. At the end of the previous line being displayed, you would add the //$if_existing// variable, like so:

${color2}Root:$color ${fs_used /}/${fs_size /} - ${alignr}${fs_free_perc /}% Free${if_existing /media/disk}

This is done at the end of the preceding line for one main reason – Everything under the TEXT header in your .conkyrc file will get displayed. That means every line break or carriage return, too. If you were to put the //$if_existing// flag at the beginning of a new line, that new line would still be displayed, but would be a blank line when your device isn’t mounted, because the conditions of the //$if_existing// variable would not be met. To think of it another way: You’re putting the carriage return inside the //$if_existing// statement, so that the line break only happens when the conditions of the statement are met.

To stay in the same pattern and display the same information, the following line would look like this

${color2}Disk:$color ${fs_used /media/disk}/${fs_size /media/disk| - ${alignr}${fs_free_perc /media/disk}% Free${endif}

Notice the $endif at the end of the line – that’s how we close the $if_existing statement so that only the information between the two is conditional on the conditions being met.

Let’s say you want a second line to appear/disappear as it’s mounted or unmounted, and that this particular device will appear at /mnt/WHATEVER. Your first two lines would basically stay the same, with one small addition:

${color2}Root:$color ${fs_used /}/${fs_size /} - ${alignr}${fs_free_perc /}% Free${if_existing /media/disk}
${color2}Disk:$color ${fs_used /media/disk}/${fs_size /media/disk| - ${alignr}${fs_free_perc /media/disk}% Free${endif}${if_existing /mnt/WHATEVER}

And on and on it can go. As I said, I have a Windows C: drive that appears, along with space for as many as five removable drives.

The $if_existing variable isn’t limited to drives, though. Any device can be detected using the '$if_existing' statement. For another example, I have a section in my Conky display that monitors my Wireless Internet connection. All sorts of information is displayed, including signal strength, SSID, internal and external IP addresses, an up/down speed graph, data transfer tallies, and more. It looks like this:


But, what if my wireless device is inactive, and I’m connected to the internet via an ethernet cable? Here’s the third part of the $if_existing variable – $else

I’ve preceded my entire wireless network monitor section with ${if_existing ath0}. As long as my wireless device is active, I have a section titled Wireless Network that displays like you saw above. At the end of the last line, I added the $else variable, and then patterned a section titled Ethernet similar to my Wireless layout.


All the info displayed is basically the same, minus the signal strength and SSID. At the end of that section, I closed it up with the $endif variable to finish it off, and that’s the end of that.

You can grab my entire .conkyrc file on page at the Gallery_2009_(1) or here.

Personal tools