2

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. :(

2
  • Can you post syntaxically correct code? Your valid() function is missing }. What do you want to achieve? Replacing lines matching :@ with connec...database? Or replace occurences of :@ in string $search by the text?
    – Lekensteyn
    Mar 7, 2011 at 16:37
  • thanks 4 reply , Replacing :@ with connec..database , but this is not the problem every thing is going fine the things that am asking is out put of execution these command..$search variable is the path of file that to be changed ....
    – moata_u
    Mar 7, 2011 at 16:46

1 Answer 1

4

For the former:

# run sed, capture any output
var1="$(sed -i "/connection.username/c connection.username=$name" $search)"

For the latter:

# run sed, discard all output but keep exit status
sed -i "/connection.username/c connection.username=$name" $search >/dev/null 2>&1

That said, valid() is... strange, to say the least. I would write it as

# valid label [status]
# check status ($? if not provided) and log success/failure
function valid {
  if [[ ${2-$?} == 0 ]]; then
    echo "$1 : status : OK"
  else
    echo "$1 : status : ERROR"
  fi
}

Actually, I would do it a bit differently from the start:

# log label command [arguments...]
# run command, output success/failure to stderr.  If label is empty, use the
# command name:  'log "" echo hi' uses 'echo' as the label.
# log entries look like
#    label1 : status : OK
#    label2 : status : ERROR
#    Error output from foo:
#      Baz is broken; fix and try again.
log() {
  # save off label
  local label="${1:-$2}"
  shift # this removes $1 and shifts $2... to $1... so "$@" works later
  # run command, capture output
  # $(command) runs 'command' and substitutes its output
  # "$@" preserves quoting; $* would turn argument "foo bar" into two
  # arguments foo bar
  err="$("$@")"
  if [[ $? == 0 ]]; then
    status=OK
  else
    status=ERROR
  fi
  echo "$label : status : $status" >&2 # standard error
  if [[ $status == ERROR ]]; then
    # log output from command, indented for clarity
    echo "Error output from $2:"
    echo "$err" | sed 's/^/  /' >&2
  fi
}

save() {
  # this sed command is pedantically correct; if GNU sed lets you
  # do single-line 'c' commands as in the original, then go for it.
  # The backslash-return after the label argument is just for clarity;
  # 'log "foo" bar' works just as well.
  log "adding database ip" \
    sed -i "/:@/c\\
connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase\\
." "$search"
  # using the original (GNU?) sed syntax
  log "adding database SID" \
    sed -i "/connection.username/c connection.username=$name" "$search"
}

save

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.

16
  • that's perfect ,, thanks let me check that :),,thanks
    – moata_u
    Mar 7, 2011 at 16:49
  • geekosaur , can you edit your post and add some comment ! specially in log() function .
    – moata_u
    Mar 7, 2011 at 17:00
  • No problem; comments and example log output now added.
    – geekosaur
    Mar 7, 2011 at 17:21
  • The quoting a="$(echo foo bar baz)" is useless - a needs still quoting. f () { echo $1; } might be a function. f $a will just display 'foo', while f "$a" will display 'foo bar baz'. Which is, what you get for a=$(echo foo bar baz). Mar 7, 2011 at 20:08
  • 1
    "$(command)" runs command and substitutes its output, preserving spaces, tabs, and newlines instead of the default of collapsing all of them to a single space. "$@" substitutes the command line arguments (minus the label, which we shifted away) with all quoting preserved. So "$("$@")" means "run the command specified in the script's arguments and substitute its output" — which we then assign to err for later use. (Where did you think I had called sed? I use it later to indent the output from the command, but that is unrelated.)
    – geekosaur
    Mar 9, 2011 at 9:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .