用于 CLI 脚本的 PHP 与 BASH?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2785155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:04:32  来源:igfitidea点击:

PHP vs BASH for CLI scripting?

phpbashcommand-line-interface

提问by never_had_a_name

I have never used PHP with CLI, but I have seen scripts running with PHP code.

我从未在 CLI 中使用过 PHP,但我见过使用 PHP 代码运行的脚本。

I was wondering:

我想知道:

  • Why should we use BASH, when PHP is so popular and is able to run in CLI?
  • What are the pros and cons with each one?
  • Should I use PHP for all CLI scripting in the future?
  • 为什么我们要使用 BASH,当 PHP 如此流行并且能够在 CLI 中运行时?
  • 各有什么优缺点?
  • 将来我应该将 PHP 用于所有 CLI 脚本吗?

采纳答案by too much php

It is still much easier to perform many common tasks in bash. Consider writing PHP equivalents for the following bash one-liners:

在 bash 中执行许多常见任务仍然容易得多。考虑为以下 bash one-liners 编写 PHP 等价物:

# remove a folder full of rubbish
rm -rf oldTrashFolder

# create an archive of files containing the word 'localhost'
tar cf backup.tar $(grep -rlw localhost ~/my-source-code)

# make a copy of a mysql database
{ echo "create database NEWDATABASE; "; mysqldump OLDDATABASE; } | mysql NEWDATABASE

回答by Steve Robillard

Bash is more common for this type of application, simply by virtue of it being around forever. But when it comes to choosing one over the other. The language you already know is usually the better choice. Just because you will be more productive.

Bash 对于这种类型的应用程序更为常见,仅仅因为它永远存在。但是当谈到选择一个而不是另一个时。您已经知道的语言通常是更好的选择。只是因为你会更有效率。

回答by Andrew McGregor

I guess the main thing is: bash is always there on a recent unix system (its predecessors really are always there, even on ancient unixen). Bash uses every little utility on the system, which can include PHP, Python, Ruby, AWK, Perl (those last two especially).

我想主要的是:bash 在最近的 unix 系统上总是存在(它的前辈确实总是在那里,甚至在古老的 unixen 上)。Bash 使用系统上的每一个小实用程序,包括 PHP、Python、Ruby、AWK、Perl(尤其是最后两个)。

Consider: how do you get PHP installed? Bunch of bash scripts and a makefile, right? Or your OS package manager, which these days is probably Python, but used to be a bash script as well.

考虑:如何安装 PHP?一堆 bash 脚本和一个 makefile,对吧?或者你的操作系统包管理器,现在可能是 Python,但也曾经是一个 bash 脚本。

You can't administer unix without knowing the shell really well, nor can you write or use makefiles. It may not be the right answer every time, but for a scripting job I always try to figure out if it can be done in bash first.

如果不非常了解 shell,就不能管理 unix,也不能编写或使用 makefile。这可能不是每次都正确的答案,但对于脚本工作,我总是试图弄清楚它是否可以先在 bash 中完成。

回答by Davor Lucic

For general purpose task scripting i would suggest going Bash all the way.

对于通用任务脚本,我建议一直使用 Bash。

Bash is probably most widely spread shell and your script will be able to run on most *nix systems. By contrast PHP will not always be readily available. Of course if you don't intend to use those scripts over unfamiliar system you'll be fine with both.

Bash 可能是传播最广泛的 shell,您的脚本将能够在大多数 *nix 系统上运行。相比之下,PHP 并不总是随时可用。当然,如果您不打算在不熟悉的系统上使用这些脚本,那么两者都可以。

That said, most standard shell commands are easier to write in bash then in php. In php you would open a subprocesses, execute shell commands through some wrapper functions or what not while in bash you would simply record your bash session in a file an made it executable. Take listing of a directory for example, in bash you simply state lsand in php you'd probably be using scandirwhich isn't fun as it sounds. Also i would imagine scandir has lot less sorting options. grep -Ri something .anyone?

也就是说,大多数标准的 shell 命令在 bash 中比在 php 中更容易编写。在 php 中,您将打开一个子进程,通过一些包装函数或其他包装函数执行 shell 命令,而在 bash 中,您只需将 bash 会话记录在一个文件中并使其可执行。以目录列表为例,在 bash 中,您只需声明,ls而在 php 中,您可能会使用scandir,这听起来并不有趣。此外,我认为 scandir 的排序选项要少得多。grep -Ri something .任何人?

Most of the general purpose languages can be used for shell scripting, but that doesn't mean they should be and at the end of the day it comes down to getting the job done and a matter of subjective preference.

大多数通用语言都可用于 shell 脚本编写,但这并不意味着它们应该如此,归根结底,这归结为完成工作和主观偏好的问题。

回答by chiggsy

PHP is only better in the web directory. Bash is better suited for handling command line inputs and general shell semantics, IMO.

PHP 仅在 web 目录中更好。Bash 更适合处理命令行输入和通用外壳语义,IMO。

回答by zhongshu

I think python is better than php for CLI.

我认为 python 比 CLI 的 php 更好。

If you have never used PHP, I recommend Python, because PHP was originally designed for web development to produce dynamic web pages, and for Python, It's more generous and emphasize code readability.

如果你没用过 PHP,我推荐 Python,因为 PHP 最初是为 web 开发而设计的,用于生成动态网页,而对于 Python,它更加大方,强调代码可读性。

回答by dlamotte

Preference mainly.

偏好为主。

Higher level languages (like php) have better data structures easily accessible (among other things). However, it's pretty easy to hack together a working bash script.

高级语言(如 php)具有更好的数据结构,易于访问(除其他外)。但是,将一个可以工作的 bash 脚本组合在一起非常容易。

So really, it's up to you.

所以真的,这取决于你。

回答by Gerry

I was looking into this myself as I'm current doing a upgrade a on large LMS having come across this link and another which has some result to a proformance test I thought I would add this link to thispost. while it task is nota real world example it does show some interesting stats.

我自己正在研究这个问题,因为我目前正在对大型 LMS 进行升级,遇到了这个链接,另一个对性能测试有一些结果,我想我会把这个链接添加到这篇文章中。虽然它的任务不是真实世界的例子,但它确实显示了一些有趣的统计数据。