Linux.com

Community Blogs



Using Perl to print Avery labels.

This blog will discuss how to use the Perl module PostScript::MailLabels to print Avery labels.

The basic design is to have your user(s) run the following BASH shell script, which will then call a Perl script that builds a postscript file, then the BASH script will send the postscript file to the printer.

Here is the shell script your user(s) will run.

 #!/bin/bash

#####################################

#This shell script is used to print address labels.#
####################################

#Must export default printer environment variable
#in order for lpr to function correctly.  The rest
#will be executed from a Perl script.

#declare variables
FILEPATH="/home/user1/"
DATE=`date +%m%d%y`
MYPERL=`which perl`
SCRIPT=${FILEPATH}'print_addresses.pl'

#get name of file user wants to process
echo "What printer do you want to print to?"
read PRINTER
#echo "Going to process: $DEFAULTPRINTER"

#set default printer to DEFAULTPRINTER
export PRINTER=$PRINTER

#call perl script which builds a postscript
#file sends it to user's printer.
perl $SCRIPT

#send postscript file to the printer
`cat ${FILEPATH}$DATE | lpr -P $PRINTER`

exit 0

 The BASH script basically asks the user what printer they want to print to,  set's the user's DEFAULT printer, then calls your Perl script using the modulePostScript::MailLabels to build a postscript file, then the BASH script  sends it to the user's printer.

Here is the Perl script which does most of the "heavy lifting."

#!/usr/bin/perl -w

##########################################

#This script will open a file containing a list of                  #
##addresses, use the addresses to build a postscript file, #
##and send that file to a printer.                                           #
##########################################

#import required modules
use strict;
use PostScript::MailLabels;

#declare local variables
my $labels;
my @addresses;
my $filename;
my $filepath = '/home/user1/';
my $date = `date +%m%d%y`;

#get name of file from user
print "Enter name of file you would like to process:  ";
$filename = <>;

##inform user of progress
print "Creating postscript file ...\n\n";

$labels = PostScript::MailLabels->new;

#create file handle for reading
open(NEWADDRESSES, "$filepath$filename") or die("Unable to open file: $!");

#read file one line at a time
while () {

    #delete first line containing name;street1;street2;citystzip$
    next if /^name;/;
n
    $labels->labelsetup(
        Avery       => $labels->averycode(5961),
        PaperSize   => 'letter',
        postnet     => 'no'
    );

    $labels->definelabel('clear');

    $labels->definelabel(0,'fname','lname');
    $labels->definelabel(1,'street');
    $labels->definelabel(2,'city','state','zip');


    #Match lastname, firstname, and address.
    my $pattern = '^(.+),\s+(.+);(.+);(.*);(.+),\s+(\w{2})\s+(\d{5}|\d{5}-\d{4})';

    $_ =~ /$pattern/;

    my $lname = $1;
    my $fname = $2;
    my $address1 = $3;
    my $address2 = $4;
    my $city = $5;
    my $state = $6;
    my $zip = $7;

    my @record;

    push @record, $fname;
    push @record, $lname;
    if ( $address2 !~ /^$/ ) {
        push @record, "$address1, $address2";
    } else {
        push @record, $address1;
    }
    push @record, "$city,";
    push @record, $state;
    push @record, $zip;

    #print $labels->makelabels( $addresses );
    push (@addresses, \@record);

#close while loop
}

#open file handle for writing
open(PRINTADDRESSES, ">$filepath$date") or die("Unable to write file: $!");
 #write postscript file
print PRINTADDRESSES $labels->makelabels( \@addresses );

#close filehandle
close(PRINTADDRESSES);

#close filehandle
close(NEWADDRESSES);

#notify user of file completion and get printer name
print "Postscript file named $date has been created successfully.\n";

#exit cleanly
exit 0;

The Perl script basically asks the user what's the name of the file they want to print, reads in the file (; delimited in this case), outputs a postscript file.

Obviously you will have to make some changes in order to get these scripts to work in YOUR environment!

Note: This is for Avery labels with Avery code 5961.  Please review  PostScript::MailLabels's documentation on CPAN to see if the module supports your particular labels or not.  You will also probably have to tweak the  "labelsetup" and "definelabel" parts to fit your needs.  Also note that the file this script reads is in the format of name;strete1;street2;citystzip$ semicolon delimited with a trailing $.  You will have to tweak the regex if your file is in a differet format.

