pid =`cat $ pidfile`或读取pid< $ pidfile? [英] pid=`cat $pidfile` or read pid <$pidfile?

查看:614
本文介绍了pid =`cat $ pidfile`或读取pid< $ pidfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多init.d脚本,并且:

I read a lot of init.d scripts and:

pid=`cat $pidfile`

台词使我难过.我不明白为什么人们不使用:

lines make me sad. I don't understand why people doesn't use:

read pid <$pidfile

最后一个示例使用符合POSIX的语法,并且不执行fork/exec来运行外部进程(cat).

Last sample uses POSIX compliant syntax and doesn't do fork/exec to run external process (cat).

最后一个解决方案还允许在第一个换行符之后跳过内容.

Last solution also allow skipping content after first newline.

使用read命令是否存在任何陷阱(尽管它会执行拆分为多个字段的操作)?

Are there any traps with read command (despite that it perform splitting into fields)?

更新.某些peole对外壳使用非便携式扩展,例如:

UPDATE. Some peole use non-portable extension for shell like:

如何将文本文件中的变量转换为Bash变量

pid=$(<$pidfile)

推荐答案

read pid < file方法是最佳实践,原因如下:比cat的fork/exec便宜得多

The read pid < file way is the Best Practice for the reason you stated: much cheaper than a fork/exec of cat.

至于为什么这么多脚本以昂贵的方式执行此操作,我只能推测.可能会从其他人的脚本中剪切掉'n'paste,再加上对外壳功能的了解不足,再加上速度飞快的CPU.堆栈溢出时,谁会阅读手册页? :-)由于引入了所有术语,特别是对于新手来说,shell手册页是很难阅读的参考手册.

As for why so many scripts do this the expensive way, I can only speculate. Probably cut'n'paste from other people's scripts, together with lack of knowledge of shell features, together with blazingly fast CPUs. Who reads man pages when there's Stack Overflow? :-) Especially the shell man page is a hard-to-read reference manual for novices due to all the terminology introduced.

谁说猫的无用使用是管道的特权?

这篇关于pid =`cat $ pidfile`或读取pid&lt; $ pidfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