Generating sequential number lists in tcsh

我是研究僧i 提交于 2019-12-02 02:12:50

问题


I've been trying to find a workaround to defining lists of sequential numbers extensively in tcsh, ie. instead of doing:

i = ( 1 2 3 4 5 6 8 9 10 )

I would like to do something like this (knowing it doesn't work)

i = ( 1..10 )

This would be specially usefull in foreach loops (I know I can use while, just trying to look for an alternative).

Looking around I found this:

foreach $number (`seq 1 1 9`)
...
end

Found that here. They say it would generate a list of number starting with 1, with increments of 1 ending in 9.

I tried it, but it didn't work. Apparently seq isn't a command. Does it exist or is this plain wrong?

Any other ideas?


回答1:


seq certainly exists, but perhaps not on your system since it is not in the POSIX standard. I just noticed you have two errosr in your command. Does the following work?

foreach number ( `seq 1 9` )
    echo $number
end

Notice the omission of the dollar sign and the extra backticks around the seq command.

If that still doesn't work you could emulate seq with awk:

foreach number ( `awk 'BEGIN { for (i=1; i<=9; i++) print i; exit }'` )

Update

Two more alternatives:

  1. If your machine has no seq it might have jot (BSD/OSX):

    foreach number ( `jot 9` )
    

    I had never heard of jot before, but it looks like seq on steroids.

  2. Use bash with built-in brace expansion:

    for number in {1..9}
    


来源:https://stackoverflow.com/questions/3921604/generating-sequential-number-lists-in-tcsh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!