Linux Blog

thinkpad_handler ACPI Script for Lenovo T61 hotkeys

Filed under: Shell Script Sundays — at 2:54 pm on Sunday, January 18, 2009

Lenovo T61 Thinkpad_Handler ACPI hotkey

This weeks shell scripting article showcases the modifications I have made to the thinkpad_handler script that normally resides in /usr/lib/acpid on an OpenSuse distribution. For any one not using a Lenovo T61 this may or may not be useful, however anyone interested in shell scripting can take a look at the file and see the simplicity of shell scripts that are used in production environments.

Here is my modified thinkpad_handler script

One of the reasons I chose to post this was because an OpenSuse upgrade broke my brightness keys support, so I figured it would be another good place to back it up while providing a reference. My modifications are very minimal at the moment, I still have a lot of work to do.

Here are my minor modifications:

Line 102 to add support for suspendon Fn+F4

Line 127 to eject /dev/cdrom on Fn+F9

I used to have a special script for Fn+F8 which enabled me to toggle support for the touchpad and track point mouse. Until I find that script I’ll endure excruciating mental anguish every time I accidentally hit the touchpad. I’ll have to make an update to this script once I change all of the other hotkeys for OpenSuse on the Lenovo T61 or other similar Lenovo notebook computer. I should also probably upgrade to OpenSuse 11.1 and see if that makes a difference in the keyboard shortcuts.

Until next week, happy scripting!

Crontabs 101

Filed under: Shell Script Sundays — at 11:42 pm on Sunday, January 4, 2009

Although not necessarily classified as shell scripting its self cron’s are very useful to any Linux administrators arsenal. The ability to run tasks at a specific interval is a great way to schedule things to run later or when the system load is lower. Many applications use crontab to schedule tasks so its hard to say what yours will look like.

crontab -l

will list all of the cron jobs scheduled for the currently logged in user mine has an entry for kpodder

# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
#KPodder entries
0 0 * * * -c "/home/owen/.kde/share/apps/kpodder/" -s "global.casts" -o "/home/owen/podcasts" -d 0
#KPodder End

The first five fields are to tell the task when to run. They are in the following order: minute, hour, day, month, day of the week. Asterisks are used to say any valid value and a forward slash can be used to make intervals such as five minutes, hours, days or months (*/5). A comma can be used for or values, so if used as 2,4,6 the cron would run at 2, 4 or 6 o’clock. Dashes are used for time spans, If you have an 8-5 work day you can use 8-17 if in the hours field.

Next, the sixth field is the actual program to run. It will look in the $PATH for the user, but for safety’s sake, I try to use the full path if possible. In my example of the kpodder script there are many arguments. I only really use simple crons and the number of arguments here seems rather excessive.

To edit the crontab do crontab -e. This will edit the current users crontab. If you are root and wish to edit a naughty cron from another user you use -u and specify the user.

Thats pretty much all there is to it. I’d love to here tips and how much people love/hate cron and why.

Happy Shell Scripting New Year,

– Owen.

To Script, or not To Script

Filed under: Shell Script Sundays — at 3:23 pm on Sunday, December 21, 2008

I hate doing manual repetitive labor. There is something about it that just bores me to death. Perhaps this is why I like shell scripting so much. To me it is one of the most useful things about the terminal. All the time I’m working I’m thinking about automation.

“Can this be automated?”
“Can I improve this process?”

Most of the time tasks can be automated in some fashion, and a simple shell script can be written. Its very useful. One of the problems about being able to shell script is the self discipline to say, no I’ll do this by hand. The reason being is that a lot of the time the learning curve and potential problems of automating a process are costly upfront, therefore taking you more time in the long run. So, next time you think about using shell scripting for automation ask yourself if it is really worth it. Depending on your workload, perhaps on the third of fourth time round it will be worth considering it. If you’re not busy and are up for a challenge, by all means knock yourself out!

Check if SELinux is Enabled

Filed under: Shell Script Sundays — at 10:41 am on Monday, December 8, 2008

This weeks (now late) Shell Script Sundays (posted on Monday) article is a short one on a check to see if SELinux is enabled. While SELinux has some great security enhancements it can present a number of problems in applications and shell scripts alike. There is a simple utility that comes with many Linux distributions called “selinuxenabled”

selinuxenabled exits with a status of 1 if it is not enabled and 0 if it is. Zero normally means false but in this case since it is an exit status it is an exception. So, if you need to do a quick check, you may just run selinuxenabled. You will quickly find that it returns nothing. To figure out the exit status for your quick check, put an ampersand (&) at the end, and it will tell you the exit status. eg:

[ ~]# selinuxenabled &
[1] 28417
[1]+  Exit 1                  selinuxenabled

As we can see from the example above SELinux is disabled.

To use selinuxenabled in your scripts you would use it like any other command. Refer to Shell Scripting 101 for some more information. selinuxenabled can also be used in your scripts to make sure that selinux is enabled, which can be useful if you are trying to do security audits across multiple machines.

Advanced Bash-Scripting Guide

Filed under: Shell Script Sundays — at 2:38 pm on Monday, November 24, 2008

Today the Advanced Bash-Scripting guide has been updated:

Changelog: “Many bugs were fixed. Additionally, there is a great deal of new material. Editing was done for increased consistency. Two new example scripts were added.”

This is not a book that everyone will want to read, but for those wanting to know more about scripting or those with bash scripting knowledge its a must. Its available in a couple of formats for your reading pleasure.

Shell Script Flow Control – my most refered to articles

Filed under: Shell Script Sundays — at 2:38 pm on Sunday, November 9, 2008

This week I do not have time to write a full fledged article on shell scripting, so I am going to some flow control and logic articles that I wrote that I read the most.

One script that I continuously refer to is one of the first shell scripting articles I wrote titled “When Photoshop Fails.” The reason I refer to this article is because it describes a couple of looping techniques and a loop that works with spaces in filenames. For those interested but do not want to read the whole thing here is the loop in short:

find * -iname "*" | while read i; do echo "$i"; done

The next article I’m linking to is the one I wrote on loops: it outlines some of the basic looping techniques. What I fail to mention in this article is that seq can be used to generate sequences of numbers for the [in list] section. The following is how you would create a loop to loop from 1 to 10 echoing out each number:

for i in `seq 1 10`; do echo $i; done;

Select Statements in Bash is exactly what the title implies. How to implement select statements in bash scripting. Also known as switch’s and case’s in other languages many will be familiar with this sort of logic. Even though I do not refer to it as often as some of the other articles I think it is worth of a mention in this list.

Decision making using if statements is something that every shell scripter is bound to come across. While not exactly the most in depth article on bash if then else statements it is a good start for any one wanting to learn more.

I love dialog’s and it just so happens that dialog and xdialog do a great job of making dialogs for your shell scripts. Creating Dialogs with Dialog has some basic uses of dialog. While it is not exactly logic or flow control, it can be used to prompt the user for input or just to pretty things up a little. It just so happens that it has an X front end called xdialog which does a good job too.

This is not all of the articles I refer to, but its the ones I wrote. If any one else has a list of articles they refer to that are bookmarked that I wrote or not I’d be interested to see them. Just paste them in a comment.

Working with CSV files in Bash

Filed under: Shell Script Sundays — at 1:25 pm on Sunday, October 19, 2008

CSV files are very common. Using them with Bash to aid in scripting can be very useful. Here are a some methods of using bash to work with CSV files. These are what I sometimes use when dealing with CSV files in bash, they may work for your CSV’s they may not. I accept no responsibility of the accuracy of these, I’m sure there are better ways of accomplishing the same tasks. If anybody finds other ways or knows better please comment and let me know. Don’t run these on production data, its always a good idea to make backups and test them before using in a production environment. Who knows how your precious data could become mangled due to a wrong number of quotes, terminations or bad regex. Jump for the post (Read on …)

Add Voice to your Shell Scripts

Filed under: Shell Script Sundays — at 3:52 pm on Sunday, October 12, 2008

Text to speech has been around for a number of years. While you may not want to add text to speech to all of your shell scripts you may have a particular script that would be nice to get some audio feedback from. There are a couple of text-to-speech engines that run in the shell, but my favorite is flite. It is really simple to install and use and has a decent set of voices. You can add other voices or compile your own with FestVox.

Here is how to use flite from the shell to directly play the text:

