How to debug a script by removing the “if”?












0















I have the following code:



debug=$?
function a {
su - javi -c "uptime"
return $debug
}

function b {
su - javi -c "cat /etc/redhat-release"
return $debug
}

function c {
su - javi -c "cat /etc/redhat-release"
return $debug
}

case $debug in
0)
a
if [ $debug == 0 ]; then
b
echo "se ejcuta la funcion"
elif [ $debug == 0 ]
c
elif.... <-----this

fi
;;

1)
echo "se ha producido un error"
;;

esac


Is there any way to debug by removing the if ??I want them to go running a function if it ends well that jumps to the other function and if it does not end well that it leaves the escript, that with 5 functions










share|improve this question

























  • ìf false ; thenfollowed by # if?

    – Archemar
    Feb 6 at 19:39








  • 1





    You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

    – roaima
    Feb 6 at 19:48











  • To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

    – Tim Kennedy
    Feb 6 at 19:48






  • 1





    @Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

    – roaima
    Feb 6 at 19:49








  • 1





    Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

    – roaima
    Feb 6 at 19:51


















0















I have the following code:



debug=$?
function a {
su - javi -c "uptime"
return $debug
}

function b {
su - javi -c "cat /etc/redhat-release"
return $debug
}

function c {
su - javi -c "cat /etc/redhat-release"
return $debug
}

case $debug in
0)
a
if [ $debug == 0 ]; then
b
echo "se ejcuta la funcion"
elif [ $debug == 0 ]
c
elif.... <-----this

fi
;;

1)
echo "se ha producido un error"
;;

esac


Is there any way to debug by removing the if ??I want them to go running a function if it ends well that jumps to the other function and if it does not end well that it leaves the escript, that with 5 functions










share|improve this question

























  • ìf false ; thenfollowed by # if?

    – Archemar
    Feb 6 at 19:39








  • 1





    You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

    – roaima
    Feb 6 at 19:48











  • To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

    – Tim Kennedy
    Feb 6 at 19:48






  • 1





    @Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

    – roaima
    Feb 6 at 19:49








  • 1





    Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

    – roaima
    Feb 6 at 19:51
















0












0








0








I have the following code:



debug=$?
function a {
su - javi -c "uptime"
return $debug
}

function b {
su - javi -c "cat /etc/redhat-release"
return $debug
}

function c {
su - javi -c "cat /etc/redhat-release"
return $debug
}

case $debug in
0)
a
if [ $debug == 0 ]; then
b
echo "se ejcuta la funcion"
elif [ $debug == 0 ]
c
elif.... <-----this

fi
;;

1)
echo "se ha producido un error"
;;

esac


Is there any way to debug by removing the if ??I want them to go running a function if it ends well that jumps to the other function and if it does not end well that it leaves the escript, that with 5 functions










share|improve this question
















I have the following code:



debug=$?
function a {
su - javi -c "uptime"
return $debug
}

function b {
su - javi -c "cat /etc/redhat-release"
return $debug
}

function c {
su - javi -c "cat /etc/redhat-release"
return $debug
}

case $debug in
0)
a
if [ $debug == 0 ]; then
b
echo "se ejcuta la funcion"
elif [ $debug == 0 ]
c
elif.... <-----this

fi
;;

1)
echo "se ha producido un error"
;;

esac


Is there any way to debug by removing the if ??I want them to go running a function if it ends well that jumps to the other function and if it does not end well that it leaves the escript, that with 5 functions







bash shell-script shell scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 6 at 20:02









ilkkachu

59.4k894168




59.4k894168










asked Feb 6 at 19:37









ortigaortiga

83




