build script return throws an error when build fails

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    build script return throws an error when build fails

    This is my build script, I calls this script from main build script, in case of SUCCESS, everything is ok, but in failure, I do "return 1", so that main build can cheque "$?" and based on that it prints success or not, but here "return 1", throwa an error "./build: line 17: return: can only `return' from a function or sourced script", any idea?. I did issue command "cat build" below. any iddue with "return 1"?.

    $ cat build

    make clean
    make
    echo "gcc -shared -Wl,-soname,libRecei ver.so.1 -o libReceiver.so. 1.0 -L/opt/lib PC_Receiver.o"
    gcc -shared -Wl,-soname,libRecei ver.so.1 -o libReceiver.so. 1.0 PC_Receiver.o
    status=$?
    if [ $status = 0 ] ; then
    echo "mv libReceiver.so. 1.0 /opt/lib"
    mv libReceiver.so. 1.0 /opt/lib
    echo "ln -sf /opt/lib/libReceiver.so. 1.0 /opt/lib/libReceiver.so"
    ln -sf /opt/lib/libReceiver.so. 1.0 /opt/lib/libReceiver.so
    echo "ln -sf /opt/lib/libReceiver.so. 1.0 /opt/lib/libReceiver.so. 1"
    ln -sf /opt/lib/libReceiver.so. 1.0 /opt/lib/libReceiver.so. 1
    echo SUCCESS creating shared lib
    else
    echo ERROR creating shared lib
    return 1
    fi
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Replace the return in line 17 with an exit command (i.e. exit 1). Typically, return is only used inside a subroutine. Outside of subroutines, use exit.

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      Thanks. But here I want to return 1 for ERROR case, since I am calling this build script from main build script, where I will check return value "$?", incase if it is not zero, then I want to display error and comeout from main routine. anyother way "$?" should show 1 ?.

      $ echo $?
      0

      $ ./build
      echo ERROR creating shared lib

      $ echo $?
      0

      Comment

      • tvnaidu
        Contributor
        • Oct 2009
        • 365

        #4
        Thanks Rick. It works, I was confused earlier, sorry.

        Comment

        Working...