Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
Registered User
 
Join Date: Jan 2012
Posts: 6
Thanks: 6
Thanked 0 Times in 0 Posts
File check script fails for multiple files

I want to check if any file with testing*.txt exists but my script fails if more than 1 file exists. It works fine for a single file


Code:
if [ -s /tmp/file_checker/testing*.txt ]
then
echo "TEST21"
fi

--------------

Code:
bash: [: too many arguments

How do I fix this?

Thanks

Moderator's Comments:
Please use code tags next time for your code and data.
Sponsored Links
    #2  
Old 01-30-2013
radoulov's Avatar
--
 
Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,468
Thanks: 139
Thanked 538 Times in 506 Posts

Code:
for f in /tmp/file_checker/testing*.txt; do
  [ -s "$f" ] && echo TEST21 && break
done

Note that -s returns true if the file exists and its size is greater than zero.
The Following User Says Thank You to radoulov For This Useful Post:
sumang24 (01-30-2013)
Sponsored Links
    #3  
Old 01-30-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,321
Thanks: 154
Thanked 742 Times in 714 Posts
Another approach:

Code:
if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then
echo "TEST21"
fi

The Following User Says Thank You to Yoda For This Useful Post:
sumang24 (01-30-2013)
    #4  
Old 01-30-2013
Registered User
 
Join Date: Jan 2012
Posts: 6
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by bipinajith View Post
Another approach:

Code:
if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then
echo "TEST21"
fi

Bipinajith: Thanks for your response. Is there a way to supress this message if the file foes not exist?


Code:
ls: /tmp/file_checker/testing*.txt: No such file or directory

Basically I want to display just the message that the file does not exist:


Code:
if ls /tmp/file_checker/testing*.txt > /dev/null; then
echo "TEST21"
else echo "file does not exist"
fi

Sponsored Links
    #5  
Old 01-30-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,321
Thanks: 154
Thanked 742 Times in 714 Posts
Redirect stderr to /dev/null like I suggested above:

Code:
if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then

The Following User Says Thank You to Yoda For This Useful Post:
sumang24 (01-30-2013)
Sponsored Links
    #6  
Old 01-30-2013
Registered User
 
Join Date: Jan 2012
Posts: 6
Thanks: 6
Thanked 0 Times in 0 Posts
Works like a charm.

Thanks

---------- Post updated at 12:27 PM ---------- Previous update was at 12:06 PM ----------

Quote:
Originally Posted by bipinajith View Post
Redirect stderr to /dev/null like I suggested above:

Code:
if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then

Sorry to bother you again. I need to check 2 conditions to be true and it seems ls does not support -a "AND" operation verification

something like this:


Code:
VAR1="FOO"
if [ $ORACLE_SID = $VAR1 -a ls /tmp/file_checker/testing*.txt ] > /dev/null 2> /dev/null; then
echo "TEST21"
else echo "file does not exist"
fi

Thanks
Sponsored Links
    #7  
Old 01-30-2013
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,407
Thanks: 492
Thanked 2,538 Times in 2,421 Posts
It was outside [ ] before, but you put it in [ ] , where it expects an expression not a command.


Code:
if [ "$ORACLE_SID" = "$VAR1" ] && ls /tmp/file_checker/testing*.txt >/dev/null 2>/dev/null
then
...
else
...
fi

The Following User Says Thank You to Corona688 For This Useful Post:
sumang24 (01-30-2013)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need a shell script to extract the files from source file and check whether those files existonserve muraliinfy04 Shell Programming and Scripting 13 02-22-2012 07:14 AM
.sh script / Check for file in two directories then cat the files Loonatic Shell Programming and Scripting 2 07-27-2010 12:16 PM
1 script or multiple scripts?? - check files, run jobs jnanasakti Shell Programming and Scripting 6 11-05-2008 12:10 PM
check the file in multiple Directory in a script mail2sant Shell Programming and Scripting 3 09-17-2008 12:44 PM
Script to Delete temp files and check file system Bwood1377 Shell Programming and Scripting 3 08-14-2008 08:34 AM



All times are GMT -4. The time now is 02:39 AM.