83













  • ìf false ; thenfollowed by # if?

    – Archemar
    Feb 6 at 19:39








  • 1





    You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

    – roaima
    Feb 6 at 19:48











  • To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

    – Tim Kennedy
    Feb 6 at 19:48






  • 1





    @Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

    – roaima
    Feb 6 at 19:49








  • 1





    Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

    – roaima
    Feb 6 at 19:51





















  • ìf false ; thenfollowed by # if?

    – Archemar
    Feb 6 at 19:39








  • 1





    You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

    – roaima
    Feb 6 at 19:48











  • To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

    – Tim Kennedy
    Feb 6 at 19:48






  • 1





    @Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

    – roaima
    Feb 6 at 19:49








  • 1





    Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

    – roaima
    Feb 6 at 19:51



















ìf false ; thenfollowed by # if?

– Archemar
Feb 6 at 19:39







ìf false ; thenfollowed by # if?

– Archemar
Feb 6 at 19:39






1




1





You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

– roaima
Feb 6 at 19:48





You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway?

– roaima
Feb 6 at 19:48













To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

– Tim Kennedy
Feb 6 at 19:48





To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run?

– Tim Kennedy
Feb 6 at 19:48




1




1





@Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

– roaima
Feb 6 at 19:49







@Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh

– roaima
Feb 6 at 19:49






1




1





Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

– roaima
Feb 6 at 19:51







Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one.

– roaima
Feb 6 at 19:51












2 Answers
2






active

oldest

votes


















0














If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:



function bail {
echo "se ha producido un error ($1)"
exit 1
}

function a {
uptime
}

function b {
cat /etc/redhat-release
}

function c {
cat /etc/redhat-release
}

for f in 'a' 'b' 'c'; do
$f || bail "$f: $?"
done


This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.



I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.






share|improve this answer































    0
















    1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.

    2. The return code of a function is the return code of its last command. So the return statements are pointless.


    In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.






    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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      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%2f499144%2fhow-to-debug-a-script-by-removing-the-if%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:



      function bail {
      echo "se ha producido un error ($1)"
      exit 1
      }

      function a {
      uptime
      }

      function b {
      cat /etc/redhat-release
      }

      function c {
      cat /etc/redhat-release
      }

      for f in 'a' 'b' 'c'; do
      $f || bail "$f: $?"
      done


      This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.



      I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.






      share|improve this answer




























        0














        If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:



        function bail {
        echo "se ha producido un error ($1)"
        exit 1
        }

        function a {
        uptime
        }

        function b {
        cat /etc/redhat-release
        }

        function c {
        cat /etc/redhat-release
        }

        for f in 'a' 'b' 'c'; do
        $f || bail "$f: $?"
        done


        This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.



        I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.






        share|improve this answer


























          0












          0








          0







          If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:



          function bail {
          echo "se ha producido un error ($1)"
          exit 1
          }

          function a {
          uptime
          }

          function b {
          cat /etc/redhat-release
          }

          function c {
          cat /etc/redhat-release
          }

          for f in 'a' 'b' 'c'; do
          $f || bail "$f: $?"
          done


          This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.



          I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.






          share|improve this answer













          If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:



          function bail {
          echo "se ha producido un error ($1)"
          exit 1
          }

          function a {
          uptime
          }

          function b {
          cat /etc/redhat-release
          }

          function c {
          cat /etc/redhat-release
          }

          for f in 'a' 'b' 'c'; do
          $f || bail "$f: $?"
          done


          This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.



          I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 6 at 20:43









          Tim KennedyTim Kennedy

          14.5k23050




          14.5k23050

























              0
















              1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.

              2. The return code of a function is the return code of its last command. So the return statements are pointless.


              In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.






              share|improve this answer




























                0
















                1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.

                2. The return code of a function is the return code of its last command. So the return statements are pointless.


                In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.






                share|improve this answer


























                  0












                  0








                  0









                  1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.

                  2. The return code of a function is the return code of its last command. So the return statements are pointless.


                  In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.






                  share|improve this answer















                  1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.

                  2. The return code of a function is the return code of its last command. So the return statements are pointless.


                  In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 6 at 21:32









                  l0b0l0b0

                  28.2k18119246




                  28.2k18119246






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499144%2fhow-to-debug-a-script-by-removing-the-if%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      Touch on Surface Book