Generating sequential number lists in tcsh

2020-04-10 03:11发布

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条回答
女痞
2楼-- · 2020-04-10 03:46

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}
    
查看更多
登录 后发表回答