加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 将stdout传递给Perl时,如何让它打印换行符?

发布时间:2020-12-16 01:49:12 所属栏目:安全 来源:网络整理
导读:我刚刚发现将stdout传输到Perl中,我很惊讶你甚至可以这样做: [user@folder] $echo print 1/3 | perl0.33333[user@folder] $ 根据我的理解,你将print命令放入Perl,并使用Perl的代码进行浮点计算.请指正.但是,每次我这样做,我都会得到一个没有换行符的答案.我
我刚刚发现将stdout传输到Perl中,我很惊讶你甚至可以这样做:

[user@folder] $echo print 1/3 | perl
0.33333[user@folder] $

根据我的理解,你将print命令放入Perl,并使用Perl的代码进行浮点计算.请指正.但是,每次我这样做,我都会得到一个没有换行符的答案.我到处搜索,我只是不能将这些术语的关键字足以创建答案.

这是我正在谈论的内容的链接,在海报Thor下:
How do I use floating-point division in bash?

他给出了一个惊人的答案,但我无法评论或给用户发消息,所以我决定在这里创建一个新问题.

我仍然试图围绕这是如何工作的,
变量也是如此.

$three=3
$echo $three/3 | perl
1[user@folder] $

奖金问题:
它起初就是我试图让bash在算术运算中输出浮点数.我不明白为什么bc无法返回浮动.据说它可以,但它对我不起作用.

理想的情况是:

$echo 1/3 | bc
0
$

应该返回.333而不是0.我正在委托一个工具,bc和bc应该能够做浮动.我只是不明白发生了什么.

解决方法

Perl的

使用-l选项(参见perldoc perlrun):

$echo print 1/3 | perl -l
0.333333333333333
$

它会自动添加换行符.文件说:

-l[octnum]

enables automatic line-ending processing. It has two separate effects. First,it automatically chomps $/ (the input record separator) when used with -n or -p. Second,it assigns $ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted,sets $ to the current value of $/. For instance,to trim lines to 80 columns:

06001

Note that the assignment $ = $/ is done when the switch is processed,so the input record separator can be different than the output record separator if the -l switch is followed by a -0 switch:

06002

This sets $ to newline and then sets $/ to the null character.

贝壳

shell扩展了shell变量,所以不应该对此感到惊讶:

$three=3
$echo print $three/3 | perl -l
1
$echo print $three/3
print 3/3
$

公元前

对于你的奖金问题:

$echo 1/3 | bc -l
.33333333333333333333
$

这是纯粹的侥幸,选项是-l.这一次,它意味着“加载库”,并巧合地将比例设置为20,这就是显示20个十进制数字的原因.

顺便说一句,快速获取π到大量小数位的方法是:

$echo '4*a(1)' | bc -l
3.14159265358979323844
$echo 'scale=40; 4*a(1)' | bc -l
3.1415926535897932384626433832795028841968
$

通过-l选项从库加载的函数a用于’arctan’.我观察到,谷歌搜索“pi 40位数”中至少有3个来源表明它应该是:3.1415926535 8979323846 2643383279 5028841971 – 这表明在bc的第40位数字中存在3个计数的错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读