ITeye博客

Shell

共 7 篇文章

[learn]kerl示范Bash脚本的编写

kerl是一个类似rvm的工具脚本,是下载管理erlang版本用的。 示范一些Shell脚本编写的规范,转过来学习一下。 https://github.com/spawngrid/kerl #! /bin/sh # Copyright (c) 2011, 2012 Spawngrid, Inc # Copyright (c) 2011 Evax Software <contact(at)evax(dot)org> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this…

2013-11-07 16:50 · Shell

寻找1729 by awk

记得拉马努金关于1729的故事吗? 小脚本算出1729 #/bin/bash echo | awk ' { input = 0 } END { while(check_sum(input) < 2) { input++ } print input } function check_sum(argi) { r = 0 for (i = 1; i < int(argi/2); i++) if ((check_cube(i) == 1) && (check_cube(argi - i) == 1) && (argi != i*2)) { r++ } return r } function chec…

2013-10-30 10:25 · Shell

awk版堆排序 heapsort

The AWK Programming Language书里 7.1节的heapsort段落 #/bin/bash echo | awk ' { A[nr] = $0 } END { hsort(A, NR) for (i = 1; i <= NR; i++) { print A[i] } } function hsort(A, n, i) { for (i = int(n/2); i >= 1; i--) { heapify(A, i, n) } for (i = n; i > 1; i--) { { swap(A, 1, i) } { heapify(A, 1, i-1) } } } f…

2013-10-29 10:56 · Shell

最大公约数的bash脚本

#!/usr/bin/bash gcd() { until test 0 -eq "$2"; do set -- "$2" "`expr "$1" % "$2"`" done test 0 -gt "$1" && set -- "`expr 0 - "$1"`" echo "$1" } gcd $1 $2 此处的set命令用--的参数将$2和'expr $1 % %2'的值交换,比较简洁。

2013-02-15 21:37 · Shell

分解质因数小脚本

awk语言的版本,如下: { x = $0; } function foo(a) { for (i = 2; i <= a; i++) if (a%i == 0) {printf("%d ",i);return i;} } END { c = foo(x); while (c < x) { x = x / c; c = foo(x); } } # usage(): $echo 2520 | awk -f script.awk 下边这个是一样思路的Bash版本,不过运行的非常慢。 #!/usr/bin/bash #usage(): $sh script.sh 2520 function foo…

2013-02-12 20:24 · Shell · python

为什么大家不愿意读正则表达式...

尝试用shell命令做一些批量的重命名,做如下的操作: mv [六人行.第三季][friends.s03e01][圣城家园dream2008.cn][中英双字幕].mp4 \ Friends.S03E01.mp4 想了一下,拼凑出下列命令,试了一下,可用。 ls | sed 's/\(\[.*\[f\)\([^]]*\)\(\].*\]\)\(.*\)/mv \1\2\3\4 F\2\4/'| sh ls | awk -F '[][]' '{printf "mv \""$0 "\" "$4$NF}'|sh ls | awk -F '[][]' \ '{for(i=1;i<=NF;i++)if…

2013-02-06 16:39 · Shell

Shell随机生成几个不重复的随机数

模拟体育彩票,36选7。 要求:随机,7个不重复,1到36这些整数。 #!/bin/bash touch ./temp >temp while [ `sort temp | uniq | wc -l` != 7 ] do echo `expr $RANDOM % 36 + 1` >> temp done sort temp | uniq rm temp just for fun... 练习一下shell编程。

2013-02-04 23:48 · Shell