0

Need your help on below script where I am testing if file found at location /log/TestFile* and id there is only one file then it will display "File successfully Found" else "note found". my second condition - [ $sfCount == 1 ]; is not working. please help me with that.

sfCount=`ls -l /log/TestFile* | awk '{print $9}'| wc -l |sed "s/[[:blank:]]//g" | sed '/^$/d' | sed 's/\n//g'`
sFile=`ls -l /log/TestFile* | awk '{print $9}'`

if [ -e /log/$sFile ] && [ $sfCount == 1 ]; then
    echo "File successfully Found"
    exit 0
else 
    echo "File Not found"
    exit 1
fi
1
  • Try adding echo "<$sfCount>" to see what really is in the variable.
    – choroba
    Nov 12, 2014 at 9:21

2 Answers 2

0

You can get the file count much more easily by doing the following:

ls /log/TestFile* | wc -l

That will relieve you of all the other. I'm not sure why you are getting a count of the number of files, and also putting them into an array to see if they exist. From what it looks like, you just want to see if they exist. You only need the first command for that. So, what we are left with is:

if [ $(ls /log/TestFile* 2>/dev/null | wc -l) != 0 ]; then
    echo "File Found"
else 
    echo "File not found"
fi

The "2>/dev/null" will suppress output to STDERR if there are no files found. We simply want 0, or not 0.

0

Sorry, I have to correct above and its working fine

----------------- Script start here ------------------------- 
sfCount=ls -l /log/TestFile* | awk '{print $9}'| wc -l |sed "s/[[:blank:]]//g" | sed '/^$/d' | sed 's/\n//g' 
sFile="TestFile*"

if [ -f $IB_DIR/$sFile ] && [ $sfCount == 1 ]; then
    echo "File successfully Found"
    exit 0
else 
    echo "File Not found"
    exit 1
fi
1
  • Its not clear to me if you added information to your question or if you answered your own question. I think you answered your own question, and that's OK (see How does accepting an answer work?). In this case, you should accept your own answer to make it clear to future visitors.
    – jww
    Nov 13, 2014 at 4:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .