Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions 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 629 Times in 586 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: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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: 8 December 2016, 5:10 PM EST
Location: Saskatchewan
Posts: 21,672
Thanks: 1,010
Thanked 3,985 Times in 3,702 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 08:14 AM
.sh script / Check for file in two directories then cat the files Loonatic Shell Programming and Scripting 2 07-27-2010 01:16 PM
1 script or multiple scripts?? - check files, run jobs jnanasakti Shell Programming and Scripting 6 11-05-2008 01:10 PM
check the file in multiple Directory in a script mail2sant Shell Programming and Scripting 3 09-17-2008 01:44 PM
Script to Delete temp files and check file system Bwood1377 Shell Programming and Scripting 3 08-14-2008 09:34 AM



All times are GMT -4. The time now is 11:50 PM.