[英]How to add curl encode space in url shell script
在應用程序中,當我們創建名稱如下的項目時:
測試項目或演示檢查出現錯誤,即它們之間的空間沒有占用,並且它的工作方式類似於示例演示或測試或鏈接
下面是我遇到的一個問題的腳本:
#!/bin/bash
# Begin
TEMP=$(getopt -n "$0" -a -l "username:,password:,project:,profile:,scanner:,emailReport:,reportType:,tags:" -- -- "$@")
[ $? -eq 0 ] || exit
eval set -- "$TEMP"
while [ $# -gt 0 ]
do
case "$1" in
--username) FX_USER="$2"; shift;;
--password) FX_PWD="$2"; shift;;
--project) FX_PROJECT_NAME="$2"; shift;;
--profile) JOB_NAME="$2"; shift;;
--scanner) REGION="$2"; shift;;
--emailReport) FX_EMAIL_REPORT="$2"; shift;;
--reportType) FX_REPORT_TYPE="$2"; shift;;
--tags) FX_TAGS="$2"; shift;;
--) shift;;
esac
shift;
done
FX_SCRIPT=""
if [ "$FX_TAGS" != "" ];
then
FX_SCRIPT="&tags=script:"+${FX_TAGS}
fi
token=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${FX_USER}'", "password": "'${FX_PWD}'"}' https://example.com/login | jq -r .token)
echo "generated token is:" $token
runId=$(curl --location --request POST "https://example.com/api/v1/runs/project/${FX_PROJECT_NAME}?jobName=${JOB_NAME}®ion=${REGION}&emailReport=${FX_EMAIL_REPORT}&reportType=${FX_REPORT_TYPE}${FX_SCRIPT}" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
echo "runId =" $runId
if [ -z "$runId" ]
then
echo "RunId = " "$runId"
echo "Invalid runid"
echo $(curl --location --request POST "https://example.com/api/v1/runs/project/${FX_PROJECT_NAME}?jobName=${JOB_NAME}®ion=${REGION}&emailReport=${FX_EMAIL_REPORT}&reportType=${FX_REPORT_TYPE}${FX_SCRIPT}" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
exit 1
fi
taskStatus="WAITING"
echo "taskStatus = " $taskStatus
while [ "$taskStatus" == "WAITING" -o "$taskStatus" == "PROCESSING" ]
do
sleep 5
echo "Checking Status...."
passPercent=$(curl --location --request GET "https://example.com/api/v1/runs/${runId}" --header "Authorization: Bearer "$token""| jq -r '.["data"]|.ciCdStatus')
IFS=':' read -r -a array <<< "$passPercent"
taskStatus="${array[0]}"
echo "Status =" "${array[0]}" " Success Percent =" "${array[1]}" " Total Tests =" "${array[2]}" " Total Failed =" "${array[3]}" " Run =" "${array[6]}"
if [ "$taskStatus" == "COMPLETED" ];then
echo "------------------------------------------------"
# echo "Run detail link https://example.com/${array[7]}"
echo "Run detail link https://example.com${array[7]}"
echo "-----------------------------------------------"
echo "Job run successfully completed"
exit 0
fi
done
if [ "$taskStatus" == "TIMEOUT" ];then
echo "Task Status = " $taskStatus
exit 1
fi
echo "$(curl --location --request GET "https://example.com/api/v1/runs/${runId}" --header "Authorization: Bearer "$token"")"
exit 1
return 0
bash job_script.sh --username USERNAME --password PASSWORD --project Test Project
%20
bash job_script.sh --username USERNAME --password PASSWORD --project Test%20Project
OUTPUT COMMAND:- bash job_script.sh --username USERNAME --password PASSWORD --project Test
我需要知道我可以在哪里添加以及需要在上面的腳本中添加什么以避免項目名稱中的空格。 無論我在項目中或在 output 命令中給出什么名稱,它都應該適合我。
嘗試這個:
bash job_script.sh --username USERNAME --password PASSWORD --project "Test Project"
在腳本中我假設最好使用不同的選項標志,即:
--project-spaced) FX_PROJECT_NAME="$2 $3"; shift 2;;
檢查它是否有任何幫助。
#!/bin/bash
# Begin
TEMP=$(getopt -n "$0" -a -l "username:,password:,project:,profile:,scanner:,emailReport:,reportType:,tags:" -- -- "$@")
[ $? -eq 0 ] || exit
eval set -- "$TEMP"
while [ $# -gt 0 ]
do
case "$1" in
--username) FX_USER="$2"; shift;;
--password) FX_PWD="$2"; shift;;
--project) FX_PROJECT_NAME="$2"; shift;;
--profile) JOB_NAME="$2"; shift;;
--scanner) REGION="$2"; shift;;
--emailReport) FX_EMAIL_REPORT="$2"; shift;;
--reportType) FX_REPORT_TYPE="$2"; shift;;
--tags) FX_TAGS="$2"; shift;;
--) shift;;
esac
shift;
done
FX_SCRIPT=""
if [ "$FX_TAGS" != "" ];
then
FX_SCRIPT="&tags=script:"+${FX_TAGS}
fi
token=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${FX_USER}'", "password": "'${FX_PWD}'"}' https://example.com/login | jq -r .token)
echo "generated token is:" $token
URL="https://example.com/api/v1/runs/project/${FX_PROJECT_NAME}?jobName=${JOB_NAME}®ion=${REGION}&emailReport=${FX_EMAIL_REPORT}&reportType=${FX_REPORT_TYPE}${FX_SCRIPT}"
url=$( echo "$URL" | sed 's/ /%20/g' )
runId=$(curl --location --request POST "$url" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
echo "runId =" $runId
if [ -z "$runId" ]
then
echo "RunId = " "$runId"
echo "Invalid runid"
echo $(curl --location --request POST "$url" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
exit 1
fi
taskStatus="WAITING"
echo "taskStatus = " $taskStatus
while [ "$taskStatus" == "WAITING" -o "$taskStatus" == "PROCESSING" ]
do
sleep 5
echo "Checking Status...."
passPercent=$(curl --location --request GET "https://example.com/api/v1/runs/${runId}" --header "Authorization: Bearer "$token""| jq -r '.["data"]|.ciCdStatus')
IFS=':' read -r -a array <<< "$passPercent"
taskStatus="${array[0]}"
echo "Status =" "${array[0]}" " Success Percent =" "${array[1]}" " Total Tests =" "${array[2]}" " Total Failed =" "${array[3]}" " Run =" "${array[6]}"
if [ "$taskStatus" == "COMPLETED" ];then
echo "------------------------------------------------"
# echo "Run detail link https://example.com/${array[7]}"
echo "Run detail link https://example.com${array[7]}"
echo "-----------------------------------------------"
echo "Job run successfully completed"
exit 0
fi
done
if [ "$taskStatus" == "TIMEOUT" ];then
echo "Task Status = " $taskStatus
exit 1
fi
echo "$(curl --location --request GET "https://example.com/api/v1/runs/${runId}" --header "Authorization: Bearer "$token"")"
exit 1
return 0
然后運行你的命令。
bash job_script.sh --username "USERNAME" --password "PASSWORD" --project "Test Project"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.