Shell生成数字序列
转自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生成数字序列的更多相关文章
- shell 生成指定范围随机数与随机字符串 .
shell 生成指定范围随机数与随机字符串 分类: shell 2014-04-22 22:17 20902人阅读 评 ...
- 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 ...
- Pytorch基础——使用 RNN 生成简单序列
一.介绍 内容 使用 RNN 进行序列预测 今天我们就从一个基本的使用 RNN 生成简单序列的例子中,来窥探神经网络生成符号序列的秘密. 我们首先让神经网络模型学习形如 0^n 1^n 形式的上下文无 ...
- Matlab生成M序列的伪随机码
伪随机编码中较常用的是m序列,它是线性反馈移位寄存器序列的一种,其特点是在相同寄存器级数的情况下输出序列周期最长.线性反馈移位寄存器的工作原理是,给定所有寄存器一个初始值,当移位脉冲到来时,将最后一级 ...
- 找出数组中最长的连续数字序列(JavaScript实现)
原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: functi ...
- 九度OJ 1544 数字序列区间最小值
题目地址:http://ac.jobdu.com/problem.php?pid=1544 题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的 ...
- 【BZOJ】【1049】【HAOI2006】数字序列
DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...
- kaggle之数字序列预测
数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...
- Oracle和Mysql分别生成sequence序列
有时候在往数据库中插入数据的时候,如果ID值是32位的UUID, 而自己随便写个字符又不合适,这时就要用到函数来产生一个序列值 Oracle: select sys_guid() from dual; ...
随机推荐
- sap 调试工具,修改变量值
1: 点击修改,输入变量值,按enter键.
- 2019.04.09 电商24 订单模快 ORM
前面三个模块已近结束,现在看是订单模块的.想一下淘宝上的订单,在购物车中选中,提交,跳转到订单界面. 获取传过来的信息,那也要建立一个订单表,当我支付的时候,也要获取一些数据,将这些数据放到这个表中 ...
- JS--理解call、apply和bind
call.apply和bind call,apply是Function原型中的方法,它们的作用一样,区别在于传入参数的方式不同. call(thisArg, arg1, arg2...) 传入的参数不 ...
- cocos2d-x JS 利用重复动作实现动画播放(实现倒计时)
cocos2d-js: cc.delayTime() and cc.repeatForever() don't work together in cc.sequence() this.numm = 1 ...
- dubbo.provider和dubbo.consumer配置
Configure service provider <?xml version="1.0" encoding="UTF-8"?> <bean ...
- servlet 的servletContext
- c++ 各种类型转换
1.int 2 string 法1:c++11里面的to_string #include <string> std::); //or auto s = std::to_string(); ...
- ubuntu安装mysql,redis,python-mysqldb
sudo apt-get install mysql-server sudo apt-get install redis-server sudo apt-get install python-redi ...
- Keras 如何利用训练好的神经网络进行预测
分成两种情况,一种是公开的训练好的模型,下载后可以使用的,一类是自己训练的模型,需要保存下来,以备今后使用. 如果是第一种情况,则参考 http://keras-cn.readthedocs.i ...
- 【转】Requests 官方中文文档 - 快速上手
迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Requests ...