Hướng dẫn how do i run a shell script in php? - làm cách nào để chạy một script shell trong php?

(Php 4, Php 5, Php 7, Php 8)

shell_exec - lệnh thực thi qua shell và trả về đầu ra hoàn chỉnh dưới dạng chuỗiExecute command via shell and return the complete output as a string

Sự mô tả

shell_exec (Chuỗi $command): Chuỗi | Sai | NULL(string $command): string|false|null

Ghi chú::

Trên Windows, đường ống bên dưới được mở ở chế độ văn bản có thể khiến hàm bị hỏng đối với đầu ra nhị phân. Xem xét sử dụng popen () thay vì cho những trường hợp như vậy.popen() instead for such cases.

Thông số

command

Lệnh sẽ được thực thi.

Trả về giá trị

Một chuỗi chứa đầu ra từ lệnh thực thi, false nếu đường ống không thể được thiết lập hoặc null nếu xảy ra lỗi hoặc lệnh không tạo ra đầu ra.string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.

Ghi chú::

Trên Windows, đường ống bên dưới được mở ở chế độ văn bản có thể khiến hàm bị hỏng đối với đầu ra nhị phân. Xem xét sử dụng popen () thay vì cho những trường hợp như vậy.null 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

Thông sốE_WARNING level error is generated when the pipe cannot be established.

command

Lệnh sẽ được thực thi.shell_exec() example

$output shell_exec('ls -lart');
echo 
"

$output
";
?>

Trả về giá trị

  • Một chuỗi chứa đầu ra từ lệnh thực thi, false nếu đường ống không thể được thiết lập hoặc null nếu xảy ra lỗi hoặc lệnh không tạo ra đầu ra.
  • Hàm này có thể trả về null cả khi xảy ra lỗi hoặc chương trình không tạo ra đầu ra. Không thể phát hiện các lỗi thực thi bằng cách sử dụng chức năng này. EXEC () nên được sử dụng khi truy cập vào mã thoát chương trình là bắt buộc.

Một lỗi cấp độ E_WARNING được tạo ra khi đường ống không thể được thiết lập.

Ví dụ

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");

command0

command1

Ví dụ #1 một ví dụ shell_exec ()

Xem thêm

command2

command3

command4

command1

EXEC () - Thực hiện một chương trình bên ngoài

Escapeshellcmd () - Escape Shell Metacharacters

command6

command7

command8

command9

false0

command1

Trev tại DEVATIATE.CO.uk ¶

11 năm trước

false2

false3

false4

false5

command1

smcbride tại msn dot com ¶

1 năm trước

false7

false8

false9

null0

null1

null2

null3

null4

null1

null6

null7

null8

null9

null0

null1

command1

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

null3

null4

null5

null6

command1

Rgemini ¶

1 năm trước

null8

null9

E_WARNING0

E_WARNING1

E_WARNING2

command1

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

E_WARNING4

E_WARNING5

E_WARNING6

E_WARNING7

command1

Rgemini ¶

11 năm trước

E_WARNING9

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
0

command1

smcbride tại msn dot com ¶

1 năm trước

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
2

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
3

command1

Alexandre Dot Schmidt tại Gmail Dot Com ¶

Xem thêm

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
5

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
6

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
7

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
8

$output shell_exec('ls -lart');
echo 
"

$output
";
?>
9

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:0

command1

EXEC () - Thực hiện một chương trình bên ngoài

Escapeshellcmd () - Escape Shell Metacharacters

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:2

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:3

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:4

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:5

Trev tại DEVATIATE.CO.uk ¶

11 năm trước

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:6

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:7

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:8

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:9

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
0

command1

smcbride tại msn dot com ¶

1 năm trước

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
2

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
3

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
4

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
5

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
6

Alexandre Dot Schmidt tại Gmail Dot Com ¶

Escapeshellcmd () - Escape Shell Metacharacters

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
7

Trev tại DEVATIATE.CO.uk ¶

11 năm trước

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
8

smcbride tại msn dot com ¶

1 năm trước

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
9

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
0

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
1

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
2

command1

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
4

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
5

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
6

Rgemini ¶

13 năm trước

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
7

Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
8

command1

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

1 năm trước

command00

command01

command02

command03

command04

command05

command06

command07

command1

Alexandre Dot Schmidt tại Gmail Dot Com ¶

11 năm trước

command09

command10

command11

command12

command1

smcbride tại msn dot com ¶

Escapeshellcmd () - Escape Shell Metacharacters

command14

command15

Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
4

command17

command18

Trev tại DEVATIATE.CO.uk ¶

11 năm trước

command19

smcbride tại msn dot com ¶

6 năm trước

command20

command21

command1

Rgemini ¶

11 năm trước

command23

command24

command25

command1

smcbride tại msn dot com ¶

11 năm trước

command27

command28

command1

smcbride tại msn dot com ¶

6 năm trước

command30

command31

command1

Rgemini ¶

11 năm trước

command33

command34

command35

command1

smcbride tại msn dot com ¶

11 năm trước

command37

command38

command39

smcbride tại msn dot com ¶

1 năm trước

command40

Alexandre Dot Schmidt tại Gmail Dot Com ¶

6 năm trước

command41

command42

command43

command44

command45

command46

command47

command1

Rgemini ¶

6 năm trước

command49

command50

command51

command1

Rgemini ¶

11 năm trước

command53

command54

command1

smcbride tại msn dot com ¶

1 năm trước

command56

command57

command58

command59

command60

command61

command62

command63

command1

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 cách nào để chạy tập lệnh PHP?

Bạn chỉ cần làm theo các bước để chạy chương trình PHP bằng dòng lệnh ...
Mở cửa sổ thiết bị đầu cuối hoặc dòng lệnh ..
Goto thư mục hoặc thư mục được chỉ định có các tệp PHP có mặt ..
Sau đó, chúng ta có thể chạy mã PHP bằng lệnh sau: php file_name.php ..

Làm cách nào để chạy tập lệnh shell?

Các bước để viết và thực thi một tập lệnh..
Mở thiết bị đầu cuối.Chuyển đến thư mục nơi bạn muốn tạo tập lệnh của mình ..
Tạo một tệp với.SH Tiện ích mở rộng ..
Viết tập lệnh trong tệp bằng trình chỉnh sửa ..
Làm cho tập lệnh có thể thực thi bằng lệnh chmod +x ..
Chạy tập lệnh bằng cách sử dụng ./ ..

Làm cách nào để chạy tập lệnh PHP trong Linux?

Bạn có thể thực thi các lệnh Linux trong tập lệnh PHP - tất cả những gì bạn phải làm là đặt dòng lệnh vào Brackits (`).Và cũng tập trung vào exec (), cái này và shell_exec () .. lưu câu trả lời này.put the command line in brackits (`). And also concentrate on exec() , this and shell_exec() .. Save this answer.