For loop with an argument based range

前端 未结 3 1780
感情败类
感情败类 2021-01-23 09:33

I want to run certain actions on a group of lexicographically named files (01-09 before 10). I have to use a rather old version of FreeBSD (7.3), so I can\'t use yummies like

3条回答
  •  后悔当初
    2021-01-23 09:56

    Bash supports C-style for loops:

    s=1
    e=30
    for i in ((i=s; i

    The syntax you attempted doesn't work because brace expansion happens before parameter expansion, so when the shell tries to expand {$1..$2}, it's still literally {$1..$2}, not {1..30}.


    The answer given by @Kent works because eval goes back to the beginning of the parsing process. I tend to suggest avoiding making habitual use of it, as eval can introduce hard-to-recognize bugs -- if your command were whitelisted to be run by sudo and $1 were, say, '$(rm -rf /; echo 1)', the C-style-for-loop example would safely fail, and the eval example... not so much.

    Granted, 95% of the scripts you write may not be accessible to folks executing privilege escalation attacks, but the remaining 5% can really ruin one's day; following good practices 100% of the time avoids being in sloppy habits.


    Thus, if one really wants to pass a range of numbers to a single command, the safe thing is to collect them in an array:

    a=( )
    for i in ((i=s; i

提交回复
热议问题