LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-26-2007, 04:51 AM   #1
prernasin
LQ Newbie
 
Registered: Sep 2007
Posts: 5

Rep: Reputation: 0
Unhappy how to find files using bash script


hi there,

i am given a bash exercise which asks me to find files in a directory. there are two files 'file1' and 'file2'. i need to write a script which displays 0 if in a directory those two particular files dont exist, displays 1 if file1 exists and displays 2 if those two particular file file1 and file2 exist in that directory.

I tried to write some code but i am getting error!!!

Please help!!!
 
Old 09-26-2007, 05:32 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,485

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
LQ has rules against asking homework questions - all your posts so far have been this homework.
Show us what you have tried, and the failure - then we can help point you in the right direction.
 
Old 09-26-2007, 05:45 AM   #3
prernasin
LQ Newbie
 
Registered: Sep 2007
Posts: 5

Original Poster
Rep: Reputation: 0
mywork

hello... i've writtent the following code...i don't know how to use AND operator and how to use if fi in bash...below is my work:

#dnt knw how to use AND operator in bash
if [ -e file1.pl -a -e file2.pl ] ;
then printf "2: two files exist\n"
printf "=======================================\n"
# dont knw how to use else if in bash...
elsif [ -e file1.pl] ;
printf "1: 1 file exists\n"
printf "=======================================\n"
# dnt knw how to show that the directory is empty
elsif [.......]
printf "0: no file exists\n"
printf "=======================================\n"

Last edited by prernasin; 09-26-2007 at 05:51 AM.
 
Old 09-26-2007, 06:16 AM   #4
prernasin
LQ Newbie
 
Registered: Sep 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
LQ has rules against asking homework questions - all your posts so far have been this homework.
Show us what you have tried, and the failure - then we can help point you in the right direction.

hello... i've writtent the following code...i don't know how to use AND operator and how to use if fi in bash...below is my work:

#dnt knw how to use AND operator in bash
if [ -e file1.pl -a -e file2.pl ] ;
then printf "2: two files exist\n"
printf "=======================================\n"
# dont knw how to use else if in bash...
elsif [ -e file1.pl] ;
printf "1: 1 file exists\n"
printf "=======================================\n"
# dnt knw how to show that the directory is empty
elsif [.......]
printf "0: no file exists\n"
printf "=======================================\n"
 
Old 09-26-2007, 07:51 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
The "-a" construct you're using is from the "test" command, of which the "[" command probably is an alias.
When you use [ in bash however, you use the BUILTIN [ command, the syntax of which is in the bash man page. In there, you'll see what bash thinks of "-a"
Code:
-a file
       True if file exists.
I suggest you read the bash man page (man bash), where you'll learn how to express the AND and OR concepts, how to properly use the if construct, and much much more.

Yves.

Last edited by theYinYeti; 09-26-2007 at 07:52 AM.
 
Old 09-26-2007, 08:47 AM   #6
dgar
Member
 
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
The bash builtin 'help' command is good too:
help test
help [
 
Old 09-26-2007, 09:57 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
What reference text are you using for bash? I recommend Bash Guide for Beginners--free at tldp .org
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 08:42 PM
A bash script to find duplicate image files fotoguy Programming 7 01-25-2007 07:47 PM
Writing a bash script to find and register vmware vms juchestyle Programming 6 01-07-2007 01:03 PM
Trying to find the files that contain distro information for my bash script ethics Linux - General 5 07-04-2006 05:48 AM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration