Using shell script good practices

Changes include:

- use of double quotes when referencing variables
- exit status other than 0 in case of error
- unification of sed commands (saves calls to fork())
- validation tests in parameters