Quote:
Originally Posted by
mohtashims
i just wish to display the failing line
I want to eat pork and curse Allah while enjoying my 72 virgins - some things just aren't going to be together.
You might succeed with a trap function, but any solution i can think of is not going to be "plugged into" an existing script with less than heavy modifications.
What i do in these cases is: write to log files, so that - when a script fails, i can trace how much of its task was completed until it failed. It may still fail but this usually makes it way easier to clean up afterwards.
Second, i identify what i (respectively the script) need(s) to know beforehand and i test/execute all these tasks first. This way, when i finally do something that might fail, i have made as sure as is possible to make it succeed - or, if not all preliminary tasks are successful, don't even attempt to do it.
For example: i need to write a file. Before even writing it, i test if the directory i want to write to is there and can be written. Then i test if enough space is available to write it. Then i make sure there is no file with the name i want to write to and eventually save an existing one to a different name (or not and raise an error). Etc., etc., only then i finally write the file. All these preliminary tasks, if unusuccessful, will lead to a program stop and a respective error message ("cannot write to dir", "not enough space",...).
In general:
plan your programs so that they do not fail instead of analysing errors. Doing it the way i described it is - for my purposes - usually sufficient and easy to analyse in case something indeed goes wrong (because regardless of how much precaution you take there is always the possibility of an error, that is just how life is).
It usually helps in planning to try to create a list of things that could go wrong - and then write code to react to exactly this.
Another example: task is to create a user account. Here is, off the top, a (surely incomplete) list of thing that could be/go wrong and possible reactions of the program. Your reactions might look different, depending on the details of your environment:
- necessary command not there (-> reinstall part of the OS)
- necessary information not provided (-> raise error, stop)
- user already exists (-> raise warning, stop)
- user id already taken (-> raise error, stop)
- script called with not enough rights (-> raise error, stop)
- home directory cannot be created (for various reasons) (-> raise error with exact reason, stop)
- user exists but has different attributes (-> raise warning, change attributes, continue)
Wheever i identify such a task i do not write a script, but i write a function with a well-documented interface which will implement exactly this one task. Further scripts will then only call this function with the appropriate set of parameters.
I hope this helps.
bakunin