以下是bash的代码:
Sub SendMessage(Subject As String, Recipient As String, Body As String, User As String, Password As String)
Dim sReq As String
Dim xmlMethod As String
Dim XMLreq As New MSXML2.XMLHTTP60
Dim EWSEndPoint As String
EWSEndPoint = "https://outlook.office365.com/EWS/Exchange.asmx"
sReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
sReq = sReq & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">" & vbCrLf
sReq = sReq & "<soap:Header>" & vbCrLf
sReq = sReq & "<t:RequestServerVersion Version=""Exchange2010""/>" & vbCrLf
sReq = sReq & "</soap:Header>" & vbCrLf
sReq = sReq & "<soap:Body>" & vbCrLf
sReq = sReq & "<CreateItem MessageDisposition=""SendAndSaveCopy"" xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages"">" & vbCrLf
sReq = sReq & "<SavedItemFolderId>" & vbCrLf
sReq = sReq & "<t:DistinguishedFolderId Id=""sentitems"" />" & vbCrLf
sReq = sReq & "</SavedItemFolderId>" & vbCrLf
sReq = sReq & "<Items>" & vbCrLf
sReq = sReq & "<t:Message>" & vbCrLf
sReq = sReq & "<t:ItemClass>IPM.Note</t:ItemClass>" & vbCrLf
sReq = sReq & "<t:Subject>" & Subject & "</t:Subject>" & vbCrLf
sReq = sReq & "<t:Body BodyType=""Text"">" & Body & "</t:Body>" & vbCrLf
sReq = sReq & "<t:ToRecipients>" & vbCrLf
sReq = sReq & " <t:Mailbox>" & vbCrLf
sReq = sReq & " <t:EmailAddress>" & Recipient & "</t:EmailAddress>" & vbCrLf
sReq = sReq & " </t:Mailbox>" & vbCrLf
sReq = sReq & "</t:ToRecipients>" & vbCrLf
sReq = sReq & "</t:Message>" & vbCrLf
sReq = sReq & "</Items>" & vbCrLf
sReq = sReq & "</CreateItem>" & vbCrLf
sReq = sReq & "</soap:Body>" & vbCrLf
sReq = sReq & "</soap:Envelope>" & vbCrLf
xmlMethod = "POST"
XMLreq.Open xmlMethod, EWSEndPoint, False, User, Password
XMLreq.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
XMLreq.setRequestHeader "Translate", "F"
XMLreq.setRequestHeader "User-Agent", "Blah"
XMLreq.send sReq
If XMLreq.Status = 207 Then
End If
End Sub
输出是:
您好
但我认为应该是:
您好
0
答案 0 :(得分:6)
你认为错了; - )
将命令放在反引号中会将右侧表达式的输出(stdout)指定给左侧的变量。
$?
为您提供命令的“输出状态”(或返回代码) - 也就是您期望的“0”。
所以:
a=`echo hello`
运行命令“echo hello”但不是回显到stdout,而是“回应”到可变a
。所以a = whatever_the_command_would_have_written_to_stdout(在本例中为“hello”) - 实际上没有任何内容写入stdout,因为它被“s”“捕获”了
答案 1 :(得分:0)
你错误认为 a=`echo hello`
:
echo hello
并将其标准输出直接打印到来电者的标准输出,echo
命令的退出代码(返回值)分配给变量$a
。两者都不是真的;代替:强>
echo hello
的标准输出在内存中捕获(无需打印到来电者的标准输出;这就是command substitutions的工作方式),$a
。命令的退出代码(表示成功与失败的返回值)永远不会直接返回 在类似POSIX的shell中,例如Bash。 /> 使用退出代码的唯一方法是:
显式地,通过在命令($?
包含最新命令'后立即访问特殊变量$?
退出代码)
隐式地,在条件中(退出代码为0
的命令在条件中评估为 true ,任何其他退出代码隐含 false )
因此,要实现您真正想要做的事情,请使用:
echo 'hello' # Execute a command directly (its stdout output goes to the caller's stdout)
a=$? # Save the previous command's exit code in var. $a
echo "$a" # Echo the saved exit code.
答案 2 :(得分:-1)
正如已经提到的这个[ this ]答案,最后执行的命令的返回值存储在
中$? # ${?} is sometimes needed
如果您希望a
包含&#39; hello&#39;以及echo hello
在单独行中的返回值,即
hello
0
下面是一种方法
$ a=`echo -en "hello\n" && echo -n $?` # here $? is ret val for 1st echo
$ echo -e "$a"
hello
0
注意强>
-n
echo
会抑制尾随新行-e
echo
解释转义序列&&
是逻辑和运营商,如果第一个echo
失败,第二个echo
将无法执行需要注意的另一个要点是即使是作业
a=b
有返回值。