LinuxQuestions.org
Visit Jeremy's Blog.
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 06-19-2014, 11:56 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
check if files exists


Hi

this is true:

#!/bin/bash

if [ -a /tmp/jboss7_pid.txt ]; then
echo "it exist!"


but what if I also need to check 2 files in /tmp? lets say jboss4_pid.txt. I tried this but didnt worked out

if [ -a /tmp/jboss7_pid.txt -a /tmp/jboss4_pid.tx ]; then
echo "it exist!"
 
Old 06-19-2014, 11:58 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
Use a for loop?

Code:
for i in /tmp/jboss*; do 
 test -a $i && echo "it exist!"
done
 
1 members found this post helpful.
Old 06-19-2014, 12:02 PM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Use a for loop?

Code:
for i in /tmp/jboss*; do 
 test -a $i && echo "it exist!"
done

what about if file2 is not almost the-same name from file1?

lets say:
file1 = jboss7_pid.txt
file2 = postgres_pid.txt
 
Old 06-19-2014, 12:13 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
Depends on what you mean. If all files are in /tmp and have the format blah_pid.txt then you can just change the wildcard usage to:

/tmp/*_pid.txt
 
1 members found this post helpful.
Old 06-19-2014, 12:16 PM   #5
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Depends on what you mean. If all files are in /tmp and have the format blah_pid.txt then you can just change the wildcard usage to:

/tmp/*_pid.txt
thanks szboardstretcher, also I wish to make sure FILE1 and FILE2 exists and if either one of them are not it will echo on screen "NOT OK"
FILE1 = cat
FILE2 = dog

Last edited by apss_evaluator; 06-19-2014 at 12:18 PM.
 
Old 06-19-2014, 12:19 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,187

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
-a file exists
This is identical in effect to -e. It has been "deprecated," and its use is discouraged.
Try
Code:
if
  [ -e /tmp/jboss7_pid.txt ] && [ -e /tmp/jboss4_pid.txt ]; then
  echo "they both exist."
fi
 
1 members found this post helpful.
Old 06-19-2014, 12:20 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
adding to the above.

Code:
else
echo "NOT OK"

Last edited by szboardstretcher; 06-19-2014 at 12:22 PM.
 
1 members found this post helpful.
Old 06-19-2014, 12:41 PM   #8
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
adding to the above.

Code:
else
echo "NOT OK"
Thanks JeremyBoden and szboardstretcher it worked!
 
Old 06-19-2014, 12:59 PM   #9
Habitual
LQ 5k Club
 
Registered: Jan 2011
Location: Nowhere near you, thank God.
Distribution: OSX Sierra
Posts: 8,592
Blog Entries: 15

Rep: Reputation: Disabled
Code:
-f file
              True if file exists and is a regular file.

Code:
if [ -f /tmp/a ] && [ -f /tmp/b ]; then echo "they both exist."; fi
 
2 members found this post helpful.
Old 06-19-2014, 01:18 PM   #10
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Code:
-f file
              True if file exists and is a regular file.

Code:
if [ -f /tmp/a ] && [ -f /tmp/b ]; then echo "they both exist."; fi
hey last one, I also want to add on the script checking if apps on my jboss deployment directory has no ".failed" extension files on that directory
 
Old 06-19-2014, 01:55 PM   #11
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by apss_evaluator View Post
hey last one, I also want to add on the script checking if apps on my jboss deployment directory has no ".failed" extension files on that directory
I'm good now this sample made it helpful = /tmp/*_pid.txt
 
Old 06-19-2014, 02:02 PM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
You will have to run that script every X number of seconds to ensure no .failed files exist in the directory. An easier way that I have implemented here is to use inotify or iwatch to watch the directory and trigger an event on any creation of said files.

Docs are here on that: http://iwatch.sourceforge.net/documentation.html

There are other implentations of inotify, and if you feel comfortable in C, you can even roll your own.
 
1 members found this post helpful.
Old 06-19-2014, 02:43 PM   #13
Habitual
LQ 5k Club
 
Registered: Jan 2011
Location: Nowhere near you, thank God.
Distribution: OSX Sierra
Posts: 8,592
Blog Entries: 15

Rep: Reputation: Disabled
Quote:
Originally Posted by apss_evaluator View Post
deployment directory has no ".failed" extension files on that directory
Example:
Code:
if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
the test:
Code:
ls -al /deployment/*.failed
ls: cannot access /deployment/*.failed: No such file or directory

if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
quit

touch /deployment/jj.failed
if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
do stuff
See https://www.linuxquestions.org/quest...llected-35954/ for other bash related links.
</shameless plug>

Last edited by Habitual; 06-20-2014 at 12:55 PM. Reason: s/tmp/deployment
 
1 members found this post helpful.
Old 06-19-2014, 08:14 PM   #14
nisqr
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
thanks, i am just looking for this.
 
Old 06-22-2014, 01:53 PM   #15
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Example:
Code:
if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
the test:
Code:
ls -al /deployment/*.failed
ls: cannot access /deployment/*.failed: No such file or directory

if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
quit

touch /deployment/jj.failed
if [ -f /deployment/*.failed ]; then echo do stuff; else echo quit;  fi
do stuff
See https://www.linuxquestions.org/quest...llected-35954/ for other bash related links.
</shameless plug>
thanks Habitual!
 
  


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
Check if file exists in C MTK358 Programming 17 06-20-2010 03:40 AM
Check File Exists shady4u Linux - General 2 02-21-2010 04:05 AM
[SOLVED] check whether a file exists or not ?? vikas027 Programming 5 06-05-2008 05:45 AM
bash check folder exists zerocool22 Programming 22 06-01-2008 08:53 AM
c++ check if file exists Genjix Programming 3 03-15-2004 01:08 AM

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

All times are GMT -5. The time now is 10:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration