Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    56

    Exclamation Unanswered: how to find a file in a directory using shell script

    Hello,

    I want to search for specific file pattern in a directory using the shell script .Written shell script but evey time I am getting file found even though the file pattern of type BI_999 is not found in current directory .


    ls *BI_999.dat > ctrlfileslist.txt
    ctrlfilename=`cat ctrlfileslist.txt`
    if [ -z $ctrlfilename ]
    then
    echo " file not found "
    else
    echo " file found "
    fi

    whats the issue in the code ?

  2. #2
    Join Date
    May 2014
    Posts
    6
    hi,

    use more quotes:
    Code:
    test -z "$variable"
    the temporary file seems to be useless.
    Last edited by Watael; 05-31-14 at 18:40.

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Your script is needlessly complex.
    Either of the following is simpler.

    Code:
    ls *BI_999.dat >/dev/null
    if [ $? = 0 ]
    then
    echo found
    else
    echo not found
    fi




    or
    Code:
    ls *BI_999.dat >ctrlfilelist.txt
    if [ -s ctrlfilelist.txt ]
    then
    echo found
    else
    echo not found
    fi
    or even this, which I did not know until I tried it.
    Code:
    if [ -s *BI_999.dat ]
    then
    echo found
    else
    echo not found
    fi
    Last edited by kitaman; 06-02-14 at 11:07.

  4. #4
    Join Date
    May 2014
    Posts
    6
    the last one will not work if there are more than on file ending with the pattern, or if the file has IFS character in its name.

Posting Permissions

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