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

moata_u
  • 5,613
  • 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 07 '11 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 07 '11 at 16:46

1 Answers1

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.

geekosaur
  • 11,787
  • that's perfect ,, thanks let me check that :),,thanks – moata_u Mar 07 '11 at 16:49
  • geekosaur , can you edit your post and add some comment ! specially in log() function . – moata_u Mar 07 '11 at 17:00
  • No problem; comments and example log output now added. – geekosaur Mar 07 '11 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). – user unknown Mar 07 '11 at 20:08
  • well , still facing same problem of out put (log) still hiding error message that i wrote, it just write sed error , [sed: no input file] i want it be like this [addning database SID : status : ERROR"] OR [addning database SID : status : ERROR"] ,,,,,,geekosaur i will try your genius script tomorrow ... – moata_u Mar 07 '11 at 20:52
  • I recommend against using the Advanced Bash Scripting Guide for learning Bash. It doesn't teach good practice, and in some parts, it's just plain wrong. Read http://mywiki.wooledge.org/BashGuide instead. – geirha Mar 07 '11 at 20:54
  • Thanks geirha ,,,mywiki.wooledge.org/BashGuide is very nice ..easy . simple , practice – moata_u Mar 08 '11 at 05:52
  • geekosaur you code work perfectly..good job – moata_u Mar 08 '11 at 13:08
  • what is different between local label=$1 and local label="${1:-$2}" , both of them do same job ! – moata_u Mar 08 '11 at 13:51
  • The second one says "if $1 is empty, substitute $2 instead"; so if you say log "" sed ..., the label will be sed. It's just defining a convenient shorthand for log sed sed ... or whatever. – geekosaur Mar 08 '11 at 18:03
  • @geekosaur,why you are using "$("$@")" , i mean after command shift you already called sed... but when i delete "$("$@")" i always get OK even its error or ERROR even its OK...Please could you explain this point... – moata_u Mar 09 '11 at 09:23
  • 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 09 '11 at 09:31
  • @geekosaur , now every thing is clear 100% ,,but you know if i used command ls instead of sed... then "$("$@")" would not work i already tried use "$("$*")" then it works ,, so there is an situation that i can use "$("$*")" instead of "$("$@")" ,, Thanks 4 ur time – moata_u Mar 09 '11 at 09:42
  • How exactly did it not work? – geekosaur Mar 09 '11 at 09:46
  • its says ./cc: line 7: 0: command not found , if you type "$("$1 $2 $3 $4")" instead ,out put comes but only 4 argument of output which mean that its run but out put not come out by using this command "$("$@")" or should i use it in different way !! – moata_u Mar 09 '11 at 09:55
  • @geekosaur . my mistake every thing is ok now ....thanks – moata_u Mar 09 '11 at 10:17