本文共 2086 字,大约阅读时间需要 6 分钟。
[haoren@IM-SJ01-Server01 gongsi]$ seq -f '201110%02g' 1 26
20111001
20111002
20111003
20111004
20111005
20111006
20111007
20111008
20111009
20111010
20111011
20111012
20111013
20111014
20111015
20111016
20111017
20111018
20111019
20111020
20111021
20111022
20111023
20111024
20111025
20111026
-s 指定分隔符,默认是换行 -w 等位补全,就是宽度相等,不足的前面补 0 -f 格式化输出,就是指定打印的格式 可以不指定起始数值,则默认为 1 另外,不用 seq 的话还可以这样: for i in {1..10};do echo $i;done 1 和 10 之间是两个半角的点 从1循环到100的两种方法 for x in `seq 1 100`;do echo $x;done for x in {1..100};do echo $x;done 输出1-100中,不包含数字7,且不能被7整除的数 seq 100 | grep -v "7" | awk '$0%7!=0{print}' seq -f"%3g" 9 11 9 10 11 % 后面指定数字的位数 默认是"%g", "%3g"那么数字位数不足部分是空格 sed -f"%03g" 9 11 这样的话数字位数不足部分是0 % 前面制定字符串 seq -f "str%03g" 9 11 str009 str010 str011 -w 指定输出数字同宽 不能和-f一起用 输出是同宽的-s 指定分隔符 默认是回车 seq -s" " -f"str%03g" 9 11 str009 str010 str011 要指定\t 做为分隔符号 seq -s"echo -e "\t"" 9 11指定\n\n作为分隔符号 seq -s " " 1 10 1 2 3 4 5 6 7 8 9 10 seq -s ", " 1 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
请用linux shell 写一段脚本,实现从1..1000中所有偶数的和值
通过while 循环得到需要的结果:
start=1;
total=0;
while [ $start -le 1000 ];do
[[ $(($start%2)) == 0 ]]&&total=$(($total+$start));
start=$(($start+1));
done;
echo $total;
[chengmo@centos5 ~]$ start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total;
250500
通过 for 循环得到结果:
start=0;
total=0;
for i in $(seq $start 2 1000); do
total=$(($total+$i));
done;
echo $total;
[chengmo@centos5 ~]$ start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total;
250500
比较性能: [chengmo@centos5 ~]$ time (start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total;) 250500 real 0m0.016s user 0m0.012s sys 0m0.003s [chengmo@centos5 ~]$ time (start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total;) 250500 real 0m0.073s user 0m0.069s sys 0m0.004s for i in ` seq -f '1704%02g' 1 17` ;do echo -n $i " " ;mysql -D JSDB -e "select count(*) from JIESUANTJ_$i;"|awk 'NR>1' ;done 转载地址:http://rcpab.baihongyu.com/