Disclaimer:  This blog entry comes with NO expressed warranty, guarantee, support, or maintenance of any kind!  Use at your own risk!  

Good luck and happy printing!

 

Novice user of obvious choice.

I started using Linux back in around 1998and have been rather stuck ever since. In the start it was har and i jumped back and forth between Linux and Windows but one day i decided to learn this "new" (to me) system so i through out Windows all together and started to use Linux 100%. Ever since that day i've been stuck. Most of the people in my surrounding are using systems like Windows and MacosX which makes it rather hard for me. I see Linux as such a natural choice that its hard to even perform everyday tasks such as bank-errands online using e-identification, sending office-documents to people (allways have to think about converting them to .doc or .pdf documents for them to be able to read them) and so on.

I am no guru on Linux really. I like to contribute with what i can, i use linux 100% and i like to learn. Its just fun to see how quickly Linux (even though some ignorant people say Linux cant do everything other popular systems can) can become the only natural choice.

It's been quite some time since i stopped arguing with people on why to use Linux or not. Its up to ever person to use the system they feel is the best choice for them. However, i would wish that more people would have the guts to actually try out new systems. If it is Linux or some BSD-system or MacosX or what ever, doesn't matter. Its a fun experience and it can change quite a lot.

 

Oh well, now i've written quite a few rows about nothing special at all. Feels like i stole a part of your life with nonsens :-)

 

Take care!

 

Simply Kruisin' ...

Wot a gem ...

 Krusader that is ... i use the PCLOS distro and have only recently moved from GNOME to KDE, the reason being that when PCLinuxOS was upgraded to version 2009.1 i could NOT get my GNOME installation to follow it .... i encountered inconsistencies and could not get networking to work as it had been.

SOooooo, installed KDE and have been using and exploring it for the past six weeks or so. My system is a small peer to peer home LAN in which two workstations act as a fileserver, for storing dynamic data, and the other as an internal LAMP server, on which i mirror my genealogical site.

On top of my workstation, which is a dual boot machine [Linux and XP], there are two other workstations on the network, one XP the other WIN2000, which the rest of the family use.

Consequently i am forever requiring access to the file systems of five internal workstations plus the remote host where i publish my blogs, genealogy, resumes etc., and until i found Krusader, it was all a bit of having to use a variety of programs to cover all the bases.

However, having fully discovered Krusader i stand in awe of this wonderful file management tool. Not only does it have a 'Command Line' and 'Terminal Emulator' built in, it has two side by side panels which can show different file systems, either local or remote, and each of those panels are tabbed!!! You can connect ftp, fish, samba, sftp.

Of course the functionality on the menu is fully integrated to support moving and comparing files between the two ... you can bookmark locations ... recall locations visited historically ... and the list goes on!!

But hey, you all already know all this dont you?? *smile* ....

... but just in case there is another NOOB out there who does a lot of file management ....

Krusader IS the answer!!

 

Recording your 3D Games Made Easy

From the glc website: “glc is an ALSA & OpenGL capture tool for Linux. It consists of a generic video capture, playback and processing library and a set of tools built around that library. glc should be able to capture any application that uses ALSA for sound and OpenGL for drawing. It is still a relatively new project but already has a long list of features.”

The question now is: How do we install glc on our Arch Linux systems? It isn't available on any of the main repositories, but like many useful tools it is on AUR. This, my friends, is the perfect time to introduce you to the yaourt package.

yaourt is a wrapper for pacman that allows you to easily install packages from AUR. But be cautious, packages from AUR are user contributed, you have to make sure the package is from a user you trust and that you have examined the package yourself and trust the code it contains. Otherwise you may end up installing a package that seriously compromises your system.

For the installation of yaourt we need to add the [archlinuxfr] repository to the end of our pacman.conf file:


# nano -w /etc/pacman.conf

And add:


[archlinuxfr]
Server = http://repo.archlinux.fr/i686

Now, after syncing our repositories (pacman -Sy) we should be able to install the yaourt package:


# pacman -Sy yaourt

We can now proceed with the installation of glc.


$ yaourt -S glc
or, if you like using the latest and greatest:
$ yaourt -S glc-git

