加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

参数在bash脚本中传递给for循环

发布时间:2020-12-15 19:04:44 所属栏目:安全 来源:网络整理
导读:参见英文答案 Variables in bash seq replacement ({1..10}) 7个 我试图将参数作为for循环的最大限制传递,如下所示: #!/bin/bashfor i in {1..$1}do echo $idone 但是,当使用参数2调用时,这将返回{1..2},而不是执行脚本并给予我 12 变量替换不是在花括号内
参见英文答案 > Variables in bash seq replacement ({1..10}) 7个
我试图将参数作为for循环的最大限制传递,如下所示:
#!/bin/bash

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

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

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

Brace Expansion

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

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.

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.

尝试以下替代方案之一:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读