参数在bash脚本中传递给for循环[重复]

时间:2022-11-28 15:40:29

This question already has an answer here:

这个问题在这里已有答案:

I am trying to pass the argument as max limit for the for loop like this:

我试图将参数作为for循环的最大限制传递,如下所示:

#!/bin/bash

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

This however returns {1..2} when called with argument 2, instead of executing the script and giving me

但是,当使用参数2调用时,这将返回{1..2},而不是执行脚本并给予我

1
2

4 个解决方案

#1


32  

Variable substitutions are not done inside of curly braces. You can use fixed numbers but not variables.

变量替换不是在花括号内完成的。您可以使用固定数字而不是变量。

Brace Expansion

A sequence expression takes the form {x..y}, where x and y are either integers or single characters. ...

序列表达式采用{x..y}形式,其中x和y是整数或单个字符。 ...

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

在任何其他扩展之前执行大括号扩展,并且在结果中保留对其他扩展特殊的任何字符。这是严格的文字。 Bash不对扩展的上下文或大括号之间的文本应用任何语法解释。

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly formed brace expansion is left unchanged.

正确形成的大括号扩展必须包含不带引号的开括号和右大括号,以及至少一个不带引号的逗号或有效的序列表达式。任何不正确形成的支撑扩展都保持不变。

Try one of these alternatives:

尝试以下替代方案之一:

for ((i = 1; i <= $1; i++)); do
    echo $i
done

# Not recommended with large sequences.
for i in $(seq 1 $1); do
    echo $i
done

#2


7  

This will cycle through all true arguments (a.k.a. "testo mesto" is one argument)

这将遍历所有真正的参数(a.k.a。“testo mesto”是一个参数)

#cycle through all args
for (( i=1; i<=$#; i++ )); do
    eval arg=\$$i
    echo "$arg"
done

OR

要么

#cycle through all args
for (( i=1; i<=$#; i++ )); do
    echo "${!i}"
done

#3


3  

...or in the unlikely event that you really just want sequential numbers:

......或者在极少数情况下你真的只想要序号:

seq $1

:-)

:-)

#4


2  

As well as John Kugelman's solution, you can use eval like this:

除了John Kugelman的解决方案,你可以像这样使用eval:

x=10; for i in $(eval echo {1..$x}); do echo $i; done

Or, if $1 is 10, then:

或者,如果1美元是10,那么:

set -- 10
for i in $(eval echo {1..$1})
do
    echo $i
done

You could also use some variants on:

您还可以使用以下变体:

set -- 1000
eval echo {1..$1} |
while read i
do
    echo $i
done

Or:

要么:

set -- 1000
while read i
do
     echo $i
done <(eval echo {1..$1})

That uses process substitution.

这使用过程替换。

#1


32  

Variable substitutions are not done inside of curly braces. You can use fixed numbers but not variables.

变量替换不是在花括号内完成的。您可以使用固定数字而不是变量。

Brace Expansion

A sequence expression takes the form {x..y}, where x and y are either integers or single characters. ...

序列表达式采用{x..y}形式,其中x和y是整数或单个字符。 ...

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

在任何其他扩展之前执行大括号扩展,并且在结果中保留对其他扩展特殊的任何字符。这是严格的文字。 Bash不对扩展的上下文或大括号之间的文本应用任何语法解释。

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly formed brace expansion is left unchanged.

正确形成的大括号扩展必须包含不带引号的开括号和右大括号,以及至少一个不带引号的逗号或有效的序列表达式。任何不正确形成的支撑扩展都保持不变。

Try one of these alternatives:

尝试以下替代方案之一:

for ((i = 1; i <= $1; i++)); do
    echo $i
done

# Not recommended with large sequences.
for i in $(seq 1 $1); do
    echo $i
done

#2


7  

This will cycle through all true arguments (a.k.a. "testo mesto" is one argument)

这将遍历所有真正的参数(a.k.a。“testo mesto”是一个参数)

#cycle through all args
for (( i=1; i<=$#; i++ )); do
    eval arg=\$$i
    echo "$arg"
done

OR

要么

#cycle through all args
for (( i=1; i<=$#; i++ )); do
    echo "${!i}"
done

#3


3  

...or in the unlikely event that you really just want sequential numbers:

......或者在极少数情况下你真的只想要序号:

seq $1

:-)

:-)

#4


2  

As well as John Kugelman's solution, you can use eval like this:

除了John Kugelman的解决方案,你可以像这样使用eval:

x=10; for i in $(eval echo {1..$x}); do echo $i; done

Or, if $1 is 10, then:

或者,如果1美元是10,那么:

set -- 10
for i in $(eval echo {1..$1})
do
    echo $i
done

You could also use some variants on:

您还可以使用以下变体:

set -- 1000
eval echo {1..$1} |
while read i
do
    echo $i
done

Or:

要么:

set -- 1000
while read i
do
     echo $i
done <(eval echo {1..$1})

That uses process substitution.

这使用过程替换。