|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
-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
fiif 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
|
|||
|
|||
|
Thanks. That's close, but the filesize check needs to check if the size is 0 or >6
|
|
#4
|
|||
|
|||
|
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
fiIt 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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
fiIf 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
|
|||
|
|||
|
I get this...
typeset: not found and syntax error at line 38 : `fi' unexpected and line 38 is the last line |
| Sponsored Links | ||
|
![]() |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| 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 |
|
|