Troubleshooters.Com and T.C Linux Library Present

Linux Quick Hacks

Copyright (C) 2000-2001, 2003 by Steve Litt, All rights reserved. Material provided as-is, use at your own risk. 


Contents

Ascii Character Lister

To list printable characters and their ascii values, do the following:
perl -e 'foreach $x (32..126){print "_" . chr($x) . "  " . $x . "\n"}' | grep '_\$'
To find the ascii code for a specific character (other than the space character, use $ in this example), do this:
perl -e 'foreach $x (32..126){print "_" . chr($x) . "  " . $x . "\n"}' | grep '_\$'
Normal characters don't need and can't use the backslash, so to find the asci value of lower case 'd' you'd do this:
perl -e 'foreach $x (32..126){print "_" . chr($x) . "  " . $x . "\n"}' | grep '_d'


To find the character corresponding to an ascii value, do this (example uses 100 as the ascii value):

perl -e 'foreach $x (32..126){print "_" . chr($x) . "  " . $x . "\n"}' | grep ' 100'

Man pages formatted as text

Man pages look great on console screens, but piped to files or to browsers they quickly degenerate into cluttered conglomerations of reverse linefeeds and backspaces designed to simulate "bold" on your console screen. You get rid of the clutter by piping the output of man through thecol command. With no args, col blows off the reverse linefeeds but leaves the backspaces (^H). The command you want (using the ls man page as an example) is:
man ls | col -bx > myfile.txt
The preceding command writes the man page to myfile.txt, without backspaces (the -b) and with spaces substituted for tabs (-x).

Man pages formatted as html

Here's a CGI script that uses a command similar to that discussed in the preceding section to output a man page to a browser. I call it mann.cgi:
 
#!/usr/bin/perl -w
#PUBLIC DOMAIN, NO WARRANTEE, AUTHOR AND DISTRIBUTORS NOT RESPONSIBLE FOR
#ANY DAMAGE CAUSED BY THIS PROGRAM OR ITS DEFECTS. USE AT YOUR OWN RISK!

# UCITA STINKS!

use strict;

my($cmd);

sub GetData()
  {
  my($line);
  read(STDIN,$line,$ENV{"CONTENT_LENGTH"});
  if($line =~ m/Tman=(.*)&Bsub/)
    {
    $cmd = $1;
    }
  elsif($line =~ m/Tman=(.*)$/)
    {
    $cmd = $1;
    }
  else
    {
    $cmd = "InternalError";
    }
  }

sub MakePageTop()
  {
  print "Content-type: text/html\n\n";
  print "<html><head><title>Man Page Displayer</title></head><body>\n";

  print "<center><big><big><big><strong>\n";
  print "Man Page Displayer<p>\n";
  print "</strong></big></big></big></center>\n";
  }

sub MakeForm()
  {
  print "<FORM ACTION=\"./mann.cgi\" METHOD=\"POST\">\n";
  print "<b>Command to look up:\n";
  print "<input type=\"text\" name=\"Tman\" size=\"24\"";
  print " value=\"$cmd\"></b>\n";
  print "<INPUT type=\"submit\" name=\"Bsub\" value=\"Submit\">\n";
  print "</form>"; 
  }

sub MakeManPage()
  {
  print "<pre><b>";
  print `man $cmd -a | col -bx`;
  print "\n</b></pre>";
  }

sub MakePageBottom()
  {
  print "</body></html>\n"; 
  }

sub main()
  {
  GetData();
  MakePageTop();
  MakeForm();
  MakeManPage();
  MakePageBottom();
  }

main();

Shellscript File Tests

Here's a script to test for a file's existence:
if [ -e $file ]; then
 ./myUtil $file
fi
 
TEST MEANING
[ -b $file ] True if file exists and is block special.
[ -c $file ] True if file exists and is character special.
[ -d $file ] True if file exists and is a directory.
[ -e $file ] True if file exists.
[ -f $file ] True if file exists and is a regular file.
[ -g $file ] True if file exists and is set-group-id.
[ -k $file ] True if file has its ``sticky'' bit set.
[ -L $file ] True if file exists and is a symbolic link.
[ -p $file ] True if file exists and is a named pipe.
[ -r $file ] True if file exists and is readable.
[ -s $file ] True if file exists and has a size greater than zero.
[ -S $file ] True if file exists and is a socket.
[ -t $fd  ] True if fd is opened on a terminal.
[ -u $file ] True if file exists and its set-user-id bit is set.
[ -w $file ] True if file exists and is writable.
[ -x $file ] True if file exists and is executable.
[ -O $file ] True if file exists and is owned by the effective user id.
[ -G $file ] True if file exists and is owned by the effective group id.

