LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Error stop when scripting (https://www.linuxquestions.org/questions/linux-software-2/error-stop-when-scripting-65217/)

eXor 06-12-2003 01:23 PM

Error stop when scripting
 
I have written a script that backups my files over the network.

My problem is that i wan't an error log to be written if somthing goes wrong in my script.

Eg.

First of all i run this line
mount -t smbfs -r -o password= //viljamusikxp/bilder /mnt/bilder

But if by some reason this computer is of I don't wan't the rest of the script to be run. Instead i wan't an error log to be written.

How can I do this?

david_ross 06-12-2003 01:32 PM

I would pipe the output of the command into a grep for the error you get if it fails then if the grep returns with a match then you stop proccessing the script.

eXor 06-12-2003 01:35 PM

can you give an exampel? Not so god at scripting!

Thx for the help anyway..

unSpawn 06-12-2003 01:40 PM

I would pipe the output of the command into a grep for the error you get if it fails then if the grep returns with a match then you stop proccessing the script.

If any app handles it's exit status "$?" well, then inserting a
Code:

case "$?" in 0) ;; *) logger "got exit status ($?)"; exit 1;; esac
should do, shouldn't it? Zero does nothing, so the script should run along, 1, 127 or whatever $? throws at you will exit it.

eXor 06-12-2003 01:44 PM

thx..but one more qustion.

the line
case "$?" in 0) ;; *) logger "got exit status ($?)"; exit 1;; esac
can you explain a bit more. Where do I tell this line that if moutn fails it should logg this. And where do it logg to.

eXor 06-12-2003 01:49 PM

it worked. But where dose it log it. I wan't it to be logged in

/var/log/test.log

How do I do this?

david_ross 06-12-2003 01:49 PM

Quote:

Originally posted by unSpawn
should do, shouldn't it?
Yes - sorry, I was thinking that he wanted to capture the output from his script and I since not everyone will output the exit codes properly I quite often check the output for strings.

eXor 06-12-2003 02:01 PM

case "$?" in
0)
;;
*)
echo "wrong on mounting windows disc" >> test.log
# logger "got exit status ($?)";
exit 1;;
esac

This worked...is it okej...what did the logger thing do?

david_ross 06-12-2003 02:04 PM

From "man logger"
Code:

NAME
    logger - a shell command interface to the syslog(3) system log module

SYNOPSIS
    logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]

DESCRIPTION
    Logger makes entries in the system log.  It provides a shell command
    interface to the syslog(3) system log module.


unSpawn 06-12-2003 02:06 PM

Yes - sorry(..) check the output for strings.
Np man, you noticed I started with "if"? :-]
About strings I agree, tho I usually try to avoid it if I can...

eXor 06-12-2003 02:06 PM

A..ok thx..now my program workes great.


All times are GMT -5. The time now is 07:34 PM.