Yaourt will automatically download the PKGBUILD from AUR, run makepkg, create the package, and install it for you. This is an interactive process; you will be prompted to review the PKGBUILD (to ensure it doesn't contain a shady download link or nefarious command like rm -rf / which will wipe out your entire hard drive), and your sudo password, among other confirmation dialogs.

Thanks kumyco for providing us with these high quality glc PKGBUILDs.

Here are some useful commands that will hopefully get us started with a basic knowledge of what glc does and how. We shall proceed by capturing a 3D application, in this case the Battle for Wesnoth game.


$ glc-capture -o /home/myusername/glc-videos -r 0.5 -s wesnoth

Let us parse the above command to see what all those options stand for. First, we have -o, which stands for output, it specifies the directory in which we want to save the .glc video we are about to capture. Then we have the -r 0.5 option, which resizes the video to half, for example glxgears being a 300x300 size window will be resized to 150x150 on the .glc video. This is very useful if the window size of the application is large. Otherwise we would end up with a very big .glc video file. I once had a video of less than three minutes that was almost a gigabyte in size!

And finally, the -s option tells the application to start recording as soon as Wesnoth is launched. If you don't want this, shift + F8 is the key combination to start and stop glc-capture recording capabilities. However, this won't work if the game has that exact key combination assigned to something else.

Now, we should have a video, let us play it.


$ glc-play filename.glc

Esc key stops playback, f toggles full screen and Right key seeks forward.


$ glc-play myvideo.glc -o - -a NUM | lame -hV2 – audio.mp3

This will separate the audio and make it into a .mp3 file. Then we can go about encoding the video and mix it with the audio file we just made ourselves.


$ glc-play myvideo.glc -o - -y NUM | mencoder -demuxer y4m - -nosound -ovc x264 -x264encopts qp=18:pass=1 -of avi -o video.avi

$ glc-play myvideo.glc -o - -y NUM | mencoder -demuxer y4m - -audiofile audio.mp3 -oac copy -ovc x264 -x264encopts qp=18:pass=2 -of avi -o video.avi

If all went well, we should have the video.avi, with sound. I hope you have enjoyed your game capturing so far, and will continue to do so in the future.

 

Interactive Fiction and Linux

I have only just recently revisited the world of Interactive Fiction (or Text Adventures if you will), and have been looking at the number of different IF-engines and applications that are available.  I didn't realise so many formats have been adopted.

 What actually prompted me to get back into it was a review I read on Adventure Gamers for 1893: A World's Fair Mystery. For a text adventure to rank amongst their top games was inspiring, so I sought out a demo of it and then had to find something to run it on.

 1893 AWFM was written for the TADS2 (Text Adventure Development System version 2) system, and, as it happens, a match for TADS2 comes up in Ubuntu's repositories.  I ran the game from the terminal and what I was met with was a simple application shown in the terminal, displaying text from the game.  It all works fine, but it's not what I saw in the screenshots on the review.  There was pictures, and it wasn't horribly mono-spaced like what I was seeing.

 Looking into it further, it appears that this particular game incorporates a feature the majority of TADS2 games don't: HTML content, allowing the display of pictures and partitioning of the screen.  Obviously the command-line app I was using wasn't up to the job, so I tried looking at other applications which would support it.  It turns out that there are only 2 applications which support TADS2 with multimedia: one for Windows called HTML TADS, and one for Mac (pre OSX) called HyperTADS.  What? Nothing for Linux at all? There is currently only one option for Linux users: HTML TADS under Wine, which does work, but it's surprising that there isn't a native app.

 But for TADS2 games which don't have any HTML content, there are alternatives:

QTads is a QT-based app which is similar to the console version, but uses nicer fonts by default, which can be changed, along with a few other features.

FrobTADS is another console app which is pretty similar to the other TADS2 app.

I actually left it at that to start, but last week I found something called Gargoyle.  It still doesn't offer HTML rendering, but it does sport a very very nice sub-pixel unhinted font rendering.  It's very easy to read the text, but even better than that, it runs a multitude of IF-engines thanks to the number of included interpreters (the site quotes these as Agility, Alan 2 and 3, Frotz, Glulxe, Hugo, Level 9, Magnetic, Scare, TADS2 and 3.)  You can get it from either the Google Project page or, if you have Debian, Ubuntu or Mandriva and don't want to compile it yourself, from here.  The Google Project page does have TADS multimedia functionality currently listed in the issues section so it may still be implemented in the future.  However, after trying the 1893 AWFM demo in HTML TADS under Wine, I was hooked, and liked it so much that I sent off for a full version from the publisher.

 If you want to try out some Interactive Fiction for yourself, try browsing the games on IFDBIFWiki or reading the SPAG (Society for the Promotion of Adventure Games) e-zine.

Or, if you're feeling creative, why not create one yourself.  The Inform 7 IDE for Gnome has pre-built packages to install for Fedora, Arch, Ubuntu and Debian. (note: I found the Ubuntu version on there to not quite work for Jaunty Jackalope, so if you're also on Ubuntu 9.04, try getting the Ubuntu2 version from the SourceForge project page).  The Inform 7 IDE has built-in comprehensive documentation and an impressive set of tools to help create, test and design interactive fiction.  Also, the natural language used to program it might interest those who are usually daunted by other programming languages.  Who knows, you could be in the running for an XYZZY award later this year.

 

