[CS-FSLUG] Slackware bash goodies

Brian Derr bderr at myrealbox.com
Fri Jul 23 07:43:12 CDT 2004


Greetings Slackers and you other folks,

I just came across a great idea on comp.os.linux.slackware last night
and modified it a bit for my own use.  The idea was using bash functions
for small mundane tasks.  I always made a bash script for small little
things, but it causes overhead calling another bash instance.  Why do
that when you can have bash think what you're calling is part of itself?

brian at hefeweizen:~$ cat ~/.bashrc
spls () {
   ls -1 /var/log/packages |
   grep -v "CHECKSUM" |
   less
}

sp () {
   ls -l /var/log/packages |
   grep "$1"
}

spinfo () {
   head -n 16 /var/log/packages/"$1"*
}

spgrep () {
   grep "$1" /var/log/packages/*
}

which () {
   (alias; declare -f) |
   /usr/bin/which --tty-only --read-alias \
   --read-functions --show-tilde --show-dot $@
}

This is Slackware specific but you other folks can at least learn the
concept if not steal my hard work (all five minutes of it).  Lets look
at the first example.

  spls () {
     ls -1 /var/log/packages |
     grep -v "CHECKSUM" |
     less
  }

This is very straight forward, when I call spls from the command line
bash will give a single column listing of /var/log/packages, remove the
CHECKSUMS.MD5 file (a swaret thing I think) then pipe the ls to less for
easy viewing.

sp() actually takes a parameter like this:

   brian at hefeweizen:~$ sp aaa
   -rw-r--r--  1 root root   2184 Jun 16 21:51 aaa_base-10.0.0-noarch-1

You give it the name of a package and it will grep the list to see if it
is installed or not.  Sure you can use swaret, but this is _a lot_
quicker.  Since it uses regexp's you don't have to type the package name
exactly as it appears.  With rpm you would have to type aaa_base, with
this method I could search for aaa or a_b, etc.

spinfo() is a neat one that I am particularly proud of:  :-)

   brian at hefeweizen:~$ spinfo aaa
   PACKAGE NAME:     aaa_base-10.0.0-noarch-1
   COMPRESSED PACKAGE SIZE:     8 K
   UNCOMPRESSED PACKAGE SIZE:     80 K
   PACKAGE LOCATION: ./aaa_base-10.0.0-noarch-1.tgz
   PACKAGE DESCRIPTION:
   aaa_base: aaa_base (Basic Linux filesystem package)
   aaa_base:
   aaa_base: Sets up the empty directory tree for Slackware and adds an
   email to
   aaa_base: root's mailbox welcoming them to Linux. :)  This package
   should be
   aaa_base: installed first, and never uninstalled.

Takes a regexp and will output the description for the package.  Ever
wondered if you needed that package and didn't know what it is for, now
you do.

And finally, spgrep() which allows you to find out what package owns a
file:

   brian at hefeweizen:~$ spgrep ospam
   /var/log/packages/sgml-tools-1.0.9-i486-12:usr/bin/ospam

Last night I found the file /usr/bin/spam and didn't know what it was
for.  It was a symlink to /usr/bin/ospam and still didn't ring a bell.
So I spgrep'ed it and voila, it belongs to the sgml-tools package.  :-)
You can actually search for anything and any hit will bring up the
package name.  I might work out a few kinks in this one so it doesn't
display hits in the package description, just the file lists.

Lastly is which(), which is a wrapper for /usr/bin/which.  Taken
straight from the which(1) manpage this will show you various bash
functions you have as well as maintain normal which functionality.

   brian at hefeweizen:~$ which spls
   spls ()
   {
       /usr/bin/ls $LS_OPTIONS -1 /var/log/packages | grep -v "CHECKSUM"
       | less
   }

There you have it folks, bash goodies for fun and amusement.  You too
can have such marvelousness in your ~/.bashrc for the nominal fee of 3
easy payments of just $19.95.  Please, no CODs.  ;-)

Don't forget to source your .bashrc file since bash doesn't seem to do
it on it's own.  In ~/.bash_profile you can add ". ~/.bashrc" or if
you're adventurous you can add this to your /etc/profile:

if [ -e $HOME/.bashrc ]; then
   . $HOME/.bashrc
fi

That way everyone on your system that may or may not have a .bashrc can
have it sourced automagically for them.  Well, I should start packing
for my camping trip.  See ya.

Brian

-- 
The just man walketh in his integrity:
his children are blessed after him.  -- Proverbs 20:7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://ofb.biz/pipermail/christiansource_ofb.biz/attachments/20040723/e29dd999/attachment.sig>


More information about the Christiansource mailing list