如何在url shell脚本中添加curl编码空间



在应用程序中,当我们创建名称类似于的项目时

测试项目或演示检查出错,即,它们之间的空间不占用,其工作方式类似于示例演示或测试或链接

以下是我的脚本,我遇到了一个问题:


#!/bin/bash
# Begin
TEMP=$(getopt -n "$0" -a -l "username:,password:" -- -- "$@")
[ $? -eq 0 ] || exit
eval set --  "$TEMP"
while [ $# -gt 0 ]
do
case "$1" in
--username) TS_USER="$2"; shift;;
--password) TS_PWD="$2"; shift;;
--) shift;;
esac
shift;
done

curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${TS_USER}'", "password": "'${TS_PWD}'"}' https://example.com/login
echo "generated token is:" $token
curl --location --request POST "https://example.com/" --header "Authorization: Bearer "$token"" 
echo "runId =" $runId
if [ -z "$runId" ]
then
echo "RunId = " "$runId"
echo "Invalid runid"
echo $(curl --location --request POST "https://example.com/" --header "Authorization: Bearer "$token"")
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/" --header "Authorization: Bearer "$token"")

taskStatus="${array[0]}"
echo "Status =" "${array[0]}" " Success Percent =" "${array[1]}"  " Total Tests =" "${array[2]}" " Total Failed =" "${array[3]}"

if [ "$taskStatus" == "COMPLETED" ];then
echo "------------------------------------------------"

echo  "Run detail link https://example.com${array[7]}"
echo "-----------------------------------------------"
echo "Job run successfully completed"
exit 0
fi
done

echo "Task Status = " $taskStatus
exit 1
fi
echo "$(curl --location --request GET "https://example.com/" --header "Authorization: Bearer "$token"")"
exit 1
return 0
  1. 由于空间
    bash script.sh --username USERNAME --password PASSWORD --project Test Project
    
  2. 以下命令有效,因为在空格之间我添加了%20
    bash script.sh --username USERNAME --password PASSWORD --project Test%20Project 
    
  3. 由于我给定的单个工作或名称,以下命令正在工作
    OUTPUT COMMAND:- bash script.sh --username USERNAME --password PASSWORD --project Test
    

检查它是否有任何帮助。

#!/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}&region=${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"

试试这个:

bash job_script.sh --username USERNAME --password PASSWORD --project "Test Project"

在脚本中,我认为最好使用不同的选项标志,即:

TEMP=$(getopt -n "$0" -a -l "username:,password:,project:,project-spaced:,profile:,scanner:,emailReport:,reportType:,tags:" -- -- "$@")
.
.
.
--project-spaced) FX_PROJECT_NAME="$2 $3"; shift 2;;

最新更新