Sleep certain sections of the script












0














I'm completely new to bash scripting, my english isn't very good.



I want to pause/sleep the elif part but keep the elif working.I don't want the whole script to sleep.



elif ["$test"="$true1"];then
echo "good read 1" &
elif ["$test"="$true2"];then
echo "good read 2" &


So if test=true2 is triggered it waits for a couple seconds before sending a "good read 2", but it doesn't sleep the other elif statement so if test=test1 it will send that while the other elif part sleeps.










share|improve this question







New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why do you use echo ... &? This will run echo in the background which usually is not required.
    – nohillside
    35 mins ago










  • Why isn't it required?
    – securitytestman
    28 mins ago










  • What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
    – nohillside
    23 mins ago






  • 1




    @securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
    – Haxiel
    21 mins ago










  • Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
    – securitytestman
    22 secs ago
















0














I'm completely new to bash scripting, my english isn't very good.



I want to pause/sleep the elif part but keep the elif working.I don't want the whole script to sleep.



elif ["$test"="$true1"];then
echo "good read 1" &
elif ["$test"="$true2"];then
echo "good read 2" &


So if test=true2 is triggered it waits for a couple seconds before sending a "good read 2", but it doesn't sleep the other elif statement so if test=test1 it will send that while the other elif part sleeps.










share|improve this question







New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why do you use echo ... &? This will run echo in the background which usually is not required.
    – nohillside
    35 mins ago










  • Why isn't it required?
    – securitytestman
    28 mins ago










  • What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
    – nohillside
    23 mins ago






  • 1




    @securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
    – Haxiel
    21 mins ago










  • Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
    – securitytestman
    22 secs ago














0












0








0







I'm completely new to bash scripting, my english isn't very good.



I want to pause/sleep the elif part but keep the elif working.I don't want the whole script to sleep.



elif ["$test"="$true1"];then
echo "good read 1" &
elif ["$test"="$true2"];then
echo "good read 2" &


So if test=true2 is triggered it waits for a couple seconds before sending a "good read 2", but it doesn't sleep the other elif statement so if test=test1 it will send that while the other elif part sleeps.










share|improve this question







New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm completely new to bash scripting, my english isn't very good.



I want to pause/sleep the elif part but keep the elif working.I don't want the whole script to sleep.



elif ["$test"="$true1"];then
echo "good read 1" &
elif ["$test"="$true2"];then
echo "good read 2" &


So if test=true2 is triggered it waits for a couple seconds before sending a "good read 2", but it doesn't sleep the other elif statement so if test=test1 it will send that while the other elif part sleeps.







bash sleep






share|improve this question







New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 41 mins ago









securitytestman

1




1




New contributor




securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






securitytestman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Why do you use echo ... &? This will run echo in the background which usually is not required.
    – nohillside
    35 mins ago










  • Why isn't it required?
    – securitytestman
    28 mins ago










  • What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
    – nohillside
    23 mins ago






  • 1




    @securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
    – Haxiel
    21 mins ago










  • Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
    – securitytestman
    22 secs ago


















  • Why do you use echo ... &? This will run echo in the background which usually is not required.
    – nohillside
    35 mins ago










  • Why isn't it required?
    – securitytestman
    28 mins ago










  • What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
    – nohillside
    23 mins ago






  • 1




    @securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
    – Haxiel
    21 mins ago










  • Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
    – securitytestman
    22 secs ago
















Why do you use echo ... &? This will run echo in the background which usually is not required.
– nohillside
35 mins ago




Why do you use echo ... &? This will run echo in the background which usually is not required.
– nohillside
35 mins ago












Why isn't it required?
– securitytestman
28 mins ago




Why isn't it required?
– securitytestman
28 mins ago












What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
– nohillside
23 mins ago




What do you want to accomplish with it? Sending it into the background takes much longer than just the echo alone, so unless this is just a sample and you want to kick off a long-running command in your real code it doesn't seem to make sense.
– nohillside
23 mins ago




1




1




@securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
– Haxiel
21 mins ago




@securitytestman Regarding the '..doesn't sleep the other elif statement..' part from your question. The elif blocks are read sequentially, so there is no parallel operation happening there.
– Haxiel
21 mins ago












Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
– securitytestman
22 secs ago




Haxiel does this mean if I want them to act as different parts of the script I will need to use something other than elif?
– securitytestman
22 secs ago










2 Answers
2






active

oldest

votes


















0














In scripts you can use practically all commands you know from using the shell interactively. The command to sleep is called sleep, it takes one argument which is the time to sleep in seconds.



Looking at the snippet of your code this would lead to



elif [ "$test" = "$true1" ]; then
echo "good read 1"
elif [ "$test" = "$true2" ]; then
sleep 42
echo "good read 2"


If you want to just delay the output and have the script continue you can use



elif [ "$test" = "$true1" ]; then
echo "good read 1"
elif [ "$test" = "$true2" ]; then
sleep 42 && echo "good read 2" &


This will run sleep 42 && echo "good read 2" in the background.






share|improve this answer























  • How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
    – securitytestman
    28 mins ago










  • @securitytestman see edit
    – nohillside
    21 mins ago










  • Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
    – securitytestman
    2 mins ago



















0














elif [ "$test" = "$true1" ]; then
echo "good read 1"
elif [ "$test" = "$true2" ]; then
(
sleep 42
echo "good read 2
#some stuff..
) &


That's will make a subshell inside your BASH script that wait 42 seconds before doing something, without stoping the rest of the script. Is that what you want?



