我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在3秒内完成大约1000万。
repeatString(){
# argument 1: The string to print
# argument 2: The number of times to print
stringToPrint=$1
length=$2
# Find the largest integer value of x in 2^x=(number of times to repeat) using logarithms
power=`echo "l(${length})/l(2)" | bc -l`
power=`echo "scale=0; ${power}/1" | bc`
# Get the difference between the length and 2^x
diff=`echo "${length} - 2^${power}" | bc`
# Double the string length to the power of x
for i in `seq "${power}"`; do
stringToPrint="${stringToPrint}${stringToPrint}"
done
#Since we know that the string is now at least bigger than half the total, grab however many more we need and add it to the string.
stringToPrint="${stringToPrint}${stringToPrint:0:${diff}}"
echo ${stringToPrint}
}
其他回答
没有简单的方法。但是举个例子:
seq -s= 100|tr -d '[:digit:]'
# Editor's note: This requires BSD seq, and breaks with GNU seq (see comments)
或者是一种符合标准的方式:
printf %100s |tr " " "="
还有一个tput代表,但对于我手头的终端(xterm和linux),它们似乎不支持它:)
下面是我在linux中用来在屏幕上打印一行字符的方法(基于终端/屏幕宽度)
在屏幕上输入“=”:
printf '=%.0s' $(seq 1 $(tput cols))
解释:
打印等号的次数与给定序列相同:
printf '=%.0s' #sequence
使用命令的输出(这是bash的一个叫做命令替换的特性):
$(example_command)
给出一个序列,我以1到20为例。在最后一个命令中,使用tput命令代替20:
seq 1 20
给出终端中当前使用的列数:
tput cols
Python无处不在,在任何地方都能正常工作。
import sys;Print ('*' * int(sys.argv[1]))"" = " 100
Character和count作为单独的参数传递。
稍微长一点的版本,但如果你出于某种原因必须使用纯Bash,你可以使用一个带增量变量的while循环:
n=0; while [ $n -lt 100 ]; do n=$((n+1)); echo -n '='; done
不是堆砌,而是另一种纯bash方法利用了数组的${//}替换:
$ arr=({1..100})
$ printf '%s' "${arr[@]/*/=}"
====================================================================================================