转自http://kodango.com/generate-number-sequence-in-shell

Shell里怎么输出指定的数字序列:

for i in {1..5}; do echo $i; done
可以输出
1
2
3
4
5 但是如果
END=5
for i in {1..$END}; do echo $i; done 就不灵了。。。
怎么才能通过变量传一个区间进来,让他输出数字序列? 查了下文档,知道为什么{1..$END}没有效果了,看GNU的bash手册是这么说的:

Brace expansion is erformed 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. To avoid conflicts with parameter expansion, the string ‘${’ is not considered eligible for brace expansion.

也就是说Brace expansion是在其它所有类型的展开之前处理的包括参数和变量展开,因此{1..$END}就是{1..$END},原封不动,不知道你能不能理解?或许你将{1..$END}看成{1..END}好了,因为这里$END只是一个字符串而已。

另外一方面,GNU的bash手册也说明了这个语法的使用方法:

A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive....When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive. Note that both x and y must be of the same type. When the increment is supplied, it is used as the difference between each term. The default increment is 1 or -1 as appropriate.

从上面可以看出{x..y}的限制条件是:1) 整数或者单个字符; 2)两者要是同一个类型。回到我们的例子,{1..$END}x=1, y=$END,前者是整数,后者是字符串,不符合以上任何一个条件,所以也就不展开了。

ABS文档里面也有例子介绍这个语法的使用方法,中间也提到这点。

$ for i in {1..$END}; do echo $i; done
{1..5}

从上面的结果可以看出,只是显示了{1..5}这个结果。如果要得到数值序列,有很多种方法。第一种是用seq start end这种形式来替换{start..end},如:

$ for i in `seq 1 $END`; do echo $i; done

当然如果你一定要用{start..end},可以用eval命令:

$ for i in `eval echo {1..$END}`; do echo $i; done

这里eval echo {1..$END}后的结果为{1..5}:

最直观的是用循环了:

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

当然,肯定有其它方法,只要你愿意去思考。

Shell生成数字序列的更多相关文章

  1. shell 生成指定范围随机数与随机字符串 .

    shell 生成指定范围随机数与随机字符串         分类:             shell              2014-04-22 22:17     20902人阅读     评 ...

  2. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...

  3. Pytorch基础——使用 RNN 生成简单序列

    一.介绍 内容 使用 RNN 进行序列预测 今天我们就从一个基本的使用 RNN 生成简单序列的例子中,来窥探神经网络生成符号序列的秘密. 我们首先让神经网络模型学习形如 0^n 1^n 形式的上下文无 ...

  4. Matlab生成M序列的伪随机码

    伪随机编码中较常用的是m序列,它是线性反馈移位寄存器序列的一种,其特点是在相同寄存器级数的情况下输出序列周期最长.线性反馈移位寄存器的工作原理是,给定所有寄存器一个初始值,当移位脉冲到来时,将最后一级 ...

  5. 找出数组中最长的连续数字序列(JavaScript实现)

    原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: functi ...

  6. 九度OJ 1544 数字序列区间最小值

    题目地址:http://ac.jobdu.com/problem.php?pid=1544 题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的 ...

  7. 【BZOJ】【1049】【HAOI2006】数字序列

    DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...

  8. kaggle之数字序列预测

    数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...

  9. Oracle和Mysql分别生成sequence序列

    有时候在往数据库中插入数据的时候,如果ID值是32位的UUID, 而自己随便写个字符又不合适,这时就要用到函数来产生一个序列值 Oracle: select sys_guid() from dual; ...

随机推荐

  1. Exception in thread “main” java.sql.SQLException: No suitable driver

    问题背景:通过Spark SQL的jdbc去读取Oracle数据做测试,在本地的idea中没有报任务错误.但是打包到集群的时候报: Exception in thread “main” java.sq ...

  2. centos 下python升级

    https://www.cnblogs.com/leon-zyl/p/8422699.html

  3. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. HttpwebRequest - 带ViewState的网页POST请求

    这是我今天下午碰到的案例,一个退订页面的post请求,请求头信息都很明确,but看看下面这个请求体,除了最后一个key是我的页面控件名称,其他的几个ViewState相关都是what呢?(ViewSt ...

  5. 删除docker下的镜像

    先显示一下docker中的镜像 删除镜像 先停止这个容器 删除容器 再删除镜像就可以啦!

  6. python SMTP other

    HTML 正文,带链接和图片 //test.py import smtplib from email.mime.image import MIMEImage from email.mime.text ...

  7. ida脚本函数

    #打印光标所在位置函数中地址和汇编代码 startaddr=GetFunctionAttr(ea, FUNCATTR_START) items = idautils.FuncItems(startad ...

  8. AsssetBunlder打包

    unity3d,资源过多的话.可以压缩成一个资源包.加载出来后.可以解压.找到自己需要的资源 就想.net网站.很多图标都是放一个大图片上.而不是一个图标就是一个图片 因为是在项目编辑时候给资源打包. ...

  9. ASP.Net Core 2.2 MVC入门到基本使用系列 (二)(转)

    本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...

  10. sqli-labs(五)——盲注(boolean盲注以及时间盲注)

    第八关: 没有查询信息,输入id=1' 报错 ,也没有报错信息,这里应该是个盲注 使用boolean的盲注吧 先判断boolean的盲注可行 输入id=1' and '1'='1' %23 页面正常 ...