File check script fails for multiple files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

File check script fails for multiple files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 2 April 2014, 8:12 AM EDT
Posts: 10
Thanks: 7
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
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 18 August 2014, 4:54 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 August 2014, 5:47 PM EDT
Location: Galactic Empire
Posts: 3,361
Thanks: 230
Thanked 1,195 Times in 1,124 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
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 2 April 2014, 8:12 AM EDT
Posts: 10
Thanks: 7
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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 August 2014, 5:47 PM EDT
Location: Galactic Empire
Posts: 3,361
Thanks: 230
Thanked 1,195 Times in 1,124 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
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 2 April 2014, 8:12 AM EDT
Posts: 10
Thanks: 7
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
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 18 August 2014, 2:39 PM EDT
Location: Saskatchewan
Posts: 19,212
Thanks: 772
Thanked 3,220 Times in 3,018 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
Closed Thread

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:50 AM.