LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2006, 10:56 AM   #1
hbinded
Member
 
Registered: May 2006
Distribution: Slackware & MyLFScratch
Posts: 37

Rep: Reputation: 15
[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)
 
Old 12-19-2006, 11:04 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Well, if .tar.bz2 exists, it exits immediately... Maybe you should simply reorder your cases?
 
Old 12-19-2006, 12:47 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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
 
Old 12-19-2006, 05:15 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
check for file existence ignoring case sensitive rammohhan2000 Programming 3 06-13-2006 03:12 PM
how does grub knows about existence of other OS? mohit_garg Linux - Newbie 29 07-13-2005 07:24 PM
Device existence problem..... roshu Mandriva 2 02-07-2005 06:01 AM
Scanner's temporal existence phantompgr Fedora 0 10-14-2004 06:51 PM
check for existence of file j-me Linux - Newbie 5 07-29-2003 07:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration