When Comment Out 'exit', Code Perform Well

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











up vote
1
down vote

favorite












I am following an instruction and confuse with the usage of exit



 FILE=~/.bashrc
if [ -e "$FILE" ] ; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ] ; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ] ; then
echo "$FILE is writabe."
fi
if [ -x "$FILE" ]; then
echo '$FILE is executable/searchable.'
fi
else
echo '$FILE does not exist'
exit 1
fi
exit


Run and come by



 $ bash test_file.sh
/Users/me/.bashrc is a regular file.
/Users/me/.bashrc is readable.
/Users/me/.bashrc is writabe.


If comment out commands of exit, the output stay unchanged.



What's its function?
Could I leave out the exits when familiar with the language.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I am following an instruction and confuse with the usage of exit



     FILE=~/.bashrc
    if [ -e "$FILE" ] ; then
    if [ -f "$FILE" ]; then
    echo "$FILE is a regular file."
    fi
    if [ -d "$FILE" ]; then
    echo "$FILE is a directory."
    fi
    if [ -r "$FILE" ] ; then
    echo "$FILE is readable."
    fi
    if [ -w "$FILE" ] ; then
    echo "$FILE is writabe."
    fi
    if [ -x "$FILE" ]; then
    echo '$FILE is executable/searchable.'
    fi
    else
    echo '$FILE does not exist'
    exit 1
    fi
    exit


    Run and come by



     $ bash test_file.sh
    /Users/me/.bashrc is a regular file.
    /Users/me/.bashrc is readable.
    /Users/me/.bashrc is writabe.


    If comment out commands of exit, the output stay unchanged.



    What's its function?
    Could I leave out the exits when familiar with the language.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am following an instruction and confuse with the usage of exit



       FILE=~/.bashrc
      if [ -e "$FILE" ] ; then
      if [ -f "$FILE" ]; then
      echo "$FILE is a regular file."
      fi
      if [ -d "$FILE" ]; then
      echo "$FILE is a directory."
      fi
      if [ -r "$FILE" ] ; then
      echo "$FILE is readable."
      fi
      if [ -w "$FILE" ] ; then
      echo "$FILE is writabe."
      fi
      if [ -x "$FILE" ]; then
      echo '$FILE is executable/searchable.'
      fi
      else
      echo '$FILE does not exist'
      exit 1
      fi
      exit


      Run and come by



       $ bash test_file.sh
      /Users/me/.bashrc is a regular file.
      /Users/me/.bashrc is readable.
      /Users/me/.bashrc is writabe.


      If comment out commands of exit, the output stay unchanged.



      What's its function?
      Could I leave out the exits when familiar with the language.







      share|improve this question














      I am following an instruction and confuse with the usage of exit



       FILE=~/.bashrc
      if [ -e "$FILE" ] ; then
      if [ -f "$FILE" ]; then
      echo "$FILE is a regular file."
      fi
      if [ -d "$FILE" ]; then
      echo "$FILE is a directory."
      fi
      if [ -r "$FILE" ] ; then
      echo "$FILE is readable."
      fi
      if [ -w "$FILE" ] ; then
      echo "$FILE is writabe."
      fi
      if [ -x "$FILE" ]; then
      echo '$FILE is executable/searchable.'
      fi
      else
      echo '$FILE does not exist'
      exit 1
      fi
      exit


      Run and come by



       $ bash test_file.sh
      /Users/me/.bashrc is a regular file.
      /Users/me/.bashrc is readable.
      /Users/me/.bashrc is writabe.


      If comment out commands of exit, the output stay unchanged.



      What's its function?
      Could I leave out the exits when familiar with the language.









      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      34.7k1269113




      34.7k1269113










      asked Apr 3 at 2:01









      JawSaw

      29410




      29410




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The exit command not only exits a script, but also sets an exit code, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash and similar shells, one can view or interrogate the exit code by examining shell variable $?.



          Also, BTW, you have an indentation problem. The else clause should be out-dented to the same level as the initial if statement...






          share|improve this answer




















          • so, it's not a compulsive requirement?
            – JawSaw
            Apr 3 at 2:38










          • @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
            – user1404316
            Apr 3 at 3:15











          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%2f435176%2fwhen-comment-out-exit-code-perform-well%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
          2
          down vote



          accepted










          The exit command not only exits a script, but also sets an exit code, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash and similar shells, one can view or interrogate the exit code by examining shell variable $?.



          Also, BTW, you have an indentation problem. The else clause should be out-dented to the same level as the initial if statement...






          share|improve this answer




















          • so, it's not a compulsive requirement?
            – JawSaw
            Apr 3 at 2:38










          • @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
            – user1404316
            Apr 3 at 3:15















          up vote
          2
          down vote



          accepted










          The exit command not only exits a script, but also sets an exit code, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash and similar shells, one can view or interrogate the exit code by examining shell variable $?.



          Also, BTW, you have an indentation problem. The else clause should be out-dented to the same level as the initial if statement...






          share|improve this answer




















          • so, it's not a compulsive requirement?
            – JawSaw
            Apr 3 at 2:38










          • @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
            – user1404316
            Apr 3 at 3:15













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The exit command not only exits a script, but also sets an exit code, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash and similar shells, one can view or interrogate the exit code by examining shell variable $?.



          Also, BTW, you have an indentation problem. The else clause should be out-dented to the same level as the initial if statement...






          share|improve this answer












          The exit command not only exits a script, but also sets an exit code, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash and similar shells, one can view or interrogate the exit code by examining shell variable $?.



          Also, BTW, you have an indentation problem. The else clause should be out-dented to the same level as the initial if statement...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 3 at 2:19









          user1404316

          2,314520




          2,314520











          • so, it's not a compulsive requirement?
            – JawSaw
            Apr 3 at 2:38










          • @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
            – user1404316
            Apr 3 at 3:15

















          • so, it's not a compulsive requirement?
            – JawSaw
            Apr 3 at 2:38










          • @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
            – user1404316
            Apr 3 at 3:15
















          so, it's not a compulsive requirement?
          – JawSaw
          Apr 3 at 2:38




          so, it's not a compulsive requirement?
          – JawSaw
          Apr 3 at 2:38












          @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
          – user1404316
          Apr 3 at 3:15





          @Tool - Using exit commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
          – user1404316
          Apr 3 at 3:15













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435176%2fwhen-comment-out-exit-code-perform-well%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay