Mailing List Archive

Support open source code!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tlug: bash ?

tlug note from "Stephen J. Turnbull" <>
>>>>> "Jim" == Jim Schweizer <> writes:

    Jim> Purpose: find files by date and rm, cat, cp or whatever.

    Jim> This works fine:

    Jim> ls -l | grep -i 'feb 27' | cut -c56-

    Jim> for finding todays files, but I want to write a shell script
    Jim> for it like:

This isn't a _script_, it's a _shell function_.  The difference is it
won't work under other shells, and it doesn't invoke a subshell.

    Jim> function lsdate {

Problem:  The `()' after lsdate is missing.  It turns out that the
keyword `function' can be omitted if there are parens, but the parens
are necessary (at least if you want to use positional parameters).
See "Tip:" below.

    Jim>   date=$1
    Jim>   ls -l | grep -i "^.\43\}$date" | cut -c56-
What's this, gomi?

    Jim> }

    Jim> then call the script like lsdate 'feb 27' but the script
    Jim> isn't accepting the filename arguments.

If you simply put `ls -l | grep -i "$1" | cut -c56-' in a file it
should do what you want (once you put the file on your path and chmod
it to be executable).  For safety put `#!/bin/sh' as the first line.
As a function, you need the syntax `function func () { ... }' in your
.bashrc or .profile (or type it at the command line).

Tip:  having defined a function `squash' at the command line, you can
then debug it, and use the `declare' builtin to add it to your .bashrc:

bash-2.00$ function squash () {
>     local d=`date +%y%m%d`;    # local declares a local variable so
>     mv $1 $1.$d;               # you don't step on any predefined values
>     gzip $1.$d
> }
bash-2.00$ declare -f squash
squash () 
    local d=`date +%y%m%d`;
    mv $1 $1.$d;
    gzip $1.$d
bash-2.00$ declare -f squash >>.bashrc

Of course you should go edit .bashrc to use the explicit syntax
beginning with `function'.

    Jim> I realize this is pretty basic, but what am I doing wrong?

    Jim> BTW, the new XFMail mentioned in this month's Linux Gazette

I'm having too much fun hacking on mh-e.el ;-)

    Jim> is pretty spiffy. No more limits on text in the editor,
    Jim> ispell works fine with it and it loads much faster. My only

Who needs ispell :-P

    Jim> viewer, etc) It doesn't appear to support kanji but it does


                            Stephen J. Turnbull
Institute of Policy and Planning Sciences                    Yaseppochi-Gumi
University of Tsukuba            
Tel: +81 (298) 53-5091;  Fax: 55-3849    
a word from the sponsor will appear below
The TLUG mailing list is proudly sponsored by TWICS - Japan's First
Public-Access Internet System.  Now offering 20,000 yen/year flat
rate Internet access with no time charges.  Full line of corporate
Internet and intranet products are available.
Tel: 03-3351-5977   Fax: 03-3353-6096

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links