如何限制重定向到文件的标准输出字符数量?
其他方式(外部)
echo $out| head -c 20
echo $out | awk '{print substr($0,1,20) }'
echo $out | ruby -e 'print $_[0,19]'
echo $out | sed -r 's/(^.{20})(.*)/\1/'
#!/bin/bash
limit=20
out=$(echo "this line has more than twenty characters in it")
echo ${out::limit} > /path/to/file
$ limit=20
$ out=$(echo "this line has more than twenty characters in it").
$ echo ${out::limit}
this line has more t
你不能直接将其写入文件,但是你可以通过 sed
或者 head
等管道传递部分输出。或者像 @SiegeX 所说,在 shell 中捕获输出(但如果输出可能很大,我会谨慎对待)。
bash
而是任何符合 POSIX 标准的 shell 中最被低估/未知的功能。我一次又一次地看到人们调用basename
或dirname
,而这两个命令都可以使用参数扩展的本机 shell 语法来完成。请参见此处以获取有关用法的良好链接,以及有关 bash 的绝佳站点。 - SiegeX