What is wrong with this bash script?

It should say that test.zip is a zip file, but the if statments, which check for TAR in the file name, all return true… If I do this outside of an if statement, say at the shell prompt, it works correctly.

What is wrong with this statement:

[dos]
#$bash

ArchiveName=”test.zip”
cur_file=”test.zip”
echo “ArchiveName: $ArchiveName”

if [ $(echo $ArchiveName | grep “tar$” -i)=$ArchiveName ]
then
echo Test1: It is a tar file
else
echo Test1: It is a zip file
fi

if [ $(echo $ArchiveName | grep “tar.gz$” -i)=$ArchiveName ]
then
echo Test2: It is a tar file
else
echo Test2: It is a zip file
fi

if [ $(echo $ArchiveName | grep “tar$” -i)=$ArchiveName ] || [ $(echo “$ArchiveName” | grep “tar.gz$” -i)=$ArchiveName ]
then
echo Test3: It is a tar file
else
echo Test3: It is a zip file
fi

[/dos]

Is there a better way to do this?

3 thoughts on “What is wrong with this bash script?”

  1. if [ $(echo $ArchiveName | grep -i “tar$” ) ]
    then
    echo Test1: It is a tar file
    else
    echo Test1: It is a zip file
    fi

    that works for me. (i’m using unix, so put your -i back where it belongs)

  2. Cripes.

    [DOS]
    if [ $(echo $ArchiveName | grep -i “tar$” ) ]
    then
    echo Test1: It is a tar file
    else
    echo Test1: It is a zip file
    fi
    [/DOS]

    You need a preview plugin 🙂

  3. Thanks Cindy!

    That works Great!

    [DOS]
    #$bash

    ArchiveName=”test.tar”
    #ArchiveName=”test.zip”
    #ArchiveName=”test.tar.gz”
    echo “ArchiveName: $ArchiveName”

    if [ $(echo $ArchiveName | grep -i “tar$” ) ]
    then
    echo Test1: It is a tar file
    else
    echo Test1: It is not a tar file
    fi

    if [ $(echo $ArchiveName | grep -i “tar.gz$” ) ]
    then
    echo Test2: It is a tar.gz file
    else
    echo Test2: It is not a tar.gz file
    fi

    if [ $(echo $ArchiveName | grep -i “tar$” ) ] || [ $(echo $ArchiveName | grep -i “tar.gz$” ) ]
    then
    echo Test3: It is a tar file
    else
    echo Test3: It is a zip file
    fi
    [/DOS]

Leave a Reply

Your email address will not be published. Required fields are marked *