Complete.Org: Mailing Lists: Archives: linux-help: January 2001:
[linux-help] Re: Quick BASH/Perl Question
Home

[linux-help] Re: Quick BASH/Perl Question

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: linux-help@xxxxxxxxx
Subject: [linux-help] Re: Quick BASH/Perl Question
From: Tom Hull <thull@xxxxxxxxxxx>
Date: Wed, 10 Jan 2001 19:26:48 -0600
Reply-to: linux-help@xxxxxxxxx

Curtis Hawthorne wrote:
> 
> First, in BASH, how do I test for the presence of any
> .log (*.log) file in a directory?  If I do "test -a
> *.log" it says there's too many arguments.

One way, the exit status of ls(1) is 0 if the argument files exist, or
1 if one or more files do not exist:

    if ls *.log >/dev/null 2>&1; then
        echo "got some .log files"
    else
        echo "ain't got no .log files"
    fi

You can pipe ls | wc -l to get a file count, but beware of the idiot
whitespace that wc(1) adds:

    nfiles=`ls -d *.log 2>/dev/null | wc -l | tr -d ' '`
    if [ "$nfiles" != 0 ]; then
        echo "got $nfiles .log files"
    else
        echo "ain't got no .log files"
    fi

You could replace the wc/tr with the non-obvious:

    nfiles=`ls -d *.log 2>/dev/null | awk 'END{print NR}'`

You could also do something like:

    set `ls *.md 2>/dev/null`
    nfiles=$#

You could also set and take the dimension of a bash array, but offhand
I don't know how to do that.

-- 
/*
 *  Tom Hull * thull at kscable.com * http://www.ocston.org/~thull/
 */

-- This is the linux-help@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]