What makes a good package manager?

This is a question that has been bothering me for a while, after realising that pacman, while being aufully close, isn't actualy perfect. Here are my requirements:

Dependecie tracking:

pacman - Yes

apt-get - yes

rpm - Yes

Conflict tracking:

pacman - Yes

apt-get - Yes

rpm -  Yes

Access to build (compile) enstructions:

pacman - Yes, via ABS

apt-get - Not as far as I know

rpm - As above

Downloads binary:

 pacman - Yes

apt-get - Yes

rpm - Yes

Tracks user created source compiles:

pacman - Yes, via makepkg

apt-get - Yes

rpm - No

Repos must be resonable size (5000+) :

pacman (Archlinux) - Yes

apt-get (Debian) - Definatly

rpm (Fedora) - Yes

Flexible, with various options for things:

pacman - Yes, up to 5 options for install, remove and upgrade, with more non spesific ones.

 apt-get - Yes, though not particularly flexible in my experience

rpm - Yes, though equaly inflexible

 

What I'm trying to say here is that pacman is the best! Actualy, what I'm saying is that pacman is good, but we must remeber how small in comparison it's repos are to debians giant repos. It shows that they all do what you want, just some better than others

 

Dual Mouse :D

Ah, this is life, I can use two mouses at ones, one on my table and one on my knee :D.

This way I don't need to stretch a lot, resulting with a healthy back.

 

Hello Linux.com

This is my first entry to my linux.com blog. The site is really good. Thanks for everyone
 

Presto: Speed up your updates and save bandwidth

A little background information

Delta RPMs (DRPMs) are very similar to binary (regular) RPMs. The main difference is that DRPMs contain only the changes between two versions of an RPM package. This allows you to do full updates in a lot less time - Instead of downloading a full 10MB for an update where only 50kb of content changed, for example, you can now download only that 50kb of change and apply it to your system.

Presto is a project which brings deltarpm and yum together; In other words, letting you use yum to apply DRPMs.

Not only will you save on bandwidth since you're only downloading in the changes in a package, but you'll also cut down on the time it takes to download and apply the packages.

Installing yum-presto

The first step toward setting up Presto is installing the yum plugin:
Code:
yum -y install yum-presto
Configure the Updates repository

Next, we need to configure your updates repository to download deltarpm packages instead of the full ones.

Fedora 8 and newer
In the /etc/yum.repos.d/fedora-updates.repo file you'll find two lines that looks like this in the [updates] section:
Code:
#baseurl=http://download.fedoraproject.org/pub/fedora/linux/updates/$releasever/$basearch/
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=updates-released-f$releasever&arch=$basearch
Change it to:
Code:
#baseurl=http://download.fedoraproject.org/pub/fedora/linux/updates/$releasever/$basearch/
#mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=updates-released-f$releasever&arch=$basearch
mirrorlist=http://presto-mirrors.anmar.eu.org/mirrorlist?repo=updates-released-f$releasever&arch=$basearch
The added mirror list will give yum a list of the Presto-enabled mirrors. Of course,if all the DRPM mirrors fail it will always drop back to the original mirror list.

Fedora 8 and 9 users only
2008/09/14: Because of the recent security issue with the Fedora repositories, it is required to change a second repositority configuration file. In the
/etc/yum.repos.d/fedora-updates-newkey.repo file, comment out the old mirrorlist just like above and add this line:
Code:
mirrorlist=http://presto-mirrors.anmar.eu.org/mirrorlist?repo=updates-released-f$releasever.newkey&arch=$basearch