Directory Size Lister/Sorter

Have you ever wondered which directories and trees take up all the diskspace? Questions like this come during pruning/archiving, backup, and numerous other activities. Make the following oneliner script (I call it dirsizes) to find out:
 
du -sm $(find $1 -type d -maxdepth 1 -xdev) | sort -g

The preceding shellscript prints every directory below the one called as an argument, together with its size, sorted with the largest at the bottom. We sort largest at bottom so there's no necessity to pipe it toless. Instead, you can see the largest 24 on the screen after the command.

If you find a large tree, but can't delete the whole thing, you can explore just that tree by using its directory as the argument, and you'll see all its subtrees and how much space they take.

But let's say you want to see ALL directories in the tree, instantly zeroing in on big diskspace directories. Make the following shellscript, which I call alldirsizes:
 
find $1 -type d  | xargs du -sm | sort -g

Both these scripts do more than just add filesizes. They take into account inodes, so they reveal the space that would be recovered if the directories were deleted.

Both of these scripts are most accurate when run as root, but they're pretty informative run as just a normal user.

CUPS Tips

X-Sender: slitt@207.171.0.150
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Fri, 24 Nov 2000 11:59:15 -0500
To: leaplist@lists.leap-cf.org
From: Steve Litt <Steve Litt's email address>
Subject: [LeapList] Cups tips
Reply-To: leaplist@lists.leap-cf.org
Sender: leaplist-admin@lists.leap-cf.org
X-Mailman-Version: 1.0rc2
List-Id: General Linux Discussion List for LEAP <leaplist.lists.leap-cf.org>
X-BeenThere: leaplist@lists.leap-cf.org

On my mandrake 72 I noticed that using lpr to print a text file was
printing too close to the left and cutting off half the first character.
This is a cups system.

So I looked at http://localhost:631, which is the cups equivalent of swat.
It pointed me to a cups manual, which noted that the lpr command can be
used to change margins:

lpr -o page-left=24 /etc/smb.conf

