我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
#!/usr/bin/awk -f
BEGIN {
OFS = "="
NF = 100
print
}
Or
#!/usr/bin/awk -f
BEGIN {
while (z++ < 100) printf "="
}
例子
其他回答
最简单的方法是在csh/tcsh中使用这一行代码:
printf "%50s\n" '' | tr '[:blank:]' '[=]'
没有简单的方法。但是举个例子:
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),它们似乎不支持它:)
printf -- '=%.0s' {1..100}
双破折号——表示“命令行标志的结束”,所以不要试图解析命令行选项后面的内容。
如果你想打印破折号字符,而不是=字符,多次,不包括双破折号-这是你会得到的:
$ printf '-%.0s' {1..100}
bash: printf: -%: invalid option
printf: usage: printf [-v var] format [arguments]
为什么不创建这样的一行函数呢:
function repeat() { num="${2:-100}"; printf -- "$1%.0s" $(seq 1 $num); }
然后,你可以这样调用它:
$ repeat -
----------------------------------------------------------------------------------------------------
或者像这样:
$ repeat =
====================================================================================================
或者像这样:
$ repeat '*' 8
********
n=5; chr='x'; chr_string='';
for (( i=0; $i<$n; i++ ))
do
chr_string=$chr_string$chr
done
echo -n "$chr_string"
适用于…… N =整数(包括0和负数)。 Chr =可打印和空白(空格和制表符)。
我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在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}
}