我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
如果你想重复一个字符n次,n是一个变量的次数,这取决于,比如说,字符串的长度,你可以这样做:
#!/bin/bash
vari='AB'
n=$(expr 10 - length $vari)
echo 'vari equals.............................: '$vari
echo 'Up to 10 positions I must fill with.....: '$n' equal signs'
echo $vari$(perl -E 'say "=" x '$n)
它显示:
vari equals.............................: AB
Up to 10 positions I must fill with.....: 8 equal signs
AB========
其他回答
#!/usr/bin/awk -f
BEGIN {
OFS = "="
NF = 100
print
}
Or
#!/usr/bin/awk -f
BEGIN {
while (z++ < 100) printf "="
}
例子
我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在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),它们似乎不支持它:)
大多数现有的解决方案都依赖于{1..shell的10}语法支持,这是bash和zsh特定的,并且不能在tcsh或OpenBSD的ksh和大多数非bash sh中工作。
以下代码适用于OS X和所有*BSD系统;实际上,它可以用来生成各种类型装饰空间的整体矩阵:
$ printf '=%.0s' `jot 64` | fold -16
================
================
================
================$
遗憾的是,我们没有得到一个尾随换行符;这可以通过在折叠后额外的printf '\n'来修复:
$ printf "=%.0s" `jot 64` | fold -16 ; printf "\n"
================
================
================
================
$
引用:
http://mdoc.su/-/printf.1 http://mdoc.su/-/jot.1 http://mdoc.su/-/fold.1
我刚刚发现了一个非常简单的方法来做到这一点使用seq:
更新:此功能适用于OS x附带的BSD序列。YMMV与其他版本
seq -f "#" -s '' 10
将打印'#' 10次,如下所示:
##########
-f "#"设置格式字符串忽略数字,只输出#。 -s "将分隔符设置为空字符串,以删除seq在每个数字之间插入的换行符 -f和-s后面的空格似乎很重要。
编辑:这里是一个方便的功能…
repeat () {
seq -f $1 -s '' $2; echo
}
你可以这样叫它…
repeat "#" 10
注意:如果你重复使用#,那么引号就很重要!