The preceding prints smb.conf with a left margin of 1/3 inch (24/72). There
are a smorgasbord of options for duplexing, characters per inch, page
ranges (and isn't that nice when a .ps or .pcl file jams in the middle),
and tons of other stuff. However, I couldn't make the brightness setting
work right, as when I made the text darker, the background on the last page
turned dark.

By the way, I'm sure there's some place where I can make the page-left
default 24 points, but I just haven't found it yet.

SteveT

Mouse: Fast and Accurate Mousing Under Linux

By default, the mouse under Linux is typically much too slow, requiring large hand movements to move the pointer. This is inconvenient, and although I'm a layman with regard to ergonomics, I personally fear those large hand movements put me at greater risk for repetative motion injuries. Luckily, this problem is easy to fix.

Many people think the fix is to boost the acceleration. That makes the mouse fast enough, but requires you to move the mouse quickly to traverse large distances, then slowly to zero in on the exact target. With an overly large (like 5) acceleration, mousing resembles golf -- one or two drives followed by a couple of putts. Not good for productivity.

The problem isn't accelleration. The problem is that the pointer moves too few pixels per inch of mouse travel. You should be able to move a good 600 pixels per inch of mouse travel, not the ~200 provided by a default Linux box. The solution is to add a resolution option to the pointer section of /etc/XFConfig86-4 file, as shown below:
 
# **************************************************
# Pointer section
# **************************************************
Section "InputDevice"

    Identifier "Mouse1"
    Driver      "mouse"
    Option "Protocol"    "imps/2"
    Option "ZAxisMapping" "4 5"
    Option "Device"      "/dev/psaux"
    Option "Emulate3Timeout"    "50"
    Option "Resolution" "1600"

# ChordMiddle is an option for some 3-button Logitech mice

#    Option "ChordMiddle"

EndSection    

You'll notice I use 1600 for the resolution. That's above the maximum, so it produces the maximum resolution. The preceding is for my Logitech optical mouse, but the Option "Resolution" "1600" line is appropriate for pretty much any mouse, although it's conceivable that for some mice you might want to go higher or lower than 1600.

After making the preceding addition, you must restart X to see the result.

Once you've upped the resolution, you can tweak the accelleration. Once I've fixed the resolution, I like an acceleration value of 2. Here's the command to get it:

xset m 2 1
To get a 3 to 1 acceleration, use this:
xset m 3 1
To make your mousing experience even better, use a teflon covered mouse pad. It's well worth the extra money.

Although technically trivial, the human-mouse interaction plays a major role in the perceived quality of the computer experience. Don't shortchange yourself. Tweak your mouse to your liking.
 

Smoothwall Tips

The default Smoothwall intall is kewl, but I've found a couple hacks to make it better for my setup. First, as shipped, Smoothwall defaults to the British English keyboard. If you have an American keyboard like I do, you need to change it or various keys like backslash will input the wrong character. Go into file  /etc/rc.d/rc.sysinit and change the following line:
/usr/bin/loadkeys /usr/lib/kbd/keymaps/i386/qwerty/uk.kmap.gz
To this:
/usr/bin/loadkeys /usr/lib/kbd/keymaps/i386/qwerty/us.kmap.gz
I changed uk to us, thereby enabling configuring for my keyboard.

Second, I use Lynx to dial and disconnect Smoothwall. Konqueror doesn't work with Smoothwall as of this writing, and Netscape is much too bloated. But Smoothwall 0.9.8 places 13 links, known as "the menu", before the dial button, meaning to get to the dial button in Lynx I need to press Tab 14 times. I removed the 13 links with the following change:

Change the following line of /home/httpd/cgi-bin/index.cgi:

&openpage($tr{'main page'}, 1, $refresh);
To the following:
&openpage($tr{'main page'}, 0, $refresh);
The second argument of openpage() defines whether or not to show the "menu", which is the 13 links appearing at the top. Because these same links are reproduced below the dial, disconnect and refresh buttons of the main page, they can safely be removed on the main page. Of course, they must be preserved on other pages, as they're the main form of navigation from those pages.
 

Recording, Playing and Converting sounds

First, make sure aumix is installed, and use it to set both the record and play volumes, and make sure the microphone is the record device and not muted. Don't use a GUI mixer -- they sometimes get it wrong.

To record a song from the microphone, just do this:

$ rec mysong.au
If you want to change the volume, use -v, which is a float where >1.0 amplifies, and <1.0 attenuates. Note our hearing is logrithmic.
$ rec -v2 mysong.au
To play it back, do this:
$ play mysong.au
To convert it to a .wav, do this:
$ sox mysong.au mysong.wav
Sox can also amplify, flanger, phaser, echo, and various other effects. See man sox.

Finding Who Has a File Open

How often do mounts and umounts fail because you can't determine what is using the device? How many times can you not eject a CD because something's got it open? How many times have you experienced a "file browser" that keeps a directory open even after you've navigated out of that directory and clicked the "refresh" button? Who's got this thing open??? Use thefuserprogram to find out:
[slitt@mydesk slitt]$ /sbin/fuser -mu /d
/d:                   1693(slitt)  1891c(slitt)  1894  1894c(slitt)
  1907  1907c(slitt)  1908  1908c(slitt)  1909  1909c(slitt)
  1910  1910c(slitt)  1912  1912c(slitt)  1913  1913c(slitt)
[slitt@mydesk slitt]$
You get the owner and the process ID. From there you can research usingps ax.

Be sure to look at the fuser man page for details on other info you can obtain. You can even kill all processes accessing a file, though I highly recommend against such a heavy handed move.

Modem Init Strings

Delay for Voicemail Phones

On some phone systems with voicemail, when there are unheard messages, the dialtone beeps several times. This can prevent the modem from connecting. If you have this problem, you can solve it with a simple modem init string addition.

On a Hayes compatible modem, and the following string onto the end of your modem init string:
S6=4
Register S6 determines the delay before checking for dialtone. By instituting a 4 second delay, by the time you check for a dialtone all the beeps will be gone. If 4 seconds isn't enough, try 10, and then trim it down from there.

The Modem Speaker

The following codes influence the modem speaker:
Code
Function
M0
Modem speaker always off
M1
Modem speaker on during dial and handshake
M2
Modem speaker on always
M3
Modem speaker on during handshake
L codes, if present, should follow M codes
L1
Modem speaker soft
L2
Modem speaker moderate
L3
Modem speaker loud


Back to Troubleshooters.Com * Back to Linux Library