Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2003
    Posts
    23

    Unanswered: I need little help with "if test"

    Hello

    I need make a script in Linux. It don`t work when I have several files in the directory (it return ./script: line 1: test: too many arguments), only when I have one file it work fine. I need make several actions when I have files, but I have this problem. Can anybody help me.

    this is the script

    if test -r /ftp/GESTOP/AI/entrada/INV*
    then
    ftp prodinven > /home/sant/inven/log/ftp.log 2>&1
    exit 0
    else
    exit 1
    exit 0
    exit 1
    fi

  2. #2
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Once INV* is expanded, it may stand for multiple files. Those files will be separated by spaces and, thus, cause a syntax error if used with "test". You could iterate over all INV* files:
    Code:
    for f in "" /ftp/.../INV*; do
        if [ -n "$f" ]; then
            ftp prodinven > ...
            exit $?
        fi
    done
    Another alternative may be to list all INV* files and then check if the listing is empty or not.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Hey stolze - very nice code! - but you should explain why it's better than the original so sant can learn from it rather than just copying it in.

    Mike

  4. #4
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    My first sentence was hinting at the problem. INV* is expanded first, leading to something like
    Code:
    INV1 INV2 INVwhatever
    If you inject that into your original statement, you get:
    Code:
    if test -r INV1 INV2 INVwhatever
    When your shell parses that, it finds "test -r INV1" and then doesn't know what to do with INV2 and raises a syntax error. Thus, you have to accommodate for a variable number of files after expansion.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •