Hướng dẫn is php a shell script? - php có phải là tập lệnh shell không?

Các tập lệnh Php Home / Running ASRunning PHP scripts as shell scripts

Cũng như chạy tập lệnh PHP qua máy chủ web như Apache, bạn cũng có thể chạy các tập lệnh PHP từ dòng lệnh (cũng có thể viết các ứng dụng GUI bằng PHP bằng PHP-GTK, nhưng có thể tôi sẽ nhìn vào lúc đó) . Bài đăng này xem xét hai cách bạn có thể chạy tập lệnh PHP từ máy tính dựa trên *NIX (Linux, UNIX, BSD, OSX) và cách duy nhất bạn có thể làm từ Windows.

Sử dụng Shebang

Các tập lệnh Unix Shell được đặt là có thể thực thi và dòng đầu tiên trong tệp chứa một dòng Shebang Shebang cho thấy đường dẫn đến thực thi sẽ phân tích tập lệnh.

Nếu nhị phân Php của bạn ở mức, ví dụ: /usr/bin/php Bạn sẽ thêm điều này vào dòng đầu tiên của tập lệnh của bạn:

#!/usr/bin/php

Và sau đó có tập lệnh PHP của bạn dưới nó.

Để chạy một kịch bản Hello Hello World, bạn đã làm điều này:

#!/usr/bin/php

Kịch bản sau đó cần được đánh dấu là thực thi. Để đặt nó để nó có thể được chạy như bất cứ ai làm điều này:

chmod 0755 /path/to/script.php

Để làm cho nó để chỉ có bạn mới có thể chạy nó (và do đó không ai khác có thể đọc nó) làm điều này:

chmod 0700 /path/to/script.php

Đọc trang phục của chmod để biết thêm chi tiết về quyền của tệp nếu bạn không biết về chúng. Tôi có một bản sao của đàn ông Chmod ở đây.

Gọi nhị phân PHP, chuyển tên tệp tập lệnh

Để tránh các vấn đề về quyền hoặc để chạy tập lệnh PHP từ dòng lệnh trên Windows (phương thức trên không hoạt động trên Windows trừ khi chạy qua Cygwin), bạn cần thực thi nhị phân PHP chuyển tên tệp tập lệnh làm tham số.

Nếu bạn nằm trong cùng thư mục với tập lệnh, tập lệnh được đặt tên là ví dụ: myscript.php và nhị phân PHP đang ở/usr/bin/php bạn sẽ làm điều này để chạy nó:

/usr/bin/php myscript.php

Nếu nhị phân PHP nằm trong đường dẫn thực thi của bạn (thường sẽ nằm trên hầu hết các bản phân phối Linux) thì bạn chỉ cần chạy php php và sau đó là tập lệnh:

php myscript.php

Nếu bạn đang chạy nó từ Cron, nó luôn luôn là một ý tưởng tốt để đưa con đường đầy đủ đến cả hai:

/usr/bin/php /path/to/myscript.php

Khi sử dụng phương pháp này, bạn không cần phải có dòng Shebang ở đầu kịch bản.

Một ví dụ về việc làm điều này trên Windows:

c:phpphp.exe c:pathtomyscript.php

Sự kết luận

Nó dễ dàng chạy tập lệnh PHP từ dòng lệnh, rất hữu ích cho việc xử lý hàng loạt hoặc dọn dẹp cơ sở dữ liệu cho trang web của bạn. Kết hợp với xử lý tự động thông qua hệ thống Cron và bạn có một công cụ mạnh mẽ theo ý của bạn nếu bạn là lập trình viên PHP và/hoặc trang web của bạn được lập trình trong PHP.

6 tháng trước

Martin tại Intelli-Gens Dot Com ¶Execute command via shell and return the complete output as a string

dburles@nospamgmaildotcom ¶

Gabekon tại Gmail Dot Com ¶(string $command): string|false|null

McBeth tại Broggs Dot org ¶:

19 năm trướcpopen() instead for such cases.

Ashraf Kaabi ¶

Martin Rampersad ¶

Khái niệm tại ConceptOnline Dot Hu ¶

Paul Cook ¶

Phillipberry tại Nospam Dot Blisswebhosting Dot Com ¶string containing the output from the executed command,

#!/usr/bin/php
0 if the pipe cannot be established or 
#!/usr/bin/php
1 if an error occurs or the command produces no output. 

McBeth tại Broggs Dot org ¶:

19 năm trước

#!/usr/bin/php
1 both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function.
exec() should be used when access to the program exit code is required. 

Errors/Exceptions

Ashraf Kaabi ¶

#!/usr/bin/php
3 level error is generated when the pipe cannot be established. 

Martin Rampersad ¶

Khái niệm tại ConceptOnline Dot Hu ¶shell_exec() example

#!/usr/bin/php
4

Paul Cook ¶

  • Phillipberry tại Nospam Dot Blisswebhosting Dot Com ¶
  • PHP [at] jsomers [dot] be ¶

Trev tại DEVATIATE.CO.uk ¶

11 năm trước

#!/usr/bin/php
5

#!/usr/bin/php
6

#!/usr/bin/php
7

#!/usr/bin/php
8

#!/usr/bin/php
9

smcbride tại msn dot com ¶

1 năm trước

chmod 0755 /path/to/script.php
0

chmod 0755 /path/to/script.php
1

chmod 0755 /path/to/script.php
2

#!/usr/bin/php
9

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

chmod 0755 /path/to/script.php
4

chmod 0755 /path/to/script.php
5

chmod 0755 /path/to/script.php
6

chmod 0755 /path/to/script.php
7

chmod 0755 /path/to/script.php
8

#!/usr/bin/php
9

Rgemini ¶

13 năm trước

chmod 0700 /path/to/script.php
0

chmod 0700 /path/to/script.php
1

chmod 0700 /path/to/script.php
2

chmod 0700 /path/to/script.php
3

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

chmod 0700 /path/to/script.php
5

chmod 0700 /path/to/script.php
6

chmod 0700 /path/to/script.php
7

chmod 0700 /path/to/script.php
8

chmod 0700 /path/to/script.php
9

/usr/bin/php myscript.php
0

/usr/bin/php myscript.php
1

/usr/bin/php myscript.php
2

chmod 0700 /path/to/script.php
9

/usr/bin/php myscript.php
4

/usr/bin/php myscript.php
5

/usr/bin/php myscript.php
6

/usr/bin/php myscript.php
7

/usr/bin/php myscript.php
8

/usr/bin/php myscript.php
9

#!/usr/bin/php
9

Tonysb tại gmx dot net ¶

20 năm trước

php myscript.php
1

php myscript.php
2

php myscript.php
3

php myscript.php
4

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

15 năm trước

php myscript.php
6

php myscript.php
7

php myscript.php
8

php myscript.php
9

/usr/bin/php /path/to/myscript.php
0

#!/usr/bin/php
9

Tonysb tại gmx dot net ¶

20 năm trước

/usr/bin/php /path/to/myscript.php
2

/usr/bin/php /path/to/myscript.php
3

/usr/bin/php /path/to/myscript.php
4

/usr/bin/php /path/to/myscript.php
5

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

13 năm trước

/usr/bin/php /path/to/myscript.php
7

/usr/bin/php /path/to/myscript.php
8

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

c:phpphp.exe c:pathtomyscript.php
0

c:phpphp.exe c:pathtomyscript.php
1

#!/usr/bin/php
9

Tonysb tại gmx dot net ¶

1 năm trước

c:phpphp.exe c:pathtomyscript.php
3

c:phpphp.exe c:pathtomyscript.php
4

c:phpphp.exe c:pathtomyscript.php
5

c:phpphp.exe c:pathtomyscript.php
6

c:phpphp.exe c:pathtomyscript.php
7

c:phpphp.exe c:pathtomyscript.php
8

#!/usr/bin/php
9

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

$command0

$command1

$command2

$command3

Rgemini ¶

13 năm trước

$command4

$command5

$command6

$command7

$command8

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

command0

command1

command2

command3

command4

Tonysb tại gmx dot net ¶

6 năm trước

command5

Rgemini ¶

13 năm trước

command6

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

command7

command8

command9

#!/usr/bin/php
00

#!/usr/bin/php
9

Tonysb tại gmx dot net ¶

20 năm trước

#!/usr/bin/php
02

#!/usr/bin/php
03

#!/usr/bin/php
04

Kamermans tại teratechnologys dot net ¶

pedroxam tại gmail dot com ¶

#!/usr/bin/php
05

#!/usr/bin/php
06

#!/usr/bin/php
9

3 năm trước

15 năm trước

#!/usr/bin/php
08

#!/usr/bin/php
09

#!/usr/bin/php
10

#!/usr/bin/php
11

#!/usr/bin/php
12

#!/usr/bin/php
13

#!/usr/bin/php
14

#!/usr/bin/php
15

#!/usr/bin/php
9

Tonysb tại gmx dot net ¶

13 năm trước

#!/usr/bin/php
17

#!/usr/bin/php
18

#!/usr/bin/php
19

#!/usr/bin/php
20

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

6 năm trước

#!/usr/bin/php
22

#!/usr/bin/php
23

command2

#!/usr/bin/php
25

#!/usr/bin/php
26

Rgemini ¶

13 năm trước

#!/usr/bin/php
27

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

20 năm trước

#!/usr/bin/php
28

#!/usr/bin/php
29

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

13 năm trước

#!/usr/bin/php
31

#!/usr/bin/php
32

#!/usr/bin/php
33

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

13 năm trước

#!/usr/bin/php
35

#!/usr/bin/php
36

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

20 năm trước

#!/usr/bin/php
38

#!/usr/bin/php
39

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

13 năm trước

#!/usr/bin/php
41

#!/usr/bin/php
42

#!/usr/bin/php
43

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

13 năm trước

#!/usr/bin/php
45

#!/usr/bin/php
46

#!/usr/bin/php
47

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

#!/usr/bin/php
48

Tonysb tại gmx dot net ¶

20 năm trước

#!/usr/bin/php
49

#!/usr/bin/php
50

#!/usr/bin/php
51

#!/usr/bin/php
52

#!/usr/bin/php
53

#!/usr/bin/php
54

#!/usr/bin/php
55

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

20 năm trước

#!/usr/bin/php
57

#!/usr/bin/php
58

#!/usr/bin/php
59

#!/usr/bin/php
9

Kamermans tại teratechnologys dot net ¶

13 năm trước

#!/usr/bin/php
61

#!/usr/bin/php
62

#!/usr/bin/php
9

Eric Dot Peyremorte tại IUT-Valence Dot Fr ¶

15 năm trước

#!/usr/bin/php
64

#!/usr/bin/php
65

#!/usr/bin/php
66

#!/usr/bin/php
67

#!/usr/bin/php
68

#!/usr/bin/php
69

#!/usr/bin/php
70

#!/usr/bin/php
71

#!/usr/bin/php
9

PHP và shell có giống nhau không?

Php Shell hoặc Shell PHP là một chương trình hoặc tập lệnh được viết bằng PHP (Bộ tiền xử lý siêu văn bản PHP) cung cấp thiết bị đầu cuối Linux (shell là một khái niệm rộng hơn nhiều) trong trình duyệt.PHP is a program or script written in PHP (Php Hypertext Preprocessor) which provides Linux Terminal (Shell is a much broader concept) in Browser.

PHP có phải là vỏ không?

Php Shell là một vỏ bọc trong tập lệnh PHP.Đó là một công cụ bạn có thể sử dụng để thực hiện các lệnh shell tùy ý hoặc duyệt hệ thống tập tin trên máy chủ web từ xa của bạn.Điều này thay thế, ở một mức độ, kết nối telnet bình thường và ở mức độ thấp hơn là kết nối SSH.a shell wrapped in a PHP script. It's a tool you can use to execute arbitrary shell-commands or browse the filesystem on your remote webserver. This replaces, to a degree, a normal telnet connection, and to a lesser degree a SSH connection.

PHP có thể chạy tập lệnh shell không?

Hàm shell_exec () là một hàm sẵn có trong PHP được sử dụng để thực thi các lệnh thông qua shell và trả về đầu ra hoàn chỉnh dưới dạng chuỗi..

Là một tập tin PHP là một tập lệnh?

Tệp PHP là gì?Một tệp có tiện ích mở rộng .PhP đề cập đến ngôn ngữ lập trình nguồn mở, được sử dụng để viết các tập lệnh phía máy chủ, để được thực thi trên máy chủ web.Đây là ngôn ngữ kịch bản web được sử dụng rộng rãi nhất thường được sử dụng để phát triển các ứng dụng web quy mô lớn.A file with . php extension refers to open source programming language, used to write server side scripts, to be executed on a web server. It is the most widely used web-scripting language that is typically used for development of large-scale web applications.