shell script to exit out of the script if diskspace is more than 75












1














I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.



df -kh | awk '{if( 0+$5 >= 75 ) exit;}'


Trying above command, its not working. Can anyone help me on this.










share|improve this question





























    1














    I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.



    df -kh | awk '{if( 0+$5 >= 75 ) exit;}'


    Trying above command, its not working. Can anyone help me on this.










    share|improve this question



























      1












      1








      1







      I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.



      df -kh | awk '{if( 0+$5 >= 75 ) exit;}'


      Trying above command, its not working. Can anyone help me on this.










      share|improve this question















      I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.



      df -kh | awk '{if( 0+$5 >= 75 ) exit;}'


      Trying above command, its not working. Can anyone help me on this.







      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:33









      Inian

      38.8k63770




      38.8k63770










      asked Nov 13 '18 at 3:15









      Himavanth

      103




      103
























          2 Answers
          2






          active

          oldest

          votes


















          0














          This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.



          df -hP  | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'


          OR



          df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'


          OR with mount name who is the culprit for breaching threshold.



          df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}' 


          In case you don't have -P option in your box then try following.



          df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev"  has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'


          I am using print statement to make sure exit is working. also -P option was tested on BASH systems.



          Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)



          if [[ $? -eq 1 ]]
          then
          echo "Exiting the complete script now..."
          exit
          else
          echo "Looks good so going further in script now.."
          fi





          share|improve this answer























          • Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
            – Himavanth
            Nov 13 '18 at 3:36












          • @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
            – RavinderSingh13
            Nov 13 '18 at 3:39





















          0














          If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:



          if df -kh | awk  '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi


          Don't forget that df returns one line per mount point, you can do:



          if dk -kh /home ....


          to check for a particular mount point.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53273270%2fshell-script-to-exit-out-of-the-script-if-diskspace-is-more-than-75%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














            This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.



            df -hP  | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'


            OR



            df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'


            OR with mount name who is the culprit for breaching threshold.



            df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}' 


            In case you don't have -P option in your box then try following.



            df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev"  has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'


            I am using print statement to make sure exit is working. also -P option was tested on BASH systems.



            Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)



            if [[ $? -eq 1 ]]
            then
            echo "Exiting the complete script now..."
            exit
            else
            echo "Looks good so going further in script now.."
            fi





            share|improve this answer























            • Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
              – Himavanth
              Nov 13 '18 at 3:36












            • @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
              – RavinderSingh13
              Nov 13 '18 at 3:39


















            0














            This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.



            df -hP  | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'


            OR



            df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'


            OR with mount name who is the culprit for breaching threshold.



            df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}' 


            In case you don't have -P option in your box then try following.



            df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev"  has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'


            I am using print statement to make sure exit is working. also -P option was tested on BASH systems.



            Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)



            if [[ $? -eq 1 ]]
            then
            echo "Exiting the complete script now..."
            exit
            else
            echo "Looks good so going further in script now.."
            fi





            share|improve this answer























            • Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
              – Himavanth
              Nov 13 '18 at 3:36












            • @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
              – RavinderSingh13
              Nov 13 '18 at 3:39
















            0












            0








            0






            This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.



            df -hP  | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'


            OR



            df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'


            OR with mount name who is the culprit for breaching threshold.



            df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}' 


            In case you don't have -P option in your box then try following.



            df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev"  has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'


            I am using print statement to make sure exit is working. also -P option was tested on BASH systems.



            Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)



            if [[ $? -eq 1 ]]
            then
            echo "Exiting the complete script now..."
            exit
            else
            echo "Looks good so going further in script now.."
            fi





            share|improve this answer














            This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.



            df -hP  | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'


            OR



            df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'


            OR with mount name who is the culprit for breaching threshold.



            df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}' 


            In case you don't have -P option in your box then try following.



            df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev"  has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'


            I am using print statement to make sure exit is working. also -P option was tested on BASH systems.



            Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)



            if [[ $? -eq 1 ]]
            then
            echo "Exiting the complete script now..."
            exit
            else
            echo "Looks good so going further in script now.."
            fi






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 3:40

























            answered Nov 13 '18 at 3:23









            RavinderSingh13

            25.7k41438




            25.7k41438












            • Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
              – Himavanth
              Nov 13 '18 at 3:36












            • @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
              – RavinderSingh13
              Nov 13 '18 at 3:39




















            • Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
              – Himavanth
              Nov 13 '18 at 3:36












            • @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
              – RavinderSingh13
              Nov 13 '18 at 3:39


















            Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
            – Himavanth
            Nov 13 '18 at 3:36






            Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
            – Himavanth
            Nov 13 '18 at 3:36














            @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
            – RavinderSingh13
            Nov 13 '18 at 3:39






            @Himavanth, use exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?
            – RavinderSingh13
            Nov 13 '18 at 3:39















            0














            If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:



            if df -kh | awk  '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi


            Don't forget that df returns one line per mount point, you can do:



            if dk -kh /home ....


            to check for a particular mount point.






            share|improve this answer


























              0














              If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:



              if df -kh | awk  '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi


              Don't forget that df returns one line per mount point, you can do:



              if dk -kh /home ....


              to check for a particular mount point.






              share|improve this answer
























                0












                0








                0






                If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:



                if df -kh | awk  '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi


                Don't forget that df returns one line per mount point, you can do:



                if dk -kh /home ....


                to check for a particular mount point.






                share|improve this answer












                If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:



                if df -kh | awk  '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi


                Don't forget that df returns one line per mount point, you can do:



                if dk -kh /home ....


                to check for a particular mount point.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 3:32









                perreal

                71.8k9110138




                71.8k9110138






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2fstackoverflow.com%2fquestions%2f53273270%2fshell-script-to-exit-out-of-the-script-if-diskspace-is-more-than-75%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly