Mar 22 2010

Silence stdout for an entire shell script

Tag: Linux, System AdministrationDan @ 6:17 pm

To silence stdout in a Linux shell script use the following line:

exec 1>/dev/null

This redirects the file stream to the /dev/null device which silently swallows the output.

This is useful if for instance you are writing a cron job which you want to have log it’s steps while you are developing or debugging it, but don’t want any output except the stderr once it’s running.


Jul 19 2007

Tips for learning to program Unix/Linux shell scripts

Tag: Linux, System Administration, UbuntuDan @ 8:25 am

I’ve been studying shell scripting in more depth recently. Here’s some of the things I found useful on the way.

1) Write your scripts to run on a real POSIX / Bourne Shell compatible sh interpreter first rather than bash or any of the other advanced shells. This improves the portability of your scripts and helps you to clearly learn the differences between the basic shell syntax and the advanced syntax introduced in shells such as bash.

On most Linux systems in recent years /bin/sh is a symlink to bash which has allowed people to ‘leak’ bash syntax in without error. Scripts like this only fail when run on a system that’s using a strict sh like on the FreeBSD or Ubuntu Feisty where /bin/sh is symlinked to ‘dash’ - The Debian Almquist Shell, which is a “lightweight POSIX compliant Bourne shell implementation”. IMO this is a better way to internalize the differences between sh and bash than using tools like ‘checkbashisms’ after you’ve created your script.

Firstly always specify ‘/bin/sh’ in the ’shebang’ line at the top of your scripts:

  1. #!/bin/sh
  2.  
  3. echo "Beginning of my script"

To see what your /bin/sh is linked to try this:

  1. $ ls -l /bin/sh
  2.  
  3. lrwxrwxrwx 1 root root 4 2007-05-03 13:09 /bin/sh -> dash

You’ll see that on my system (Ubuntu Feisty) the output at the end of the ls command is ‘/bin/sh -> dash’ which indicates that /bin/sh is linked (->) to dash. If on your system it links to bash do not try to link sh to anything else! If you do you may not be able to boot your system! Instead install the ‘ash’ or ‘dash’ shells and run you script in those directly like this:

  1. $ dash myscript.sh

2) Bookmark and read these reference manuals:

  • Bourne Shell Manual - Steve Parker’s html version of the original Bourne Shell manual great to check exactly what syntax is available in the real Bourne Shell.
  • Advanced Bash-Scripting Guide - The best tutorial on bash with loads of practical examples. The section on ‘tests’ (conditional logic) is particularly good.
  • Bash Reference Manual - The actual bash manual. It can be a little difficult to find things in this sometimes so it’s worth getting used to its structure

3) Use shell options to help you debug problems. You can do this either by using the ’set’ command (in bash type ‘help set’ to see usage or ‘man sh’ to view the man page) in your script or by passing the option on the command line as you run your script e.g.

  1. $ sh -x bin/myscript

Here are the options I find most useful:

  • -x Prints commands and their arguments as they are executed
  • -u Treat unset variables as an error when substituting
  • -e Exit immediately if a command exits with a non-zero status. Note this should be used with caution as there will be no warning when ths happens, you need to use -x to help discover where the problem is happening

There are loads so it’s worth looking through the list.

4) Use bash to discover syntax errors not highlighted by sh. If you script is failing silently in sh it can sometimes be due to a syntax error. On my system dash isn’t reporting these but I’ve found that explicitly running the script in bash often helps locate the cause of the problem.

5) Consider portability. If you know your script is going to be used across many different systems then it’s worth making sure you are only using commands available in the POSIX standard or on Linux now we can use the ones defined in the Linux Standards Base. See LSB Base - Commands and Utilities for more information.


Oct 30 2006

Making HP LaserJet-1020 work with Ubuntu 6.06

Tag: Linux, System Administration, UbuntuDan @ 8:33 pm

I inherited an HP LaserJet 1020 from the previous owner of my house. I found the drivers for Windows on the net and got it working fine, I really liked it (and so did my wife cause it prints quicker than my HP-PSC-1210). However, in June I migrated to Ubuntu 6.06 as my primary OS and couldn’t get it to work with the 1020.

My first attempt was to try the HPLIP drivers however, I noticed the 1020 listed in their unsupported devices page. On further investigation I found a note about it in their FAQ - apparently they consider the 1020 a “non-standard host based printer” and they don’t plan to support it.

Then I found a page on it on LinuxPrinting.org which pointed me to the foo2zjs project which is a linux printer driver for the ZjStream protocol which is what the 1020 uses apparently. Various posts on the Ubuntu forums also direct to this project for 1020 support. This post seems to be the best of the lot.

The process I followed in the end was to download the most current version of foo2zjs and compile and install it from source. This worked but I’m not 100% happy with this setup as it requires uninstallation of the ubuntu packaged version of foo2zjs which also forces uninstallation of the ubuntu-desktop meta package. I also tried to install it using checkinstall so that I could manage the installation using apt/synaptic but the dpkg installation process kept failing reporting that foo2zjs was

trying to overwrite
`/usr/share/foomatic/db/source/driver/foo2hp.xml’,
which is also in package foomatic-db

Here are the exact steps I took in the end, based on the instructions from various places:

  • Build and install foo2zjs
  1.  
  2. wget -O foo2zjs.tar.gz http://foo2zjs.rkkda.com/foo2zjs.tar.gz
  3. tar zxf foo2zjs.tar.gz
  4. cd foo2zjs
  5. ./getweb 1020
  6. sudo make install cups
  7.  
  • Run gnome-cups-manager (System > Adminstration > Printing) and add a new printer. It had already found my 1020 so I accepted the defaults all the way through the wizard.
  • Unplug (if not already) and then plug in the 1020
  • Print a test page (can be found from the gnome-cups-manager printer properties page).

Oct 27 2006

Prevent caching of static content using Apache config

Tag: System Administration, Web DevelopmentDan @ 10:51 am

Sometimes you have static content like a Flash SWF file which you want to prevent browsers from caching. Normally allowing clients to cache this type of content is useful however if you update it not all browsers will immediately fetch the new version.

To force a client to grab the new copy you can use the HTTP headers Cache-Control and Expires. It’s easy to do this in a dynamic page coded with PHP or similar. But how can you do this for static content?

Luckily the Apache server has an extension module called mod_expires which allows us to do this.

Basically mod_expires defines three directives: ExpiresActive, ExpiresByType, and ExpiresDefault.

ExpiresActive is required to switch on or off generation of the Expires and Cache-Control headers. ExpiresByType allows you to specify a expiration rule for a specific MIME type. ExpiresDefault allows you to set up a default expiration rule for all content within it’s scope.

Here’s a small example

  1.  
  2. # Switch on the Expires and Cache-Control headers
  3. ExpiresActive On
  4.  
  5. # Set a default rule: headers will report expiration
  6. # date as 4 weeks from when the page is accessed
  7. ExpiresDefault "access plus 4 weeks"
  8.  
  9. # Flash content is updated often so expiration is
  10. # immediate.
  11. ExpiresByType application/x-shockwave-flash "access plus 0 minutes"
  12.  

Oct 16 2006

Enabling CPU Frequency Scaling on Ubuntu

Tag: Linux, System Administration, UbuntuDan @ 9:01 am

Found an interesting blog entry about explicitly controlling CPU frequency on Ubuntu/Linux.

The system will by default automatically scale the CPU speed and does a good job, however some users may like to do this manually themselves.

Enabling CPU Frequency Scaling « Ubuntu Blog


Oct 02 2006

Printing to a networked HP Color Laserjet from Linux

Tag: Linux, System Administration, UbuntuDan @ 10:32 am

At work we have a HP Color Laserjet 2840, here are some rather brief notes on how I got it setup with Ubuntu:

  1. Install hplip from the Ubuntu repos
  2. Install PyQT so the HP admin tool works
  3. Install SNMP package for network support (is this required?)
  4. Run the Gnome Cups Manager Tool (System > Administration > Printing)
  5. Select Add Printer, choose HP JetDirect in the Network Printer select list, in the text box marked ‘Host’ input the IP or hostname of your printer.
  6. Print a test page and everything should be fine

A helpful tutorial is available at http://hplip.sourceforge.net/ in the Installation Instructions section which guides through installation using the CUPS admin interface instead of the Gnome Cups Manager.


Sep 08 2006

Power Vim Usage: Tips & Tricks for Everyday Editing

I’ve recently become a bit of a vim convert. Not for all editing tasks at the moment, but I’ve found myself choosing it sometimes in preference to my previous favourite editor JEdit.

I like vim for quick editing as I’m finding the key sequences very speedy once you’ve learned the basics. I miss JEdit’s fantastic hypersearch feature, however, knowing vim I’m sure there’s a way to do something similar.

Found this article today looks like it’s got lots of useful power-user tips for getting more out of vim. I’ll have a look through later.

Power Vim Usage: Tips & Tricks for Everyday Editing


Jul 21 2006

Uninstalling VMWare Player from Ubuntu Dapper

Tag: Linux, System AdministrationDan @ 6:21 pm

Note that if you need to uninstall VMWare Player (if you installed it from the Ubuntu repositories) you must select “Mark for Complete Removal” in Synaptic otherwise the vmware init scripts won’t get removed and the init process will logging errors (not fatal though) at startup. I think this is bug, I should probably raise it in launchpad.

Update: make sure your also completely remove the /etc/vmware directory and all files beneath it as well. The player uninstall process leaves this directory intact with the ‘locations’ file in it. When I tried to install VMWare Server it spotted that these files existed and though that it was already installed.


Jul 21 2006

VMWare Server now available free

Tag: Linux, System Administration, WindowsDan @ 9:40 am

I know I’m probably a bit behind the times on this one, but I’ll make a note of it anyway. VMWare seem to have decided to make their VMWare Server product free. A few months back it was only the Player tool which was free and in order to use that you had to make use of pre-created virtual appliances.

However, the Server product does allow appliance creation so users can potentially install any OS they like (as long as it’s supported). This is great for me as I have been running Ubuntu as my main OS since it was released (June 2006) and although I could reboot into XP for Windows stuff it’s hassle and interrupts my productivety.

There’s a howto on the Ubuntu forum here: HowTo: Windows (XP) on Ubuntu with VMWare Server, and there are a selection of VMWare related articles on the Ubuntu Wiki. These ones seem the most relevant: VmwareServer, VMware Guide: Installing VMware Server on Ubuntu 6.06 LTS amd64.

The only downside at the moment compared to VMWare Player is that the Ubuntu repositories don’t seem to have Server packages available only the Player is included in the non-free section. So manual install for the moment. Lets hope .deb packages can be included into the repo soon.

One other thing to note. Before I upgraded to Dapper I was running VMWare player using bridged networking on Breezy. I was also using NetworkManager. I had a weird problem which I never got to the bottom of where bringing network interfaces up and down in NetworkManager would intermittently hang the system. I found a thread on the NetworkManager mailing list which seems to indicate I may have been suffering from a nic driver/vmware incompatibility, the solution to which was to upgrade the driver software. I’m hoping this issue has gone away in Dapper but I’ll need to prove that after I’ve installed VMWare Server.


Jul 19 2006

FileZilla is being ported to Linux! Hurray

I used to use FileZilla on Windows before I migrated to Ubuntu Linux. Found out today that the FileZilla team are working on a port to Linux. Alpha snapshots are available here:

http://filezilla-project.org/nightly.php.

FileZilla is a great FTP tool, user friendly and very capable. It appears that the team are using the cross-platform wxWidgets GUI toolkit for the UI, it uses GTK+ on linux so FileZilla already looks good on the Ubuntu (Gnome) desktop. I look forward to the official release of version 3 on linux. Here’s a screen shot.

FileZilla 3 Screenshot


Next Page »