How to modify these bash functions into one?Checking if an input number is an integerIs it possible to write portable service scripts to control a daemon?How do I use vim on the command line to add text to the middle of a file?How to merge these commands into one?Stable timing for shell scripts?Script to run commands as soon as a key is hitForward stdout to different ttyControl characters in a terminal with an active foreground processHow can I swap two characters in the command line input?How can I turn these instructions into a loop?Bash: Complete wrapper for command line programs

Send out email when Apex Queueable fails and test it

Processor speed limited at 0.4 Ghz

how do we prove that a sum of two periods is still a period?

Forgetting the musical notes while performing in concert

What is a Samsaran Word™?

How to travel to Japan while expressing milk?

Is it possible to create a QR code using text?

Different meanings of こわい

How does a dynamic QR code work?

What is the fastest integer factorization to break RSA?

How to prevent "they're falling in love" trope

What are the G forces leaving Earth orbit?

Implication of namely

How do I exit BASH while loop using modulus operator?

Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

What is the opposite of "eschatology"?

Why are UK visa biometrics appointments suspended at USCIS Application Support Centers?

Is it a bad idea to plug the other end of ESD strap to wall ground?

What exactly is ineptocracy?

Did 'Cinema Songs' exist during Hiranyakshipu's time?

Do Iron Man suits sport waste management systems?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

How to Prove P(a) → ∀x(P(x) ∨ ¬(x = a)) using Natural Deduction



How to modify these bash functions into one?


Checking if an input number is an integerIs it possible to write portable service scripts to control a daemon?How do I use vim on the command line to add text to the middle of a file?How to merge these commands into one?Stable timing for shell scripts?Script to run commands as soon as a key is hitForward stdout to different ttyControl characters in a terminal with an active foreground processHow can I swap two characters in the command line input?How can I turn these instructions into a loop?Bash: Complete wrapper for command line programs













2















Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.



function time_seconds()

echo "Counting to $1 seconds"
time_func $1


function time_minutes()

echo "Counting to $1 minutes"
time_func $1*60


function time_hours()

echo "Counting to $1 hours"
time_func $1*60*60


function time_flexible() # accepts flexible input hh:mm:ss (does not work)

echo "Counting to $1"
secs=$(time2seconds $1)
time_func $secs



enter image description here



I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:



sleep 5h 22m 44s


I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:



stopwatch 5h 22m 44s


How would I go about accomplishing this?



p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.










share|improve this question
























  • Can you convert that picture of text, to text.

    – ctrl-alt-delor
    2 days ago











  • Thanks for contributing! But please, don't post images of text (read this for reasons why).

    – Kusalananda
    2 days ago















2















Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.



function time_seconds()

echo "Counting to $1 seconds"
time_func $1


function time_minutes()

echo "Counting to $1 minutes"
time_func $1*60


function time_hours()

echo "Counting to $1 hours"
time_func $1*60*60


function time_flexible() # accepts flexible input hh:mm:ss (does not work)

echo "Counting to $1"
secs=$(time2seconds $1)
time_func $secs



enter image description here



I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:



sleep 5h 22m 44s


I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:



stopwatch 5h 22m 44s


How would I go about accomplishing this?



p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.










share|improve this question
























  • Can you convert that picture of text, to text.

    – ctrl-alt-delor
    2 days ago











  • Thanks for contributing! But please, don't post images of text (read this for reasons why).

    – Kusalananda
    2 days ago













2












2








2


1






Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.



function time_seconds()

echo "Counting to $1 seconds"
time_func $1


function time_minutes()

echo "Counting to $1 minutes"
time_func $1*60


function time_hours()

echo "Counting to $1 hours"
time_func $1*60*60


function time_flexible() # accepts flexible input hh:mm:ss (does not work)

echo "Counting to $1"
secs=$(time2seconds $1)
time_func $secs



enter image description here



I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:



sleep 5h 22m 44s


I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:



stopwatch 5h 22m 44s


How would I go about accomplishing this?



p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.










share|improve this question
















Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.



function time_seconds()

echo "Counting to $1 seconds"
time_func $1


function time_minutes()

echo "Counting to $1 minutes"
time_func $1*60


function time_hours()

echo "Counting to $1 hours"
time_func $1*60*60


function time_flexible() # accepts flexible input hh:mm:ss (does not work)

echo "Counting to $1"
secs=$(time2seconds $1)
time_func $secs



enter image description here



I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:



sleep 5h 22m 44s


I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:



stopwatch 5h 22m 44s


How would I go about accomplishing this?



p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.







bash shell-script shell command-line scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

41.8k1483142




41.8k1483142










asked 2 days ago









Anonymous UserAnonymous User

436




436












  • Can you convert that picture of text, to text.

    – ctrl-alt-delor
    2 days ago











  • Thanks for contributing! But please, don't post images of text (read this for reasons why).

    – Kusalananda
    2 days ago

















  • Can you convert that picture of text, to text.

    – ctrl-alt-delor
    2 days ago











  • Thanks for contributing! But please, don't post images of text (read this for reasons why).

    – Kusalananda
    2 days ago
















Can you convert that picture of text, to text.

– ctrl-alt-delor
2 days ago





Can you convert that picture of text, to text.

– ctrl-alt-delor
2 days ago













