Linux Blog

APC Access Temperature Query and Conversion. (1 of 2)

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:54 pm on Sunday, February 1, 2009

This week I present to you; dearest reader part of a script I wrote to monitor the temperature on APC brand UPS’s. The script requires the apcupsd package to be installed and properly configured.

Here is the script:

#!/bin/bash

f () {
echo $(echo “($(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)*1.8)+32″ | bc);
}

c () {
echo “$(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)”
}

case “$1″ in
c)
c
;;
f)
f
;;
b)
echo “$(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)” C
echo $(echo “($(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)*1.8)+32″ | bc) F;
;;
*)
echo $”Usage $0 {f|c|b}”
;;
esac

The script uses basic bash, grep, cut and bc. It requires only one input, and that is f, c or b. f for Fahrenheit, c for Celsius and b for both. The second part of the script is a cron, with some more basic bash. It write a log and e-mails me if the temperature goes over a certain threshold, and it e-mails me again once the temperature has been resumed. I will post the cron script next week as it is a major portion. The tricky part of the cron was making it e-mail me only once.

Until next time, Happy Scripting!

APCUPSD

Creating Script Parameters With getopts

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:06 am on Sunday, October 14, 2007

Many programs for Linux have parameters that can be given at run time. These are also known as switches, arguments or options. These parameters make it easy to tell a program or script what to do and what options to use. In this Shell Script Sundays Blog post I will show you how to implement these in a script by using getopts.

For this example to work the following must be placed in a script:

while getopts “:e:” ARG;
do case “${ARG}” in
e) echo “${OPTARG}”;;
esac;
done

This code basically gets loops around the arguments. All this script does is take the value after the -e and echo’s it out. In the example below I named the script getopts.

owen@the-linux-blog:$ ./getopts -e “Hi There Linux Blog Viewers”
Hi There Linux Blog Viewers

For each extra parameter that is needed a new constant to the getopts and do loop need to be added. For example if the option -e and -q need to be in the script then the following getopts loop would be created:

while getopts “:e:q” ARG;
do case “${ARG}” in
e) echo “${OPTARG}”;;
q) echo “${OPTARG}”;;
esac;
done

Of course the above script for -q also only echo’s out the value for -q as seen below:

owen@the-linux-blog:$ ./getopts -e “Hi There Linux Blog Viewers” -q “Another Option”
Hi There Linux Blog Viewers
Another Option

This is all very well, but documentation is always nice, even if you think you know how to use your script you may forget in the future. Its also nice if you have a script that other people can easily use. That being said its good to have a way to show users how to run the script.

usage () {
echo -e “Usage: $0 -e \”What To Echo\” [ -q \"Output\" ]”
echo -e “\t-e: specifies what to echo (required)”
echo -e “\t-q: Where to write output to. If not specified the output is written to the console”
}

while getopts “:e:q:” ARG;
do case “${ARG}” in
e) ECHO=”${OPTARG}”;;
q) OUTPUT=”${OPTARG}”;;
esac;
done

[ -z "${ECHO}" ] && { usage && exit 1; }

[ "${OUTPUT}" ] && { echo $ECHO > $OUTPUT; } || { echo $ECHO; }

The code above takes the options and assigns a variable to each of the options $ECHO is what to echo and $OUTPUT is where to write the output to. The script calls the usage() function and exits whenever the required option ($ECHO) is not set. If $ECHO is set it checks to see if $OUTPUT is set, if so it echo’s the contents of $ECHO to the $OUTPUT variable (file or device). If $OUTPUT is not set then it just echo’s the $ECHO variable normally. This is the script running with its various different actions:

owen@the-linux-blog:$ ./getopts
Usage: ./getopts -e “What To Echo” [ -q "Output" ]
-e: specifies what to echo (required)
-q: Where to write output to. If not specified the output is written to the console

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example”
The Linux Blog getopts Example

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example. Output To Null” -q /dev/null

owen@the-linux-blog:$ ./getopts -e “The Linux Blog getopts Example” -q Write_To_This_File
owen@the-linux-blog:$ ls
Write_To_This_File getopts
owen@the-linux-blog:$ cat Write_To_This_File
The Linux Blog getopts Example

As there are many different variations and each implementation would be different I can not cover each individual getopts scenario but by assigning variables your option arguments you should be able to get switches working in your own shell scripts.

Select Statements in Bash

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:40 pm on Sunday, August 19, 2007

Shell scripts often need a menu so that the user can interact with the script and choose options. An easy way to so this is with the “select” statement.

Syntax:

select selection_var [in list]; do
statements (can use selection_var)
done

Example:

echo “########################################”;
echo “# SELECTS EXAMPLE – TheLinuxBlog.Com #”;
echo “########################################”;
echo “# Please Choose an Option #”;
echo “########################################”;

select selection_var in Number_1 This_is_Choice_2 Exit; do
case $selection_var in
Number_1 )
echo “You picked $selection_var or #$REPLY”
;;
This_is_Choice_2 )
echo “You picked $selection_var or #$REPLY”
;;
Exit )
echo Goodbye!
break
;;
* )
echo “Invalid Selection”
;;
esac
done

This bash select example has three choices. The first two are just examples, the third exits the script. There is a fourth case but it is not a choice, it tells the user that they have entered an invalid selection. I recommend using a case inside of a select because it will make life easier when adding onto a script. The $REPLY variable is returned from the select statement as a means of knowing what number was pressed. The $REPLY variable can be used in the case statement but I avoid doing so as all of the case blocks will have to be rearranged every time a new option is added.

The select statement in bash is very easy to implement and it can add a whole new range of functionality into your scripts. So try them out and look out for them in future shell scripts from The Linux Blog.