Shell Azure Pipeline 脚本中的 Curl 命令不起作用

Curl Command in Shell Script in Azure Pipeline not working

我正在尝试 运行 使用 azure 管道提供某些 url 的状态代码的脚本。

我的 Azure Yaml 文件:

pool:
  vmImage: ubuntu-16.04

steps:
- script: echo Hello
  displayName: ' Welcome'
- script: cat webpages.txt
  displayName: 'display file'
- script: curl -s -w '%{http_code}\n' -o /dev/null https://www.google.co.in
  displayName: 'Checking Curl Code'
- script: cat -v script.sh
  displayName: 'Cariage retrun'
- task: ShellScript@2
  inputs:
    scriptPath: script.sh

我的Script.sh文件

#!/bin/sh
while read line ; do echo "$line - `curl -s -w '%{http_code}\n' -o /dev/null $line`" ;done < webpages.txt

webpages.txt 文件

https://www.vodafone.co.uk/good-stuff
https://www.vodafone.co.uk/help-and-information/cancel-your-account
https://www.vodafone.co.uk/help-and-information/complaints
https://www.vodafone.co.uk/help-and-information/complaints/code-of-practice
https://www.vodafone.co.uk/help-and-information/costs-and-charges
https://www.vodafone.co.uk/help-and-information/costs-and-charges/call-and-text-charges
https://www.vodafone.co.uk/help-and-information/costs-and-charges/data-charges

问题

当我 运行 我的管道时,curl 命令不起作用 输出为

2020-11-17T09:59:05.4094324Z ##[section]Starting: ShellScript
2020-11-17T09:59:05.4102534Z ==============================================================================
2020-11-17T09:59:05.4102904Z Task         : Shell script
2020-11-17T09:59:05.4103204Z Description  : Run a shell script using Bash
2020-11-17T09:59:05.4103453Z Version      : 2.165.2
2020-11-17T09:59:05.4103701Z Author       : Microsoft Corporation
2020-11-17T09:59:05.4104086Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/shell-script
2020-11-17T09:59:05.4104506Z ==============================================================================
2020-11-17T09:59:05.7637654Z [command]/bin/bash /home/vsts/work/1/s/script.sh
2020-11-17T09:59:05.7692023Z https://www.vodafone.co.uk/good-stuff
2020-11-17T09:59:05.7700780Z  - 000
2020-11-17T09:59:05.7797276Z https://www.vodafone.co.uk/help-and-information/cancel-your-account
2020-11-17T09:59:05.7798378Z  - 000
2020-11-17T09:59:05.7851183Z https://www.vodafone.co.uk/help-and-information/complaints
2020-11-17T09:59:05.7866944Z  - 000
2020-11-17T09:59:05.7908420Z https://www.vodafone.co.uk/help-and-information/complaints/code-of-practice
2020-11-17T09:59:05.7909144Z  - 000
2020-11-17T09:59:05.7967261Z https://www.vodafone.co.uk/help-and-information/costs-and-charges
2020-11-17T09:59:05.7967920Z  - 000
2020-11-17T09:59:05.8023329Z https://www.vodafone.co.uk/help-and-information/costs-and-charges/call-and-text-charges
2020-11-17T09:59:05.8024443Z  - 000
2020-11-17T09:59:05.8095527Z https://www.vodafone.co.uk/help-and-information/costs-and-charges/data-charges

但是如果我用

替换我的 curl
curl -s -w '%{http_code}\n' -o /dev/null https://www.vodafone.co.uk/good-stuff

它给出了 200 的输出。

当我在本地 运行 时,它完美地工作:

$ while read line ; do echo "$line - `curl -s -w '%{http_code}\n' -o /dev/null $line`" ;done < webpages.txt
https://www.vodafone.co.uk/good-stuff - 200
https://www.vodafone.co.uk/help-and-information/cancel-your-account - 200
(...)

我注意到 \n 字符打印在您的 echo $line 中,这可能是导致问题的原因。可以解决它的是:

  • $line 替换为 ${line},
  • echo 替换为 echo -n 以省略换行符。

这就是我所做的,

  1. 我用一个简单的 curl $line 替换了整个 curl 命令
  2. 它给了我一个错误:curl: (3) URL
  3. 中发现非法字符
  4. 所以,我认为我的 URL 添加了一些不需要的值。
  5. 我用
  6. 替换了我的 do
line=${line%$'\r'}
while read line ; do line=${line%$'\r'} ; echo "$line - `curl -s -w '%{http_code}\n' -o /dev/null $line`"; done < webpages.txt

瞧,它成功了!