exit not terminating the script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












exit doesn't terminate the script when error is called..



output



Error: Could not resolve localhost
after exit


script



#!/bin/sh

resolve_ip ()
if [ -z "$1" ]; then
host="localhost"
ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
else
host="$1"
ip=$(dig +short $1)
fi

if [ -z "$ip" ]; then
error "Could not resolve $host"
fi

echo "$ip"


error ()
(>&2 echo "Error: $1")
exit 1


master_host='google.com'

if [ "$(resolve_ip)" = "$(resolve_ip $master_host)" ]; then
error "some error"
fi

echo "after exit"
exit









share|improve this question

























    up vote
    1
    down vote

    favorite












    exit doesn't terminate the script when error is called..



    output



    Error: Could not resolve localhost
    after exit


    script



    #!/bin/sh

    resolve_ip ()
    if [ -z "$1" ]; then
    host="localhost"
    ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
    else
    host="$1"
    ip=$(dig +short $1)
    fi

    if [ -z "$ip" ]; then
    error "Could not resolve $host"
    fi

    echo "$ip"


    error ()
    (>&2 echo "Error: $1")
    exit 1


    master_host='google.com'

    if [ "$(resolve_ip)" = "$(resolve_ip $master_host)" ]; then
    error "some error"
    fi

    echo "after exit"
    exit









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      exit doesn't terminate the script when error is called..



      output



      Error: Could not resolve localhost
      after exit


      script



      #!/bin/sh

      resolve_ip ()
      if [ -z "$1" ]; then
      host="localhost"
      ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
      else
      host="$1"
      ip=$(dig +short $1)
      fi

      if [ -z "$ip" ]; then
      error "Could not resolve $host"
      fi

      echo "$ip"


      error ()
      (>&2 echo "Error: $1")
      exit 1


      master_host='google.com'

      if [ "$(resolve_ip)" = "$(resolve_ip $master_host)" ]; then
      error "some error"
      fi

      echo "after exit"
      exit









      share|improve this question













      exit doesn't terminate the script when error is called..



      output



      Error: Could not resolve localhost
      after exit


      script



      #!/bin/sh

      resolve_ip ()
      if [ -z "$1" ]; then
      host="localhost"
      ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
      else
      host="$1"
      ip=$(dig +short $1)
      fi

      if [ -z "$ip" ]; then
      error "Could not resolve $host"
      fi

      echo "$ip"


      error ()
      (>&2 echo "Error: $1")
      exit 1


      master_host='google.com'

      if [ "$(resolve_ip)" = "$(resolve_ip $master_host)" ]; then
      error "some error"
      fi

      echo "after exit"
      exit






      shell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 38 mins ago









      clarkk

      48241122




      48241122




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          exit exits the current shell process¹.



          In $(resolve_ip), resolve_ip is running in a subshell process.



          You can do:



           my_ip=$(resolve_ip) || exit
          master_ip=$(resolve_ip "$hostname") || exit
          if [ "$my_ip" = "$master_ip" ]; ...


          For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.



          Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.



          Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.




          ¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a $ ...; form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477719%2fexit-not-terminating-the-script%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            exit exits the current shell process¹.



            In $(resolve_ip), resolve_ip is running in a subshell process.



            You can do:



             my_ip=$(resolve_ip) || exit
            master_ip=$(resolve_ip "$hostname") || exit
            if [ "$my_ip" = "$master_ip" ]; ...


            For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.



            Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.



            Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.




            ¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a $ ...; form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.






            share|improve this answer


























              up vote
              4
              down vote













              exit exits the current shell process¹.



              In $(resolve_ip), resolve_ip is running in a subshell process.



              You can do:



               my_ip=$(resolve_ip) || exit
              master_ip=$(resolve_ip "$hostname") || exit
              if [ "$my_ip" = "$master_ip" ]; ...


              For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.



              Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.



              Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.




              ¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a $ ...; form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.






              share|improve this answer
























                up vote
                4
                down vote










                up vote
                4
                down vote









                exit exits the current shell process¹.



                In $(resolve_ip), resolve_ip is running in a subshell process.



                You can do:



                 my_ip=$(resolve_ip) || exit
                master_ip=$(resolve_ip "$hostname") || exit
                if [ "$my_ip" = "$master_ip" ]; ...


                For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.



                Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.



                Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.




                ¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a $ ...; form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.






                share|improve this answer














                exit exits the current shell process¹.



                In $(resolve_ip), resolve_ip is running in a subshell process.



                You can do:



                 my_ip=$(resolve_ip) || exit
                master_ip=$(resolve_ip "$hostname") || exit
                if [ "$my_ip" = "$master_ip" ]; ...


                For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.



                Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.



                Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.




                ¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a $ ...; form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 29 mins ago

























                answered 34 mins ago









                Stéphane Chazelas

                289k54536875




                289k54536875



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477719%2fexit-not-terminating-the-script%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery