เหตุใดคำสั่ง curl ของฉันจึงล้มเหลวเมื่อรันผ่าน applescript แต่ทำงานได้ดีเมื่อฉันรันโดยตรงในเทอร์มินัล

Aug 19 2020

ฉันพยายามเขียน applescript เพื่อควบคุมโปรเจ็กเตอร์ที่ติดตั้งโดยใช้อินเทอร์เฟซบนเว็บในตัว ฉันมีคำสั่ง curl ที่ส่ง Ping ไปยัง URL ที่เหมาะสมและสามารถควบคุมโปรเจ็กเตอร์ได้ตามต้องการจากบรรทัดคำสั่งเมื่อฉันสร้างสิ่งนี้ลงในแอปเปิ้ลสคริป (จำเป็นต้องใช้โดยซอฟต์แวร์ควบคุมการแสดงของฉัน)

applescript ของฉันในปัจจุบันมีลักษณะดังนี้:

set theUrl to "http://192.168.0.103/cgi-bin/proj_ctl.cgi?key=shutter_on&lang=e&osd=on"
set theUsername to "dispadmin" -- set this to the username for the projector
set thePassword to "password" --set this to the password for the projector

set myFile to (POSIX path of (path to temporary items)) & "curl_downloaded_file.xml"

set curlScript to "/usr/bin/curl --connect-timeout 1 --anyauth --user" & " " & theUsername & ":" & thePassword & " -L " & theUrl & " -o " & myFile

display dialog "my variable: " & curlScript

do shell script curlScript

display dialogบรรทัดสำหรับการแก้จุดบกพร่องเพื่อแสดงสิ่งที่คำสั่งขดจะมีลักษณะเช่นเมื่อผมทำงานนี้ฉันจะได้รับต่อไปนี้:

/usr/bin/curl --connect-timeout 1 --anyauth --user dispadmin:password -L http://192.168.0.103/cgi-bin/proj_ctl.cgi?key=shutter_on&lang=e&osd=on -o /private/var/folders/zs/f1hpkd2x281fvzyh4zd5dpcw0000gn/T/TemporaryItems/curl_downloaded_file.xml

ซึ่งดูเหมือนถูกต้อง หากฉันเพิ่มเครื่องหมายคำพูดรอบ ๆ URL ฉันสามารถเรียกใช้คำสั่งนี้ในเทอร์มินัลโดยไม่มีปัญหาและได้รับเอฟเฟกต์ที่ต้องการอย่างไรก็ตาม Applescript จะคายข้อผิดพลาดต่อไปนี้:

error "sh: -o: command not found
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   350  100   350    0     0  38888      0 --:--:-- --:--:-- --:--:-- 38888

100   267    0   267    0     0   5235      0 --:--:-- --:--:-- --:--:--  5235" number 127

ดูเหมือนว่า curl กำลังทำงานอยู่ แต่มันไม่ได้ไปที่เว็บเซิร์ฟเวอร์ของโปรเจ็กเตอร์เพื่อเรียกใช้การควบคุมนอกจากนี้ยังerror "sh: -o: command not foundทำให้ฉันคิดว่าคำสั่งไม่ได้รับการแยกวิเคราะห์ / ส่งผ่านอย่างถูกต้อง

ฉันทำอะไรผิดที่นี่?

คำตอบ

2 user3439894 Aug 19 2020 at 22:00

ในขณะที่

http://192.168.0.103/cgi-bin/proj_ctl.cgi?key=shutter_on&lang=e&osd=on

ทำงานได้ดีในเบราว์เซอร์ก็ไม่ได้อยู่ในเปลือก

เพื่อแก้ไขปัญหาที่คุณจำเป็นต้องพูดURL

ในset curlScript to ...การเปลี่ยนแปลงtheUrlอย่างใดอย่างหนึ่งtheUrl's quoted formหรือ:quoted form of theUrl