17 October 2008

seq Unix Utility for MacOS X

Today I needed the seq Unix utility and I discovered that MacOS X does not have it. Neither does MacPorts :-(. So I Googled for it and found that scripting god Dave Taylor (who lives in Boulder) has a blog post about this. He provides a shell script that produces basic seq functionality and it works great. Now I'm able to write quick little one-liners like this:

for i in $(seq 1 10); do cp ~/tmp/cheese.xml ~/tmp/cheese$i.xml; done

Thanks, Dave!

11 comments:

  1. Hi Bruce. Actually on BSD style Mac OS systems, the seq command is actually known as "jot". For example, with seq you would do:

    for i in `seq 20 40`; do ping 192.168.1.${i}; done

    Where on BSD OS' you do:

    for i in `jot 21 20`; do ping 192.168.1.${i}; done

    Same thing, differeent name. ;-)

    ReplyDelete
  2. Thanks, Jeff! I have never heard of jot.

    ReplyDelete
  3. Um - got bash?

    for i in {1..9}; do echo $i; done

    ReplyDelete
  4. That was the first thing I tried and now I see I should have used the braces instead of parens :-/. Thanks!

    ReplyDelete
  5. @dc2447, that doesn't seem to work when using variables:

    $ upper=10

    $ for i in {1..$upper}; do echo $i; done
    {1..10}

    $ for i in {1..${upper}}; do echo $i; done
    {1..10}


    Thus my Google search led me to this post and now I know about jot -- thanks Jeff!

    ReplyDelete
  6. If you need to make this cross platform neither jot nor seq will work and bash will be best to use. If you throw in an eval I can get this to work with a variable.

    $ for i in `eval echo {1..$upper}`; do echo $i; done

    ReplyDelete
  7. jot is a great native alternative, and the {1..12} range thing is nice, but if you are way too lazy, or unwilling to hack up old scripts that assume seq or some other gnu utility will be there, you could do:

    1. install coreutils
    2. make a temp file or somehow generate a list of the coreutils installed files, and use the list to create symbolic links in your path without the g prefix. I used port -d install coreutils, and just copied a bit of the debug output, but I'm sure there is a better way
    3. profit?

    [566][csuttles.killface: /Users/csuttles/Work/tmp]$ cat /tmp/list.txt
    DEBUG: Adding file to file_map: /opt/local/bin/gbase64 for: coreutils
    DEBUG: Adding file to file_map: /opt/local/bin/gbasename for: coreutils
    DEBUG: Adding file to file_map: /opt/local/bin/gcat for: coreutils
    DEBUG: Adding file to file_map: /opt/local/bin/gchcon for: coreutils
    ...


    create the links:

    for i in `awk '{print $6}' /tmp/list.txt ` ; do echo $i ; ln -s $i `echo $i | sed -e 's/bin\/g/bin\//'`; done


    check your links:

    for i in `awk '{print $6}' /tmp/list.txt ` ; do echo $i ; ls -l $i ; ls -l `echo $i | sed -e 's/bin\/g/bin\//'`; done

    killface:~ root# for i in `awk '{print $6}' /tmp/list.txt ` ; do echo $i ; ls -l $i ; ls -l `echo $i | sed -e 's/bin\/g/bin\//'`; done
    /opt/local/bin/gbase64
    -rwxr-xr-x 2 root admin 62112 Jan 26 22:44 /opt/local/bin/gbase64*
    lrwxr-xr-x 1 root admin 22 Jan 26 22:49 /opt/local/bin/base64@ -> /opt/local/bin/gbase64
    /opt/local/bin/gbasename
    -rwxr-xr-x 2 root admin 57544 Jan 26 22:44 /opt/local/bin/gbasename*
    lrwxr-xr-x 1 root admin 24 Jan 26 22:49 /opt/local/bin/basename@ -> /opt/local/bin/gbasename
    /opt/local/bin/gcat
    -rwxr-xr-x 2 root admin 80024 Jan 26 22:44 /opt/local/bin/gcat*
    lrwxr-xr-x 1 root admin 19 Jan 26 22:49 /opt/local/bin/cat@ -> /opt/local/bin/gcat
    ...

    ReplyDelete
  8. their is an easy way :

    sudo port install coreutils

    and in your .profile add the line :

    alias seq='gseq'

    voilĂ .

    ReplyDelete
    Replies
    1. BTW: aliases only work in the interactive shell. If you want to use seq in scripts, you'd need to e.g. symbolically link:

      ln `which gseq` ~/bin/seq

      (assuming ~/bin is in your $PATH)

      Delete
  9. you can also try:

    sudo brew install coreutils

    and alias seq='gseq'

    ReplyDelete
  10. This is a lifesaver. I've been using seq forever and this was really starting to bug me!

    ReplyDelete