Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

File check script fails for multiple files

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-30-2013
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 7 December 2015, 2:52 AM EST
Posts: 18
Thanks: 12
Thanked 1 Time in 1 Post
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:
File check script fails for multiple files Please use code tags next time for your code and data.
Sponsored Links
    #2  
Old Unix and Linux 01-30-2013
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 628 Times in 585 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 Unix and Linux 01-30-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 August 2016, 4:04 PM EDT
Location: Galactic Empire
Posts: 3,522
Thanks: 248
Thanked 1,278 Times in 1,200 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 Unix and Linux 01-30-2013
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 7 December 2015, 2:52 AM EST
Posts: 18
Thanks: 12
Thanked 1 Time in 1 Post
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 Unix and Linux 01-30-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 August 2016, 4:04 PM EDT
Location: Galactic Empire
Posts: 3,522
Thanks: 248
Thanked 1,278 Times in 1,200 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 Unix and Linux 01-30-2013
sumang24 sumang24 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 7 December 2015, 2:52 AM EST
Posts: 18
Thanks: 12
Thanked 1 Time in 1 Post
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 Unix and Linux 01-30-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 August 2016, 5:27 PM EDT
Location: Saskatchewan
Posts: 21,430
Thanks: 967
Thanked 3,899 Times in 3,628 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:45 PM.