check for "cannot open file" | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

check for "cannot open file"

UNIX for Dummies Questions & Answers


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-26-2007
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 12 June 2007, 11:14 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
check for "cannot open file"

I have a small script that checks to see if a file exists and that it has data in it. I also need to check if the file can be opened. I had an issue today where the file it checks could not be opened and my script did not catch it. How do I check to see if it cannot be opened?


Code:
dirname=/opt/manu/srv
f1=manumgrmdip2_7500.log
f2=manumgrmdip2_7500.pid
export HOST=`hostname`
f1size=`wc -c $dirname/manumgrmdip2_7500.log | cut -f1 -d " "`
f2size=`wc -c $dirname/manumgrmdip2_7500.pid | cut -f1 -d " "`

if [[ ! -f $dirname/$f1 || ! -f $dirname/$f2 ]] ; then
      echo "Either <$dirname/$f1> or <$dirname/$f1> does not exist" | mailx -s "Missing files on Manugistics $HOST Server (MDIP2)" user@xyz.com
else if (( $(wc -c < $dirname/$f1) != 0 )) || (( $(wc -c < $dirname/$f2) > 6 )) ; then
      echo "The size of $dirname/$f1 size = $f1size, and $dirname/$f2 = $f2size'" | mailx -s "Error with file sizes on Manugistics $HOST Server (MDIP2)" user@xyz.com < manumgrmdip2_7500.log
     fi;
fi;

Sponsored Links
    #2  
Old 02-26-2007
sb008 sb008 is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 12 January 2013, 3:37 AM EST
Posts: 398
Thanks: 1
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by ssmith001
I have a small script that checks to see if a file exists and that it has data in it. I also need to check if the file can be opened. I had an issue today where the file it checks could not be opened and my script did not catch it. How do I check to see if it cannot be opened?


Code:
dirname=/opt/manu/srv
f1=manumgrmdip2_7500.log
f2=manumgrmdip2_7500.pid
export HOST=`hostname`
f1size=`wc -c $dirname/manumgrmdip2_7500.log | cut -f1 -d " "`
f2size=`wc -c $dirname/manumgrmdip2_7500.pid | cut -f1 -d " "`

if [[ ! -f $dirname/$f1 || ! -f $dirname/$f2 ]] ; then
      echo "Either <$dirname/$f1> or <$dirname/$f1> does not exist" | mailx -s "Missing files on Manugistics $HOST Server (MDIP2)" user@xyz.com
else if (( $(wc -c < $dirname/$f1) != 0 )) || (( $(wc -c < $dirname/$f2) > 6 )) ; then
      echo "The size of $dirname/$f1 size = $f1size, and $dirname/$f2 = $f2size'" | mailx -s "Error with file sizes on Manugistics $HOST Server (MDIP2)" user@xyz.com < manumgrmdip2_7500.log
     fi;
fi;


-r <filename> to check for existance and readability
-s <filename> to check for existance and size greater than 0


Code:
#!/usr/bin/ksh
dirname=/opt/manu/srv
f1=manumgrmdip2_7500.log
f2=manumgrmdip2_7500.pid
export HOST=`hostname`

if [ ! -r ${dirname}/${f1} -o ! -r ${dirname}/${f2} ]
then
  echo "Either <$dirname/$f1> or <$dirname/$f1> does not exist or is not readable" | mailx -s "Missing files on Manugistics $HOST Server (MDIP2)" user@xyz.com
elif [ ! -s ${dirname}/${f1} -o ! -s ${dirname}/${f2} ]
then
  echo "Either <$dirname/$f1> or <$dirname/$f2>has a filesize of 0'" | mailx -s "Error with file sizes on Manugistics $HOST Server (MDIP2)" user@xyz.com < manumgrmdip2_7500.log
fi

if you want to know the exact size use the typeset command so you don't need the cut command:


Code:
typset -i f1size f2size
f1size=`wc -c ${dirname}/${f1}`
f2size=`wc -c ${dirname}/${f2}`

Sponsored Links
    #3  
Old 02-26-2007
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 12 June 2007, 11:14 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. That's close, but the filesize check needs to check if the size is 0 or >6
    #4  
