我怎么用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}
}
其他回答
如果你想在echo和printf的不同实现之间遵循posix并保持一致性,和/或shell而不仅仅是bash:
seq(){ n=$1; while [ $n -le $2 ]; do echo $n; n=$((n+1)); done ;} # If you don't have it.
echo $(for each in $(seq 1 100); do printf "="; done)
...将在所有地方产生与perl -E 'say "=" x 100'相同的输出。
我的建议(接受n的变量值):
n=100
seq 1 $n | xargs -I {} printf =
你可以使用:
printf '=%.0s' {1..100}
这是如何工作的:
Bash扩展{1..100},那么命令就变成:
printf '=%.0s' 1 2 3 4 ... 100
我已经将printf的格式设置为=%。这意味着无论给出什么参数,它总是打印一个=。因此它输出100 =s。
正如其他人所说,在bash中,大括号展开先于参数展开,因此{m,n}范围只能包含字面量。Seq和jot提供了干净的解决方案,但不能完全从一个系统移植到另一个系统,即使在每个系统上使用相同的shell。(尽管seq越来越多;例如,在FreeBSD 9.3和更高版本中。)eval和其他形式的间接方法总是有效的,但有些不优雅。
幸运的是,bash支持c风格的for循环(只支持算术表达式)。这里有一个简洁的“纯bash”方法:
repecho() { for ((i=0; i<$1; ++i)); do echo -n "$2"; done; echo; }
这将重复次数作为第一个参数,将要重复的字符串(如问题描述中所示,可以是单个字符)作为第二个参数。Repecho 7b输出BBBBBBB(以换行符结束)。
Dennis Williamson四年前在他关于在shell脚本中创建重复字符字符串的出色回答中给出了这个解决方案。我的函数体与代码略有不同:
Since the focus here is on repeating a single character and the shell is bash, it's probably safe to use echo instead of printf. And I read the problem description in this question as expressing a preference to print with echo. The above function definition works in bash and ksh93. Although printf is more portable (and should usually be used for this sort of thing), echo's syntax is arguably more readable. Some shells' echo builtins interpret - by itself as an option--even though the usual meaning of -, to use stdin for input, is nonsensical for echo. zsh does this. And there definitely exist echos that don't recognize -n, as it is not standard. (Many Bourne-style shells don't accept C-style for loops at all, thus their echo behavior needn't be considered..) Here the task is to print the sequence; there, it was to assign it to a variable.
如果$n是你想要的重复次数,你不需要重用它,你想要更短的东西:
while ((n--)); do echo -n "$s"; done; echo
N必须是一个变量——这种方法不适用于位置参数。$s是要重复的文本。
大多数现有的解决方案都依赖于{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