Thanks for contributing! But please, don't post images of text (read this for reasons why).

– Kusalananda
2 days ago





Thanks for contributing! But please, don't post images of text (read this for reasons why).

– Kusalananda
2 days ago










1 Answer
1






active

oldest

votes


















3














Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):



to_seconds () 
seconds=0

for ts do
case $ts in
*h) seconds=$(( seconds + 60*60*"$ts%h" )) ;;
*m) seconds=$(( seconds + 60*"$ts%m" )) ;;
*) seconds=$(( seconds + "$ts%s" )) ;;
esac
done

printf '%sn' "$seconds"



The loop in the function will loop over the given arguments.



The $ts%h parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.



If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.



You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.



Testing:



$ to_seconds 5h 22m 44s
19364
$ to_seconds 5h 22m 44
19364
$ to_seconds 44
44
$ to_seconds 10
10
$ to_seconds 10m
600
$ to_seconds 10h
36000
$ to_seconds 2i
bash: seconds + 2i: value too great for base (error token is "2i")


To verify that a given number is an integer, see



  • Checking if an input number is an integer





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%2f509712%2fhow-to-modify-these-bash-functions-into-one%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














    Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):



    to_seconds () 
    seconds=0

    for ts do
    case $ts in
    *h) seconds=$(( seconds + 60*60*"$ts%h" )) ;;
    *m) seconds=$(( seconds + 60*"$ts%m" )) ;;
    *) seconds=$(( seconds + "$ts%s" )) ;;
    esac
    done

    printf '%sn' "$seconds"



    The loop in the function will loop over the given arguments.



    The $ts%h parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.



    If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.



    You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.



    Testing:



    $ to_seconds 5h 22m 44s
    19364
    $ to_seconds 5h 22m 44
    19364
    $ to_seconds 44
    44
    $ to_seconds 10
    10
    $ to_seconds 10m
    600
    $ to_seconds 10h
    36000
    $ to_seconds 2i
    bash: seconds + 2i: value too great for base (error token is "2i")


    To verify that a given number is an integer, see



    • Checking if an input number is an integer





    share|improve this answer





























      3














      Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):



      to_seconds () 
      seconds=0

      for ts do
      case $ts in
      *h) seconds=$(( seconds + 60*60*"$ts%h" )) ;;
      *m) seconds=$(( seconds + 60*"$ts%m" )) ;;
      *) seconds=$(( seconds + "$ts%s" )) ;;
      esac
      done

      printf '%sn' "$seconds"



      The loop in the function will loop over the given arguments.



      The $ts%h parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.



      If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.



      You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.



      Testing:



      $ to_seconds 5h 22m 44s
      19364
      $ to_seconds 5h 22m 44
      19364
      $ to_seconds 44
      44
      $ to_seconds 10
      10
      $ to_seconds 10m
      600
      $ to_seconds 10h
      36000
      $ to_seconds 2i
      bash: seconds + 2i: value too great for base (error token is "2i")


      To verify that a given number is an integer, see



      • Checking if an input number is an integer





      share|improve this answer



























        3












        3








        3







        Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):



        to_seconds () 
        seconds=0

        for ts do
        case $ts in
        *h) seconds=$(( seconds + 60*60*"$ts%h" )) ;;
        *m) seconds=$(( seconds + 60*"$ts%m" )) ;;
        *) seconds=$(( seconds + "$ts%s" )) ;;
        esac
        done

        printf '%sn' "$seconds"



        The loop in the function will loop over the given arguments.



        The $ts%h parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.



        If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.



        You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.



        Testing:



        $ to_seconds 5h 22m 44s
        19364
        $ to_seconds 5h 22m 44
        19364
        $ to_seconds 44
        44
        $ to_seconds 10
        10
        $ to_seconds 10m
        600
        $ to_seconds 10h
        36000
        $ to_seconds 2i
        bash: seconds + 2i: value too great for base (error token is "2i")


        To verify that a given number is an integer, see



        • Checking if an input number is an integer





        share|improve this answer















        Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):



        to_seconds () 
        seconds=0

        for ts do
        case $ts in
        *h) seconds=$(( seconds + 60*60*"$ts%h" )) ;;
        *m) seconds=$(( seconds + 60*"$ts%m" )) ;;
        *) seconds=$(( seconds + "$ts%s" )) ;;
        esac
        done

        printf '%sn' "$seconds"



        The loop in the function will loop over the given arguments.



        The $ts%h parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.



        If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.



        You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.



        Testing:



        $ to_seconds 5h 22m 44s
        19364
        $ to_seconds 5h 22m 44
        19364
        $ to_seconds 44
        44
        $ to_seconds 10
        10
        $ to_seconds 10m
        600
        $ to_seconds 10h
        36000
        $ to_seconds 2i
        bash: seconds + 2i: value too great for base (error token is "2i")


        To verify that a given number is an integer, see



        • Checking if an input number is an integer






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        KusalanandaKusalananda

        139k17259430




        139k17259430



























            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%2f509712%2fhow-to-modify-these-bash-functions-into-one%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

            Sum ergo cogito? 1 nng

            419 nièngy_Soadمي 19bal1.5o_g

            Queiggey Chernihivv 9NnOo i Zw X QqKk LpB