LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to find files using bash script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-files-using-bash-script-587431/)

prernasin 09-26-2007 04:51 AM

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!!!

syg00 09-26-2007 05:32 AM

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.

prernasin 09-26-2007 05:45 AM

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"

prernasin 09-26-2007 06:16 AM

Quote:

Originally Posted by syg00 (Post 2904083)
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"

theYinYeti 09-26-2007 07:51 AM

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.

dgar 09-26-2007 08:47 AM

The bash builtin 'help' command is good too:
help test
help [

pixellany 09-26-2007 09:57 AM

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


All times are GMT -5. The time now is 09:28 AM.