[owen@TheLinuxBlog ~]$ flite -t "Thanks for reading The Linochs Blog"

Notice the misspelling of the word Linux. If flite doesn’t correctly output the words, you can sometimes use phonetics to get a better result.

You can also play text files by using the -f option.

[owen@TheLinuxBlog ~]$ flite -t "Thanks for reading The Linochs Blog"
flite: pcm_params.c:2333: sndrv_pcm_hw_params: Assertion `err >= 0' failed.

(Read on …)

shell script argument lists too long

Filed under: Shell Script Sundays — at 11:19 am on Sunday, September 21, 2008

Sometimes you will run into the problem of having too many files in a folder. This results in basic programs like rm and mv to not be able to process your commands. For example, I recently had to extract 52628 jpgs from seven zip files. Once extracted I realized that I had them in the wrong folder. Now, there are other ways I could have accomplished the same goals, such but for various reasons sometimes the easiest way (renaming the folder) was not feasible. Here is the solution: (Read on …)

Reworking Shell Scripts – Part 2

Filed under: Shell Script Sundays — at 10:00 pm on Sunday, September 14, 2008

In the last Shell Script Sunday’s article I wrote, I said that I’d show you some more ways to rework shell scripts to make them easier to use. I’ve got some more tricks up my sleeve that I’d like to share, its  been rather busy this site of the internet at So why I write up some more shell scripting methods, here are some previous posts that can enhance your shell scripts. Be sure to comment on them if you find them useful, or would like more information.

Creating Dialogs with Dialog
If you have a shell script that you use on a regular basis, you may want to consider using dialog to make it more user friendly. Dialog makes it easy to create easy to use dialogs that are intuitive and easy to use. There are so many combinations of dialogs that can be created that the possibilities are ended. Dynamically create dialogs for select lists, input boxes, progress bars and much much more.

Graphical Shell Scripting
This article I wrote introduces graphical shell scripting. Similar to dialog this is an updated “Dialog” and works within X. If you support end users, or your target audience is Ubuntu/Linspire users (j/k) then XDialog may be the better choice. Its got most of the same functionality as Dialog except it depends on X. You can even support both Dialog and XDialog as they pretty much use the same syntax.

Reworking Shell Scripts

Filed under: Shell Script Sundays — at 7:02 am on Sunday, August 24, 2008

To me shell Scripts are all about automation, their primary purpose is to get stuff done. If you have a one liner, snippet or other script you use on a regular basis, have you thought about how you could rework it for it to become more in handy?

Lets take last weeks snippet from this column. It was a simple one liner to reconnect to a host. Now, I knew when I posted this article that it was a helpful snippet of code. Now, how can this script be adapted to be a neat utility that we use on a regular basis? Over the next few week’s we’ll find out.

The first thing that I will note on is that this script or shell snippet is a pain to remember. Does a script save you time if you can’t remember how it works? Is it worth the hassle? Not exactly. So, in order to make this snippet a little better the first thing we are going to do is add something that it needs: parameters. Adding parameters to shell scripts is actually easy, much easier than adding parameters to some other languages that we wont mention. although this script does not use it getopts can be used. I’ve covered how to do this with getopts in other posts. Just do a site search (located at the bottom of the right bar for getopts.)

So, here is the modified script that automatically reconnects to a host by using ping and SSH:

# Sleep Time Default: 15 seconds
# Set a default user up
#usage function
usage () {
echo -e "Usage: $0 host [user] [Sleep Time]"
# display usage if no host is specified
[ -z $1 ] && { usage && exit 1; }
# set the variables
[ $1 ] && { HOST=$1; }
[ $2 ] && { USERNAME=$2; }
[ $3 ] && { STIME=$3; }
# trying:
echo -e "host: $HOST \nuser: $USERNAME \ndelay: $STIME"
while ! ping -W 1 -c 1 $HOST 2>&1 >/dev/null; do true; done && echo "Successful waiting: $STIME secs"; sleep $STIME; ssh $USERNAME@$HOST

Now that you have that done, all you need to do is give the file a name (I called mine ssh_auto) and put it in a folder in your path. Use the filename and parameters defined in the script to connect to the host.

The next shell scripting article I demonstrate how you can further rework shell scripts to better suit your needs.

« Previous PageNext Page »