Old 02-26-2007
sb008 sb008 is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 12 January 2013, 3:37 AM EST
Posts: 398
Thanks: 1
Thanked 3 Times in 3 Posts
Sorry, I overlooked the "6" size.


Code:
#!/usr/bin/ksh
dirname=/opt/manu/srv
f1=manumgrmdip2_7500.log
f2=manumgrmdip2_7500.pid
export HOST=`hostname`

typset -i f1size f2size
f1size=`wc -c ${dirname}/${f1}`
f2size=`wc -c ${dirname}/${f2}`

if [ ! -r ${dirname}/${f1} -o ! -r ${dirname}/${f2} ]
then
  echo "Either <$dirname/$f1> or <$dirname/$f1> does not exist or is not readable" | mailx -s "Missing files on Manugistics $HOST Server (MDIP2)" user@xyz.com
elif [ ${f1size} -eq 0 -o ${f2size} -eq 0 -o ${f1size} -gt 6 -o ${f2size} -gt 6 ]
then
  echo "Either <$dirname/$f1> or <$dirname/$f2> has an incorrect filesize'" | mailx -s "Error with file sizes on Manugistics $HOST Server (MDIP2)" user@xyz.com < manumgrmdip2_7500.log
fi

It is not exactly clear to me which size is right or wrong, but that's just a matter of changing "-eq" into "-ne" or changing "-gt" into "-ge", "-le", -lt" or whatever.
Sponsored Links
    #5  
Old 02-26-2007
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 12 June 2007, 11:14 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I keep getting an error of "unknown test operator"

These are the conditions that need to send the email:

1) file1 or file2 do not exist
2) file1 and file2 exist but are not readable
3) if file1 size != 0
4) if file2 size > 6

Last edited by ssmith001; 02-26-2007 at 03:16 PM..
Sponsored Links
    #6  
Old 02-26-2007
sb008 sb008 is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 12 January 2013, 3:37 AM EST
Posts: 398
Thanks: 1
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by ssmith001
I keep getting an error of "unknown test operator"

These are the conditions that need to send the email:

1) file1 or file2 do not exist
2) file1 and file2 exist but are not readable
3) if file1 size != 0
4) if file2 size > 6
Ok, I made a mistake, I was calculating the size of files before knowing if they exist and are readable.


Code:
#!/usr/bin/ksh

dirname=/opt/manu/srv
f1=manumgrmdip2_7500.log
f2=manumgrmdip2_7500.pid
export HOST=`hostname`

typset -i f1size f2size

if [ ! -r ${dirname}/${f1} -o ! -r ${dirname}/${f2} ]
then
  echo "Either <$dirname/$f1> or <$dirname/$f1> does not exist or is not readable" | mailx -s "Missing files on Manugistics $HOST Server (MDIP2)" user@xyz.com
else 
  f1size=`wc -c ${dirname}/${f1}`
  f2size=`wc -c ${dirname}/${f2}`

  if [ ${f1size} -eq 0 -o ${f2size} -gt 6 ]
  then
    echo "The size of $dirname/$f1 size = $f1size, and $dirname/$f2 = $f2size'" | mailx -s "Error with file sizes on Manugistics $HOST Server (MDIP2)" user@xyz.com < ${f1}
  fi
fi

If you still get an error message put "set -x" at the beginning of the script so you can see exactly which statement causes the error.


Code:
#!/usr/bin/ksh

set -x

dirname=/opt/manu/srv
......

Sponsored Links
    #7  
Old 02-26-2007
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 12 June 2007, 11:14 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I get this...

typeset: not found

and

syntax error at line 38 : `fi' unexpected

and line 38 is the last line
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
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
How to check "faulty" or "stalled" print queues - SAP systems? newbie_01 Solaris 0 07-15-2010 09:40 AM
"sed" to check file size & echo " " to destination file jockey007 Shell Programming and Scripting 7 04-28-2009 02:08 AM
VMDB Failure" followed by "Unable to open snapshot file" s_linux SuSE 0 02-10-2009 11:01 AM
check input = "empty" and "numeric" geoffry Shell Programming and Scripting 6 12-13-2007 04:12 AM



All times are GMT -4. The time now is 09:59 AM.