我有一个带有2个参数的bash脚本 - 一个字符和一个整数。我想在aplhabet中打印字符,从该整数的长度字符开始(以字母为模)
#!/bin/bash
[[ $# != 2 ]] && echo Exactly 2 argument needed && exit 1
[[ "$1" =~ ^[a-zA-Z]$ ]] || { echo Enter only one char; exit 2; }
[[ "$2" =~ ^[0-9]+$ ]] || { echo Enter integer; exit 3; }
letter="$1"
cnt="$2"
letter=`printf "%d" \'$letter`;
z=`printf "%d" \'z`
a=`printf "%d" \'a`
[[ "$1" =~ ^[[:upper:]]$ ]] && { ((letter+=32)); } # make lower
while [[ "$cnt" -gt 0 ]]; do
printf \\$(printf "%03o" "$letter")
((letter++))
[[ "$letter" -gt $z ]] && letter=$a # alphabet modulo
((cnt--))
done
echo ""
这个脚本运行得很好,但速度太慢了。我必须调用printf程序的2x n(int值)。
我的问题是,是否有类似enable printf
工作解决方案来提高printf执行速度或某些解决方案根本不使用printf。
我需要使用bash,我只想知道如何以最快的方式处理ASCII。我正在使用cygwin atm编写脚本,因此可能存在与之相关的速度失败。
这个脚本只是问题的一个例子,我不想要一些“增加stdout flush buffer size”的解决方案。谢谢:))
答案 0 :(得分:2)
你走了:
#!/bin/bash
[[ $# != 2 ]] && echo Exactly 2 argument needed && exit 1
[[ "$1" =~ ^[a-zA-Z]$ ]] || { echo Enter only one char; exit 2; }
[[ "$2" =~ ^[0-9]+$ ]] || { echo Enter integer; exit 3; }
alpha=`echo {a..z}|sed 's/ //g'`
letter="$1"
cnt="$2"
letter=$(expr `printf "%d - %d + 1" \'$letter \'a`;)
[[ "$1" =~ ^[[:upper:]]$ ]] && { ((letter+=32)); } # make lower
echo -n `echo $alpha |tail -c +$letter |head -c $cnt`
((cnt=cnt-26))
while [[ "$cnt" -gt 0 ]]; do
echo -n `echo $alpha |head -c $cnt`
((cnt=cnt-26))
done
echo ""
此程序使用大括号扩展将a..z
扩展到字符列表中,然后使用头部和尾部显示此字符串的大小。它甚至可以更加优化,但这对你来说是一种练习。
效果比较(<1>
是有问题的代码,<2>
是此答案中的代码):
*启动部分在<2>
中有4个命令,而<1>
中有3个命令
* <2>
每26个字符运行4个命令(echo(x2),head,tail),而<1>
中每个字符1个命令。
* <2>
每26个字符有一个算术运算,而<1>
每个字符有2个算术运算。
samveen@precise:/tmp$ time bash 1.sh a 1000
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
real 0m2.065s
user 0m0.244s
sys 0m0.552s
samveen@precise:/tmp$ time bash 2.sh a 1000
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
real 0m0.285s
user 0m0.024s
sys 0m0.060s
进一步阅读:man bash