LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Question] Check File existence (https://www.linuxquestions.org/questions/programming-9/%5Bquestion%5D-check-file-existence-511953/)

hbinded 12-19-2006 10:56 AM

[Question] Check File existence
 
Hi, I'm trying to write s simple script, that will check if a file exists and echo if found or not. Here is a piece of the script.
Code:

#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo "$(color ltgreen)usage: $(color off)$0 $(color ul)<argument>$(color off)";
else
if [  -e $1.tar.bz2 ]; then
    echo "$1.tar.bz2";
    exit 0
else
if [ ! -e $1.tar.bz2 ] && [ ! -e $1.tar.gz ]; then
    echo "$(color ltred).::ERROR::.$(color off):$(color ul)no$(color off) file found"
    exit 1
else
if [  -e $1.tar.bz2 ] && [  -e $1.tar.bz2 ]; then
    echo "Both Exist!!";
else
    echo "$1.tar.gz";
    exit 1
fi
fi
fi
fi

So what it basically does is to check if the user's input exists in a "tar.bz2" file or "tar.gz" file. It kinda works, but I noticed if a tar.bz & a tar.gz of the same name exist, it only outputs the tar.bz2 filename. Is there a simpler (or even better way to do this)

raskin 12-19-2006 11:04 AM

Well, if .tar.bz2 exists, it exits immediately... Maybe you should simply reorder your cases?

95se 12-19-2006 12:47 PM

I think for this line,
Code:

if [  -e $1.tar.bz2 ] && [  -e $1.tar.bz2 ]; then
You meant,

Code:

if [  -e $1.tar.bz2 ] && [  -e $1.tar.gz ]; then

chrism01 12-19-2006 05:15 PM

Possibly you should use 'elif' instead of 'else if', and also take a look at 'case';
http://www.faqs.org/docs/bashman/bashref_20.html#SEC20


All times are GMT -5. The time now is 04:15 PM.