I'm trying to catch any error when run a command in order to write a log-file / report
I've tried this code:
function valid (){
if [ $? -eq 0 ]; then
echo "$var1" ": status : OK"
else echo "$var1" ": status : ERROR"
fi
}
function save(){
sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search
var1="adding database ip"
valid $var1
sed -i "/connection.username/c connection.username=$name" #$search
var1="addning database SID"
valid $var1
}
save
The output looks like this:
adding database ip : status : OK
sed: no input file
But I want it to look like this:
adding database ip : status : OK
sed: no input file : status : ERROR"
or this:
adding database ip : status : OK
addning database SID : status : ERROR"
I've been trying, but it's not working with me. :(
For the former:
For the latter:
That said,
valid()
is... strange, to say the least. I would write it asActually, I would do it a bit differently from the start:
I would also include a timestamp and program ID, etc. in a real program.
You should probably explore the Advanced Bash Scripting Guide to learn more about writing shell scripts. The UNIX Programming Environment's shell programming chapters don't cover
bash
extensions to the original Bourne shell, but are still useful for learning the "zen" of shell scripting.