LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 03: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, 04:32 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 04: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 04:51 AM.
 
Old 09-26-2007, 05: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, 06:51 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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 06:52 AM.
 
Old 09-26-2007, 07: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, 08:57 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:17 PM.

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