Bash for loop does run foreverBash script, string not replaced correctly with escaped $ and &...

Giving a talk in my old university, how prominently should I tell students my salary?

How to get the first element while continue streaming?

Can I solder 12/2 Romex to extend wire 5 ft?

Formatting a table to look nice

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Is there a frame of reference in which I was born before I was conceived?

Create chunks from an array

Lock enemy's y-axis when using Vector3.MoveTowards to follow the player

Rationale to prefer local variables over instance variables?

3.5% Interest Student Loan or use all of my savings on Tuition?

PTIJ: Is all laundering forbidden during the 9 days?

How does signal strength relate to bandwidth?

How can I conditionally format my HTML table?

Meaning of word ягоза

Deal the cards to the players

I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?

When was drinking water recognized as crucial in marathon running?

Being asked to review a paper in conference one has submitted to

Script that counts quarters, dimes, nickels, and pennies

Where is the fallacy here?

I can't die. Who am I?

Can a Trickery Domain cleric cast a spell through the Invoke Duplicity clone while inside a Forcecage?

Why won't the strings command stop?

Make me a metasequence



Bash for loop does run forever


Bash script, string not replaced correctly with escaped $ and & charactersweird behaviour with forever, bash, apache and phpBASH - run commands before and after loopWhile loop Bash script that outputs how many users use a certain shellLoop to execute multiple instructions in bash, which start from the top if one instruction failBatch file input issues on remote computerLast background process ID ($!) not working in sudo bash commandBash script that itself generates bash commandsHow do I make my backspace key functional in a bash script for Linux being accessed remotely from Windows?What am I doing wrong in this bash script?













0















Okay so i build a script to send me notifications after a certain amount of time.
Here's the part which doesn't work (will highlight important stuff later):



if [[ $choice == "-c" ]]
then

timer=$2
if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi
echo $timer

if [[ $5 == "-h" ]]
then
timer=$(( $2 * 3600 ))
fi

echo "Notification will be send in:" $timer "Second(s)"
for (( index = 0; index < $timer; index++ ))
do
sleep 1
done
notify-send -t $time $msg
fi


I call the script with



./scriptname <type> <time it should run> [message] [set the time to m/h]


The type doesn't matter here because it executes the correct if block.
The time it should run is how long it waits till the notification is send.
The set the time to m/h is a simple parameter. If i give nothing in here the time will be given in seconds (it always is but the user input isn't changed).



Okay so.
The $msg variable contains the message which will get send.
$2is the time input.
$choice is not really important all it does is select the correct code to execute (and the programs comes in this if statement)
$5 contains either '-m' or '-h' (nothing is also an option) and then the $timer variable gets increased by the following.



The problem is that if i want to have it in minutes (for what the "-m" stands). The programm will execute this statement:



if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi


With the echo $timer i know that it worked but the problem i got is that the script doesn't stop running. It runs perfectly when i just execute it with the given input. (So the program doesn't go into any if statements for the $timer variable.) But if i say that the time should be treated as minutes or hours (just tested with minutes) it doesn't work, or to be more correct it just doesn't override the $timer variable
Does anybody know why?










share|improve this question




















  • 1





    Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

    – xenoid
    yesterday











  • Is your script also named sleep?

    – grawity
    yesterday











  • Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

    – glenn jackman
    yesterday













  • For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

    – glenn jackman
    yesterday


















0















Okay so i build a script to send me notifications after a certain amount of time.
Here's the part which doesn't work (will highlight important stuff later):



if [[ $choice == "-c" ]]
then

timer=$2
if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi
echo $timer

if [[ $5 == "-h" ]]
then
timer=$(( $2 * 3600 ))
fi

echo "Notification will be send in:" $timer "Second(s)"
for (( index = 0; index < $timer; index++ ))
do
sleep 1
done
notify-send -t $time $msg
fi


I call the script with



./scriptname <type> <time it should run> [message] [set the time to m/h]


The type doesn't matter here because it executes the correct if block.
The time it should run is how long it waits till the notification is send.
The set the time to m/h is a simple parameter. If i give nothing in here the time will be given in seconds (it always is but the user input isn't changed).



Okay so.
The $msg variable contains the message which will get send.
$2is the time input.
$choice is not really important all it does is select the correct code to execute (and the programs comes in this if statement)
$5 contains either '-m' or '-h' (nothing is also an option) and then the $timer variable gets increased by the following.



The problem is that if i want to have it in minutes (for what the "-m" stands). The programm will execute this statement:



if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi


With the echo $timer i know that it worked but the problem i got is that the script doesn't stop running. It runs perfectly when i just execute it with the given input. (So the program doesn't go into any if statements for the $timer variable.) But if i say that the time should be treated as minutes or hours (just tested with minutes) it doesn't work, or to be more correct it just doesn't override the $timer variable
Does anybody know why?










share|improve this question




















  • 1





    Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

    – xenoid
    yesterday











  • Is your script also named sleep?

    – grawity
    yesterday











  • Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

    – glenn jackman
    yesterday













  • For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

    – glenn jackman
    yesterday
















0












0








0








Okay so i build a script to send me notifications after a certain amount of time.
Here's the part which doesn't work (will highlight important stuff later):



if [[ $choice == "-c" ]]
then

timer=$2
if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi
echo $timer