(Note consider putting #!/bin/bash in the start of the script to guarantee you are running bash)



And why you just hate to use 'case' and start to use this horrible 'if' structures? Are you sure you really need to use if instead of case?



And please put the rest of the script... or a full sample.





share























    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
    });


    }
    });






    securitytestman is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f491280%2fsleep-certain-sections-of-the-script%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














    In scripts you can use practically all commands you know from using the shell interactively. The command to sleep is called sleep, it takes one argument which is the time to sleep in seconds.



    Looking at the snippet of your code this would lead to



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42
    echo "good read 2"


    If you want to just delay the output and have the script continue you can use



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42 && echo "good read 2" &


    This will run sleep 42 && echo "good read 2" in the background.






    share|improve this answer























    • How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
      – securitytestman
      28 mins ago










    • @securitytestman see edit
      – nohillside
      21 mins ago










    • Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
      – securitytestman
      2 mins ago
















    0














    In scripts you can use practically all commands you know from using the shell interactively. The command to sleep is called sleep, it takes one argument which is the time to sleep in seconds.



    Looking at the snippet of your code this would lead to



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42
    echo "good read 2"


    If you want to just delay the output and have the script continue you can use



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42 && echo "good read 2" &


    This will run sleep 42 && echo "good read 2" in the background.






    share|improve this answer























    • How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
      – securitytestman
      28 mins ago










    • @securitytestman see edit
      – nohillside
      21 mins ago










    • Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
      – securitytestman
      2 mins ago














    0












    0








    0






    In scripts you can use practically all commands you know from using the shell interactively. The command to sleep is called sleep, it takes one argument which is the time to sleep in seconds.



    Looking at the snippet of your code this would lead to



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42
    echo "good read 2"


    If you want to just delay the output and have the script continue you can use



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42 && echo "good read 2" &


    This will run sleep 42 && echo "good read 2" in the background.






    share|improve this answer














    In scripts you can use practically all commands you know from using the shell interactively. The command to sleep is called sleep, it takes one argument which is the time to sleep in seconds.



    Looking at the snippet of your code this would lead to



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42
    echo "good read 2"


    If you want to just delay the output and have the script continue you can use



    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    sleep 42 && echo "good read 2" &


    This will run sleep 42 && echo "good read 2" in the background.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 22 mins ago

























    answered 36 mins ago









    nohillside

    2,292819




    2,292819












    • How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
      – securitytestman
      28 mins ago










    • @securitytestman see edit
      – nohillside
      21 mins ago










    • Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
      – securitytestman
      2 mins ago


















    • How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
      – securitytestman
      28 mins ago










    • @securitytestman see edit
      – nohillside
      21 mins ago










    • Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
      – securitytestman
      2 mins ago
















    How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
    – securitytestman
    28 mins ago




    How would I make it so they both were to sleep if triggered but not effect each elif statement ? I don't want the whole script paused.
    – securitytestman
    28 mins ago












    @securitytestman see edit
    – nohillside
    21 mins ago




    @securitytestman see edit
    – nohillside
    21 mins ago












    Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
    – securitytestman
    2 mins ago




    Would this also work then ? elif [ "$test" = "$true1" ]; then sleep 42 && echo "good read 1" & elif [ "$test" = "$true2" ]; then sleep 42 && echo "good read 2" &
    – securitytestman
    2 mins ago













    0














    elif [ "$test" = "$true1" ]; then
    echo "good read 1"
    elif [ "$test" = "$true2" ]; then
    (
    sleep 42
    echo "good read 2
    #some stuff..
    ) &


    That's will make a subshell inside your BASH script that wait 42 seconds before doing something, without stoping the rest of the script. Is that what you want?



    (Note consider putting #!/bin/bash in the start of the script to guarantee you are running bash)



    And why you just hate to use 'case' and start to use this horrible 'if' structures? Are you sure you really need to use if instead of case?



    And please put the rest of the script... or a full sample.





    share




























      0














      elif [ "$test" = "$true1" ]; then
      echo "good read 1"
      elif [ "$test" = "$true2" ]; then
      (
      sleep 42
      echo "good read 2
      #some stuff..
      ) &


      That's will make a subshell inside your BASH script that wait 42 seconds before doing something, without stoping the rest of the script. Is that what you want?



      (Note consider putting #!/bin/bash in the start of the script to guarantee you are running bash)



      And why you just hate to use 'case' and start to use this horrible 'if' structures? Are you sure you really need to use if instead of case?



      And please put the rest of the script... or a full sample.





      share


























        0












        0








        0






        elif [ "$test" = "$true1" ]; then
        echo "good read 1"
        elif [ "$test" = "$true2" ]; then
        (
        sleep 42
        echo "good read 2
        #some stuff..
        ) &


        That's will make a subshell inside your BASH script that wait 42 seconds before doing something, without stoping the rest of the script. Is that what you want?



        (Note consider putting #!/bin/bash in the start of the script to guarantee you are running bash)



        And why you just hate to use 'case' and start to use this horrible 'if' structures? Are you sure you really need to use if instead of case?



        And please put the rest of the script... or a full sample.





        share














        elif [ "$test" = "$true1" ]; then
        echo "good read 1"
        elif [ "$test" = "$true2" ]; then
        (
        sleep 42
        echo "good read 2
        #some stuff..
        ) &


        That's will make a subshell inside your BASH script that wait 42 seconds before doing something, without stoping the rest of the script. Is that what you want?



        (Note consider putting #!/bin/bash in the start of the script to guarantee you are running bash)



        And why you just hate to use 'case' and start to use this horrible 'if' structures? Are you sure you really need to use if instead of case?



        And please put the rest of the script... or a full sample.






        share













        share


        share








        edited 2 mins ago

























        answered 8 mins ago









        Luciano Andress Martini

        3,453931




        3,453931






















            securitytestman is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            securitytestman is a new contributor. Be nice, and check out our Code of Conduct.













            securitytestman is a new contributor. Be nice, and check out our Code of Conduct.












            securitytestman is a new contributor. Be nice, and check out our Code of Conduct.
















            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.





            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%2funix.stackexchange.com%2fquestions%2f491280%2fsleep-certain-sections-of-the-script%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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt