Detecting failure of a Bash “export” value

后端 未结 4 1516
故里飘歌
故里飘歌 2021-01-19 05:29

In Bash I\'m executing a command and putting the result in a variable like this:

export var=`svn ls`

But if SVN fails for some reason--say it returns a n

相关标签:
4条回答
  • 2021-01-19 05:43

    I had similar problem, it can be done like this:

    rm -f error_marker_file
    export var=`svn ls || touch error_marker_file`
    
    [ -f error_marker_file ] && echo "error in executing svn ls"
    
    0 讨论(0)
  • 2021-01-19 05:47
    export FOO=$(your-command) || echo "your-command failed"
    
    0 讨论(0)
  • 2021-01-19 05:49
    var=`svn ls`
    if [[ $? == 0 ]]
    then
            export var
    else
            unset var
    fi
    

    $? is the exit code of the last command executed, which is svn ls here.

    jmohr's solution is short and sweet. Adapted mildly,

    var=`svn ls` && export var || unset var
    

    would be approximately equivalent to the above (export of a valid identifier will never fail, unless you've done something horrible and run out of environment space). Take whatever you want -- I use unset just to avoid $var possibly having a value even though it's not exported.

    0 讨论(0)
  • 2021-01-19 06:04
    var=`svn ls` && export var
    
    0 讨论(0)
提交回复
热议问题