Saturday, January 29, 2011

How do I install 'repeat' on Ubuntu?

This StackOverflow question mentions a unix command called 'repeat'. It sounds like it does exactly what I want. From reading the question and answers, I think the user is on Mac OSX.

However that command is not installed by default on Ubuntu, and I can't find the package to install to get it. What should I install?

  • From the prompt, I'd guess it's a csh builtin.

    And from reading "man csh", that appears to be the case

      repeat count command
               The specified command, which is subject to  the  same  restric-
               tions  as  the  command  in the one line if statement above, is
               executed count times.  I/O  redirections  occur  exactly  once,
               even if count is 0.
    

    So in order to use it, either type "csh" and issue it from the command line, or write your script so that it uses #!/bin/csh as the interpreter at the top. Here are some csh basics to get you started.

    kmarsh : Better yet- don't get started on an obsolete, incompatible shell. Learn real shell programming and write yourself a repeat alias or function in Bash, a (mostly) Posix standard shell.
    Matt Simmons : Eh. I'm a bash guy, but csh doesn't bother me. I know a *lot* of people that would say the exact same thing as you, except change csh to bash and bash to korn. There's a lot of truth to the fact that korn is more advanced than bash. It's all what you're comfortable with and what gets the job done. csh is going to be around for a long, long time
    Joseph Kern : Or you could change csh to bash and bash to zsh.
    Raphink : While quite a few people have their favorite shell, I find that most sysadmins know bash, while there's few that know csh, zsh and others, and since Ubuntu comes with bash by default (for the users at least, it has dash for root), it's still nicer to play with bash when possible. That's just my opinion though.
  • I can't find this command on Ubuntu. It doesn't seem to exist. I even find it very weird that the post on StackOverflow says it's a builtin command when I can't find it on Ubuntu.

    Edit: Like Matt noted, it is a builtin csh command. The following are tips to do quite the same with bash.

    If what you want is to repeat a command n times, you can do that with a loop though:

    for i in {1..n}; do yourcommand; done
    

    For example, to print 100 times "It works", use:

    for i in {1..100}; do echo "It works"; done
    

    If you want to have a repeat function, you could add something like this to your ~/.bashrc:

    function repeat() { 
        local times="$1"; 
        shift; 
        local cmd="$@"; 
    
        for ((i = 1; i <= $times; i++ )); do 
           eval "$cmd"; 
        done 
     }
    

    Source your ~/.bashrc again with . ~/.bashrc and you can call it:

     $ repeat 2 date
    Mon Dec 21 14:25:50 CET 2009
    Mon Dec 21 14:25:50 CET 2009
    
     $ repeat 3 echo "my name is $USER"
    my name is raphink
    my name is raphink
    my name is raphink
    
    Matt Simmons : It's a "shell builtin", which means it's sort of like "echo" in that although there is a /bin/echo, if you just type "echo", it doesn't get executed. bash (or whatever your shell is) has an "echo" command that it runs instead, which prevents the system from having to launch another process.
    Matt Simmons : Although your way works as well
    Raphink : Yes Matt, I read your comment thanks. However, it's not a builtin in bash. The command doesn't exist when I use bash.
    Dennis Williamson : You can avoid calling the external `seq` by using `for ((i = 1; i <= $times; i++ ))`
    Raphink : Yes, that's probably more efficient Dennis, although I find the `seq` syntax more readable somehow.
    Dennis Williamson : You should note that in your "my name is" example, `$USER` is evaluated before the function is called and because of that, something that changes over time wouldn't be reflected during the repeated runs. In order to fix that, you'd have to do `eval "$cmd"` in your function instead of just `$cmd` and use single quotes around the argument to `repeat` to prevent early evaluation. From there, quoting issues just get hairier.
    Raphink : Nice suggestion Dennis. I'll fix my piece of code with this.
    From Raphink
  • You could use watch, which is a standard command available in any shell. For example:

    watch -n 5 date
    
    From Tobu

0 comments:

Post a Comment