如何忽略shell_exec中的错误?

时间:2017-05-26 12:40:31

标签: php error-handling shell-exec

我在PHP中获得了一个shell命令的输出

Sub email_DGMS89()

Application.ScreenUpdating = False

Dim olApp As New Outlook.Application
Dim olNameSpace As Object
Dim olMailItem As Outlook.MailItem
Dim olFolder As Object
Dim olFolderName As String
Dim olAtt As Outlook.Attachments
Dim strName As String
Dim sPath As String
Dim i As Long
Dim j As Integer
Dim olSubject As String
Dim olSender As String
Dim sh As Worksheet
Dim LastRow As Integer

Dim TempFolder As String: TempFolder = VBA.Environ$("TEMP")
Dim wB As Excel.Workbook


'delete content except from row 1
ThisWorkbook.Worksheets("FileNames").Rows(2 & ":" & ThisWorkbook.Worksheets("FileNames").Rows.Count).Delete

'set foldername and subject
olFolderName = ThisWorkbook.Worksheets("Control").Range("D10")
'olSubject = ThisWorkbook.Worksheets("Control").Range("D16")
olSender = ThisWorkbook.Worksheets("Control").Range("D16")

sPath = Application.FileDialog(msoFileDialogFolderPicker).Show
sPathstr = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

Set olNameSpace = olApp.GetNamespace("MAPI")

'check if folder is subfolder or not and choose olFolder accordingly
'Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders(olFolderName)
Set olFolder = olNameSpace.Folders("email@email.com").Folders("Inbox")

If (olFolder = "") Then
    Set olFolder = olNameSpace.Folders("email@email.com").Folders("Inbox")
End If

'loop through mails
h = 2
For i = 1 To olFolder.items.Count
    '''Const olMail = 43 (&H2B)
    If olFolder.items(i).Class <> olMail Then
    Else
        Set olMailItem = olFolder.items(i)

        'check if the search name is in the email subject
        'If (InStr(1, olMailItem.Subject, olSubject, vbTextCompare) <> 0) Then
        If (InStr(1, olMailItem.Sender, olSender, vbTextCompare) <> 0) Then

            With olMailItem
                For j = 1 To .Attachments.Count
                    strName = .Attachments.Item(j).DisplayName

                    'check if file already exists
                    If Not Dir(sPathstr & "\" & strName) = vbNullString Then
                         strName = "(1)" & strName
                    Else
                    End If

                    '''Save in temp
                    .Attachments(j).SaveAsFile TempFolder & "\" & strName
                    ThisWorkbook.Worksheets("FileNames").Range("A" & h) = strName

                    '''Open file as read only
                    Set wB = workbooks.Open(TempFolder & "\" & strName, True)
                    DoEvents

                    '''Start error handling
                    On Error Resume Next
                    Set sh = wB.sheets("ASK")
                    Set sh = wB.sheets("BID")
                    If Err.Number <> 0 Then
                        '''Error = At least one sheet is not detected
                    Else
                        '''No error = both sheets found
                        .Attachments(j).SaveAsFile sPathstr & "\" & strName
                    End If
                    Err.Clear
                    Set sh = Nothing
                    wB.Close
                    On Error GoTo 0

                    h = h + 1
                Next j

            End With

        End If
    End If
Next i

Application.ScreenUpdating = True
MsgBox "Download complete!", vbInformation + vbOKOnly, "Done"

End Sub

并在终端中运行PHP脚本。当$str = shell_exec("command"); 返回错误时,它将打印在终端上。如何判断shell command仅返回命令输出而没有任何错误输出?

3 个答案:

答案 0 :(得分:3)

您只需通过将stderr重定向到/dev/null

来放弃错误输出
$str = shell_exec("command 2>/dev/null");

非错误输出 - stdout - 将像以前一样存储到$str

请注意,您不需要使用shell_exec运算符抑制@上的错误,也不需要将其包含在try - catch块中,因为shell_exec不会失败(您没有PHP运行时错误)。

要求它执行的命令可能会产生错误,上面的方法会抑制输出中的那些。

另外some-command > /dev/null 2>&1和其他建议不是你想要的(如果我理解你的问题),因为这会丢弃错误和非错误输出

最终说明:您可以决定捕获/放弃stdout和/或stderr

当然,您必须依赖于以下事实:您运行的命令会将常规输出发送到stdout并将错误发送到stderr。如果命令不符合标准(例如,将所有内容发送到stdout),那你就不幸了。

答案 1 :(得分:0)

如果要忽略命令的错误,则需要修改命令以重定向错误,如下所示:

some-command > /dev/null 2>&1

答案 2 :(得分:-1)

您也可以在代码的开头执行此操作:

ini_set( 'display_errors', 0 );
error_reporting( E_ALL );

如果设置为0,则不会看到错误,如果设置为1,则会看到所有错误