1

I am trying to automate out a lot of our pre fs/db tasks and one thing that bugs me is not knowing whether or not a command i issue REALLY happened. I'd like a way to be able to watch for a return code of some sort from executing that command. Where if it fails to rm because of a permission denied or any error. to issue an exit..

If i have a shell script as such:

rm /oracle/$SAPSID/mirrlogA/cntrl/cntrl$SAPSID.ctl;

psuedo code could be something similar to..

rm /oracle/$SAPSID/mirrlogA/cntrl/cntrl$SAPSID.ctl;
if [returncode == 'error']
  exit;
fi

how could i for example, execute that rm command and exit if its NOT rm'd. I will be adapting the answer to execute with multiple other types of commands such as sed -i -e, and cp and umount

edit:

Lets suppose i have a write protected file such as:

$ ls -lrt | grep protectedfile
-rwx------ 1 orasmq  sapsys         0 Nov 14 12:39 protectedfile

And running the below script generates the following error because obviously theres no permissions..

rm: remove write-protected regular empty file `/tmp/protectedfile'? y
rm: cannot remove `/tmp/protectedfile': Operation not permitted

Here is what i worked out from your guys' answers.. is this the right way to do something like this? Also how could i dump the error rm: cannot remove /tmp/protectedfile': Operation not permitted` to a logfile?

#! /bin/bash
function log(){
    //some logging code, simply writes to a file and then echo's out inpit
}
function quit(){
   read -p "Failed to remove protected file, permission denied?"
   log "Some log message, and somehow append the returned error message from rm"
   exit 1;
}

rm /tmp/protectedfile || quit;
4

3 回答 3

1

如果我正确理解你想要什么,只需使用这个:

rm blah/blah/blah || exit 1
于 2013-11-14T17:54:48.990 回答
0

一种可能性:一个“包装器”,以便您可以检索原始命令stderr |和stdout?],也许还可以在放弃之前重试几次?等等。

这是一个同时重定向标准输出和标准错误的版本

当然,您根本无法重定向标准输出(通常,我猜您不应该让“try_to”函数在脚本的其余部分更有用!)

export timescalled=0  #external to the function itself

try_to () {
    let "timescalled += 1"  #let "..." allows white spaces and simple arithmetics
    try_to_out="/tmp/try_to_${$}.${timescalled}" 
        #tries to avoid collisions within same script and/or if multiple script run in parrallel
    zecmd="$1" ; shift ;
    "$1" "$@" 2>"${try_to_out}.ERR" >"${try_to_out}.OUT"
    try_to_ret=$?
    #or: "$1" "$@" >"${try_to_out}.ERR" 2>&1  to have both in the same one
    if [ "$try_to_ret" -ne "0" ]
    then  log "error $try_to_ret while trying to : '${zecmd} $@' ..." "${try_to_out}.ERR" 
            #provides custom error message + the name of the stderr from the command
          rm -f "${try_to_out}.ERR" "${try_to_out}.OUT" #before we exit, better delete this
          exit 1 #or exit $try_to_ret ?
    fi
    rm -f "${try_to_out}.ERR" "${try_to_out}.OUT"
}

它很丑陋,但可以帮助^^

请注意,有很多事情可能会出错:“timecall”可能变得太高,tmp 文件无法写入,zecmd 可能包含特殊字符,等等...

于 2013-11-14T20:17:38.473 回答
0

通常有些人会使用这样的命令:

doSomething.sh
if [ $? -ne 0 ]
then
    echo "oops, i did it again"
    exit 1;
fi

顺便说一句,搜索“bash 退出状态”会给你带来很多好的结果

于 2013-11-14T20:17:05.040 回答