If you've previously followed this howto (pre-June 2008)
There's been an update by the presto team, so if you've followed this howto before June 2008, undo the changes then follow the section above.
In the /etc/yum.repos.d/fedora-updates.repo file you'll find two lines that looks like this in the [updates] section:
Code:
#baseurl=http://dl.anmar.eu.org/repos/f$releasever/updates-$basearch
#mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=updates-released-f8&arch=$basearch
Remove the pound character to the start of the mirrorlist line so that it looks like this:
Code:
#baseurl=http://dl.anmar.eu.org/repos/f$releasever/updates-$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=updates-released-f8&arch=$basearch
  • for i386 (32 bit users), remove the line:
    Code:
    baseurl=http://lesloueizeh.com/f8/i386/updates
  • for x86_64 (64 bit users), remove the line:
    Code:
    baseurl=http://dl.anmar.eu.org/repos/f8/updates-x86_64

Fedora 7
In Fedora 7, the deprecated deltaurl= key is used. This sound bad, however it actually makes the configuration much easier! Simply add the following line to the /etc/yum.repos.d/fedora-updates.repo file just "mirrorlist=" line in the [updates] section:
  • for i386 (32 bit users), add:
    Code:
    deltaurl=http://lesloueizeh.com/f7/i386/updates
  • for x86_64 (64 bit users), add:
    Code:
    deltaurl=http://lesloueizeh.com/f7/x86_64/updates

That's it! Now run you can use yum or yumex as normal and benefit from the advantages of deltarpms.
 

Big Endian or Little Endian.

#include <stdio.h>

int w = 0x41000042;
int main()
{

if( 'A' == *(char*)&w ) {
printf("First char in integer is %c ", *(char*)&w);
printf(", so Big Endian\n");
} else {
printf("First char in integer is %c ", *(char*)&w);
printf(", so little endian\n");
}
}

 

Network Card Bonding On RedHat

In the following I will use the word bonding because practically we will bond interfaces as one. Bonding allows you to aggregate multiple ports into a single group, effectively combining the bandwidth into a single connection. Bonding also allows you to create multi-gigabit pipes to transport traffic through the highest traffic areas of your network. For example, you can aggregate three megabits ports into a three-megabits trunk port. That is equivalent with having one interface with three megabytes speed.

Where should I use bonding?

You can use it wherever you need redundant links, fault tolerance or load balancing networks. It is the best way to have a high availability network segment. A very useful way to use bonding is to use it in connection with 802.1q VLAN support (your network equipment must have 802.1q protocol implemented).

Diverse modes of bonding:

mode=1 (active-backup)
Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior of this mode.

mode=2 (balance-xor)
XOR policy: Transmit based on [(source MAC address XOR'd with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

mode=3 (broadcast)
Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

mode=4 (802.3ad)
IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.
Prerequisites: * Ethtool support in the base drivers for retrieving the speed and duplex of each slave.
* A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode.

mode=5 (balance-tlb)
Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.
* Prerequisite: Ethtool support in the base drivers for retrieving the speed of each slave.

mode=6 (balance-alb)
Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the source hardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server. Also you can use multiple bond interface but for that you must load the bonding module as many as you need.

Example:

In the /etc/modprobe.conf file add the following:

alias bond0 bonding
options bond0 miimon=80 mode=5

In the /etc/sysconfig/network-scripts/ directory create ifcfg-bond0:

DEVICE=bond0
IPADDR=(ip address)
NETMASK=
NETWORK=
BROADCAST=
GATEWAY=
ONBOOT=yes
BOOTPROTO=none
USERCTL=no

Change the ifcfg-eth0 to:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
MASTER=bond0
SLAVE=yes

Change the ifcfg-eth1 to:

DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
MASTER=bond0
SLAVE=yes

That´s all! Now your trunk should be up and running!

 
Page 120 of 133

Upcoming Linux Foundation Courses

  1. LFD320 Linux Kernel Internals and Debugging
    03 Nov » 07 Nov - Virtual
    Details
  2. LFS416 Linux Security
    03 Nov » 06 Nov - Virtual
    Details
  3. LFS426 Linux Performance Tuning
    10 Nov » 13 Nov - Virtual
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board