Testing file size and file exist? - Help

This is a discussion on Testing file size and file exist? - Help ; I want to perform a couple of statements if either the file does not exist OR the file size of a given file is equal 0. Currently the best solution I can imagine is something like: if [! -e myfile.dat ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Testing file size and file exist?

  1. Testing file size and file exist?

    I want to perform a couple of statements if either the file does not exist OR the file size of a given file
    is equal 0. Currently the best solution I can imagine is something like:


    if [! -e myfile.dat ]; then
    echo "File not existing or file size = 0"
    do_something
    else
    filesize=`wc -c myfile.dat`
    echo "filesize=$filesize"
    if [ $filesize = 0 ]; then
    echo "File not existing or file size = 0"
    do_something
    fi
    fi

    The first problem I encounter is that the back ticks are NOT recognized by the shell interpreter.
    filesize contains blank after the wc command. Why?

    Is there a filesize testing without back quotes instructions?

    Secondly, I feel that there is a (much) shorter way of doing this.
    Has someone a better idea?

    Thx
    Matt


  2. Re: Testing file size and file exist?

    Matt Benson wrote:
    > I want to perform a couple of statements if either the file does not exist OR the file size of a given file
    > is equal 0. Currently the best solution I can imagine is something like:
    >
    >
    > if [! -e myfile.dat ]; then
    > echo "File not existing or file size = 0"
    > do_something
    > else
    > filesize=`wc -c myfile.dat`
    > echo "filesize=$filesize"
    > if [ $filesize = 0 ]; then
    > echo "File not existing or file size = 0"
    > do_something
    > fi
    > fi
    >
    > The first problem I encounter is that the back ticks are NOT recognized by the shell interpreter.
    > filesize contains blank after the wc command. Why?
    >
    > Is there a filesize testing without back quotes instructions?
    >
    > Secondly, I feel that there is a (much) shorter way of doing this.
    > Has someone a better idea?
    >
    > Thx
    > Matt
    >

    Leaving the backticks aside for a bit, under bash, at any rate, the
    `!' character needs to be in front of the brackets, separated by a
    space, for the test to be negated properly.

    You might try fixing that, then see how the backticks work.

  3. Re: Testing file size and file exist?

    On Wed, 16 Mar 2005 17:03:30 -0800, Bryan Batten wrote:
    | Matt Benson wrote:
    |> I want to perform a couple of statements if either the file does not exist OR the file size of a given file
    |> is equal 0. Currently the best solution I can imagine is something like:
    |>
    |>
    |> if [! -e myfile.dat ]; then
    |> echo "File not existing or file size = 0"
    |> do_something
    |> else
    |> filesize=`wc -c myfile.dat`
    |> echo "filesize=$filesize"
    |> if [ $filesize = 0 ]; then
    |> echo "File not existing or file size = 0"
    |> do_something
    |> fi
    |> fi
    |>
    |> The first problem I encounter is that the back ticks are NOT recognized by the shell interpreter.
    |> filesize contains blank after the wc command. Why?


    You could try the POSIX replacement for backticks: $( ... ) is equivalent
    to ` ... ` but is more visible, nests easier (without massive and tricky quoting),
    and works inside double-quoted strings "like $( this )".

    Also, the 'wc' command generates leading whitespace in it's results,
    which is probably stuffing up your variable assignment.


    |> Is there a filesize testing without back quotes instructions?


    "man test" will give you a list of the available tests.


    |> Secondly, I feel that there is a (much) shorter way of doing this.
    |> Has someone a better idea?
    |>
    |> Thx
    |> Matt
    |>
    | Leaving the backticks aside for a bit, under bash, at any rate, the
    | `!' character needs to be in front of the brackets, separated by a
    | space, for the test to be negated properly.
    |
    | You might try fixing that, then see how the backticks work.

    --
    Reverend Paul Colquhoun, ULC. http://andor.dropbear.id.au/~paulcol
    Asking for technical help in newsgroups? Read this first:
    http://catb.org/~esr/faqs/smart-questions.html#intro

  4. Re: Testing file size and file exist?

    Bryan Batten wrote:
    > ...
    > Leaving the backticks aside for a bit, under bash, at any rate, the `!'
    > character needs to be in front of the brackets, separated by a space,
    > for the test to be negated properly.
    >
    > You might try fixing that, then see how the backticks work.


    Woops! I should have meant '[ !', that is, bracket, space, bang.

    Sorry

+ Reply to Thread