if [[ $5 == "-h" ]]
then
timer=$(( $2 * 3600 ))
fi

echo "Notification will be send in:" $timer "Second(s)"
for (( index = 0; index < $timer; index++ ))
do
sleep 1
done
notify-send -t $time $msg
fi


I call the script with



./scriptname <type> <time it should run> [message] [set the time to m/h]


The type doesn't matter here because it executes the correct if block.
The time it should run is how long it waits till the notification is send.
The set the time to m/h is a simple parameter. If i give nothing in here the time will be given in seconds (it always is but the user input isn't changed).



Okay so.
The $msg variable contains the message which will get send.
$2is the time input.
$choice is not really important all it does is select the correct code to execute (and the programs comes in this if statement)
$5 contains either '-m' or '-h' (nothing is also an option) and then the $timer variable gets increased by the following.



The problem is that if i want to have it in minutes (for what the "-m" stands). The programm will execute this statement:



if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi


With the echo $timer i know that it worked but the problem i got is that the script doesn't stop running. It runs perfectly when i just execute it with the given input. (So the program doesn't go into any if statements for the $timer variable.) But if i say that the time should be treated as minutes or hours (just tested with minutes) it doesn't work, or to be more correct it just doesn't override the $timer variable
Does anybody know why?










share|improve this question
















Okay so i build a script to send me notifications after a certain amount of time.
Here's the part which doesn't work (will highlight important stuff later):



if [[ $choice == "-c" ]]
then

timer=$2
if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi
echo $timer

if [[ $5 == "-h" ]]
then
timer=$(( $2 * 3600 ))
fi

echo "Notification will be send in:" $timer "Second(s)"
for (( index = 0; index < $timer; index++ ))
do
sleep 1
done
notify-send -t $time $msg
fi


I call the script with



./scriptname <type> <time it should run> [message] [set the time to m/h]


The type doesn't matter here because it executes the correct if block.
The time it should run is how long it waits till the notification is send.
The set the time to m/h is a simple parameter. If i give nothing in here the time will be given in seconds (it always is but the user input isn't changed).



Okay so.
The $msg variable contains the message which will get send.
$2is the time input.
$choice is not really important all it does is select the correct code to execute (and the programs comes in this if statement)
$5 contains either '-m' or '-h' (nothing is also an option) and then the $timer variable gets increased by the following.



The problem is that if i want to have it in minutes (for what the "-m" stands). The programm will execute this statement:



if [[ $5 == "-m" ]]
then
timer=$(( $2 * 60 ))
fi


With the echo $timer i know that it worked but the problem i got is that the script doesn't stop running. It runs perfectly when i just execute it with the given input. (So the program doesn't go into any if statements for the $timer variable.) But if i say that the time should be treated as minutes or hours (just tested with minutes) it doesn't work, or to be more correct it just doesn't override the $timer variable
Does anybody know why?







linux command-line bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







Stefan xyz

















asked yesterday









Stefan xyzStefan xyz

63




63








  • 1





    Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

    – xenoid
    yesterday











  • Is your script also named sleep?

    – grawity
    yesterday











  • Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

    – glenn jackman
    yesterday













  • For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

    – glenn jackman
    yesterday
















  • 1





    Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

    – xenoid
    yesterday











  • Is your script also named sleep?

    – grawity
    yesterday











  • Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

    – glenn jackman
    yesterday













  • For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

    – glenn jackman
    yesterday










1




1





Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

– xenoid
yesterday





Why don't you use sleep to its full extent? sleep 5m will sleep 5 minutes. No loops, no awkward parameter parsing...

– xenoid
yesterday













Is your script also named sleep?

– grawity
yesterday





Is your script also named sleep?

– grawity
yesterday













Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

– glenn jackman
yesterday







Why are you looping instead of using sleep $timer? You can spawn sleep once or $timer times.

– glenn jackman
yesterday















For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

– glenn jackman
yesterday







For using options, I'd recommend getopts -- then you're not forcing your users to enter options in a particular order.

– glenn jackman
yesterday












1 Answer
1






active

oldest

votes


















3















./scriptname <type> <time it should run> [message] [set the time to m/h]



[set the time to m/h] is $4. Your code tests against $5.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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%2fsuperuser.com%2fquestions%2f1411603%2fbash-for-loop-does-run-forever%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3















    ./scriptname <type> <time it should run> [message] [set the time to m/h]



    [set the time to m/h] is $4. Your code tests against $5.






    share|improve this answer




























      3















      ./scriptname <type> <time it should run> [message] [set the time to m/h]



      [set the time to m/h] is $4. Your code tests against $5.






      share|improve this answer


























        3












        3








        3








        ./scriptname <type> <time it should run> [message] [set the time to m/h]



        [set the time to m/h] is $4. Your code tests against $5.






        share|improve this answer














        ./scriptname <type> <time it should run> [message] [set the time to m/h]



        [set the time to m/h] is $4. Your code tests against $5.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Kamil MaciorowskiKamil Maciorowski

        28k156184




        28k156184






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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%2fsuperuser.com%2fquestions%2f1411603%2fbash-for-loop-does-run-forever%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

            VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

            Tribunal Administrativo e Fiscal de Mirandela Referências Menu de...

            looking for continuous Screen Capture for retroactivly reproducing errors, timeback machineRolling desktop...