LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 10: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, 10:58 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587
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, 11:02 AM   #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, 11:13 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587
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, 11:16 AM   #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 11:18 AM.
 
Old 06-19-2014, 11:19 AM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,132

Rep: Reputation: 233Reputation: 233Reputation: 233
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, 11:20 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

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

Code:
else
echo "NOT OK"

Last edited by szboardstretcher; 06-19-2014 at 11:22 AM.
 
1 members found this post helpful.
Old 06-19-2014, 11:41 AM   #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, 11:59 AM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,265
Blog Entries: 11

Rep: Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292
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, 12: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, 12: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, 01:02 PM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587
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, 01:43 PM   #13
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,265
Blog Entries: 11

Rep: Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292Reputation: 2292
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 11:55 AM. Reason: s/tmp/deployment
 
1 members found this post helpful.
Old 06-19-2014, 07: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, 12: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 02:40 AM
Check File Exists shady4u Linux - General 2 02-21-2010 03:05 AM
[SOLVED] check whether a file exists or not ?? vikas027 Programming 5 06-05-2008 04:45 AM
bash check folder exists zerocool22 Programming 22 06-01-2008 07:53 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM


All times are GMT -5. The time now is 03:06 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