Linux Blog

Shell Script Sundays

Filed under: Shell Script Sundays,The Linux Blog News — TheLinuxBlog.com at 4:30 am on Sunday, October 10, 2010

I’ve been trying to keep up this blog for a number of years now. Problem is, I think I over committed when I created the Shell Script Sundays column. I’ve been keeping it going as best as possible, and will continue to do so, but I’m not going to be able to do it every week. If you may have noticed, I haven’t done any on a regular basis anyway. I do however, have some queued up for every other week, but I don’t know how long I’ll be able to continue doing that. There is also, only so much you can write about shell scripts. If anyone wants to step up and write some shell scripting articles you’re more than welcome to do so.

This will be my 70th article written since I’ve started. I doubt any one will even notice anyway, if you’d like to see it make a comeback, let me know.

That is all.

Remotely Changing Windows Volume

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:41 pm on Sunday, October 18, 2009

This is not really “shell scripting” but the end result is some more bash scripts in my bin directory so what the hell? It’s going in the shell script section because its Sunday. So what?

I like to listen to music on my Windows box while I work on my Linux box. Online radio and other sounds, just get in the way too much. One of the things I wanted to do for a while was remotely control my volume so I didn’t have to use my KVM to switch over to change the volume when ever anyone came in my office.

Its actually pretty easy to control your windows volume from Linux.

At first I thought, I’d create a dummy audio device, and some how map it over. Then I figured that was overkill and I’d try something a bit easier. I have SSH via Cygwin, so all I needed was a way to control the volume locally, and I could execute the command with SSH. Having no volume utilities jump at me when I looked through the Cygwin repositories I went to look for something else.

NirCmd is an awesome utility, giving me and other Windows users the ability to do things that Linux users may take for granted, you can read about it here: http://www.nirsoft.net/utils/nircmd.html after installing it, and making sure that my corporate AV didn’t throw a hissy, it was just a matter of dumping some scripts in my bin directory and chmodding them so they would run.

Here is what they look like:

Volume Down Script: ssh windowsbox -l owen -C “nir changesysvolume -2000″

Volume Up Script: ssh windowsbox -l owen -C “nir changesysvolume 2000″

Mute: ssh windowsbox -l owen -C “nir mutesysvolume 1″

Unmute: ssh windowsbox -l owen -c “nir mutesysvolume 0″

Real simple, and the mute/unmute really comes in handy for when some one walks into my office.

Whats old is New!

Filed under: General Linux,Linux Hardware — TheLinuxBlog.com at 1:41 am on Tuesday, March 10, 2009

There was a display in a local library showcasing “What’s old is new again” Basically showing the similarities between new and older newspapers and the cartoons in them. It may have something to do with the NY Times tour, but maybe not. All I know is it was there the other day, and now I go to write about it – GONE. Many of the same concepts such as recession “funnies” are popping up again. What so these journalists and cartoonist’s have it easy eh? All they have to do to get their job done is find some old drawing and change it a little. Well, I’m jumping on the bandwagon folks. Here are two “What’s old is New!” Articles:
Living without Windows
Shell Scripting 101
Since they’ve been written we’ve learned a lot, new concepts are out there but while they are somewhat older, the concepts still apply. As everything old they enjoy receiving comments but don’t take criticism well. They know their faults such as bad spelling and grammar but their ageing and refuse to acknowledge their flaws unless you directly point them out. Give them a read and talk to them before they die and go to internet Heaven (or Hell).

thinkpad_handler ACPI Script for Lenovo T61 hotkeys

Filed under: Shell Script Sundays — TheLinuxBlog.com 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. 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 — TheLinuxBlog.com 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 * * * kpodder.sh -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.

Check if SELinux is Enabled

Filed under: Shell Script Sundays — TheLinuxBlog.com 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:

[root@thelinuxblog.com ~]# 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.

Shell Script Flow Control – my most refered to articles

Filed under: Shell Script Sundays — TheLinuxBlog.com 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: http://www.thelinuxblog.com/for-while-and-until-loops-in-bash/ 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.

Reworking Shell Scripts – Part 2

Filed under: Shell Script Sundays — TheLinuxBlog.com 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 TheLinuxBlog.com. 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.

Bash Scripting Techniques

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:20 pm on Sunday, May 18, 2008

Here are some techniques that you can use in your bash scripts for finding and searching through files. Combined with other shell scripting techniques these can be very powerful.

Find all files in the current directory and print them:

find . -iname “.jpg”

Find all files that you have access to read with matching patern:

find / -iname “pattern”

Normally with grep text is matched and is case sensitive. Heres how to do a case insensitive search with grep:

cat <filename> | grep -i <match>

Finding and replacing text is easily done in bash with sed. This find and replace puts the contents into a new file:

 cat <filename> | sed ‘s/FIND/REPLACE/’ > <new filename>

Finding the line number that a particular line of text is on is sometimes useful. Here is how to do it:

 cat <filename> | grep -n <match>

Looping over a file in bash and echoing the output is sometimes useful for the processing of text files. Heres how to do it:

cat <filename> | while read i; do echo $i; done

Thats about all the bash scripting techniques that I can currently think of for finding in files. I know there are a ton more that I use but its hard to write them all down at once. As I come up with them or solve a problem I’ll add them here. If you have any of your own, please leave them in the comments.

Linux Blog new year roundup

Filed under: The Linux Blog News — TheLinuxBlog.com at 10:16 pm on Monday, December 31, 2007

I’d just like to say I’ve been busy over the past week or two with some projects at work and with the holiday season so I haven’t had the time to dedicated to this blog.

Its been a good year at the Linux Blog, I’ve increased traffic since I’ve started and have been beginning to see more activity with comments. I have a couple of goals for the upcoming year.

Firstly I’d like to make some money with Google ads. This isn’t specifically related to the Linux Blog but every little counts. I want to do this because I think that this site has some value to those that use it and I would like to be rewarded for my hard work.

Another goal I have is to write more posts than I did this year. It shouldn’t be hard to do since I stopped writing for a long time in 2007. From about Jan to July. I have a long list of posts to write but I need to know what topics people want to read about.

I have a list of articles to write for the Shell Scripting Sundays column but haven’t got enough ideas for the year yet. I may be able to get a few more if I split some of the more intense ones up but I’d rather keep them simple. If any one has any ideas for articles please let me know.

Thats going to be all for this year, I hope you enjoyed reading my blog and continue to visit me in the new year. For those that do

Happy New Year!

Graphical Shell Scripting

Filed under: Shell Script Sundays — TheLinuxBlog.com at 4:07 pm on Sunday, December 16, 2007

From all of my other previous shell scripting articles you can see that shell scripting is a very good way to get a task done or to automate. This is great but some times a little bit of that three letter acronym “GUI” is a nice touch. Well my friends, if you’ve been reading my articles and following my dialog examples then you are in luck. If not don’t worry, you can view all of the articles in the Shell Script section to the right.

The program to make your GUI’s for your shell scripts is the exact same thing as dialog except its graphical. Its called Xdialog. Once installed you basically use it the same way as dialog. If your thinking about upgrading a script thats written in dialog you might want to think twice because some window managers will display the windows differently.

Check out these Xdialog examples and corresponding screenshots:

Xdialog

Xdialog -yesno “Do you like Xdialog?” 5 50 && echo “Thats nice” || echo “Yea, some times its better to stick to CLI”

Xdialog ExampleXdialog Example

Next Page »