【发布时间】:2022-01-20 08:39:44
【问题描述】:
下面是我执行得到多行时的输出命令。
我的问题是我应该想要多行而不是脚本中需要单行
输出命令:- bash script.sh --username USERNAME --password PASSWORD --project "Test Project"
oxNjM5ODA2MTg5fQ.ea82ZkR5afv5uT0m6l8ttutOWCelPlxuyr4iU3VkZyU
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9617 0 9617 0 0 7166 0 --:--:-- 0:00:01 --:--:-- 7160
runId = 8a8094017dbc2780017dc6ea6a3c0780
taskStatus = WAITING
Checking Status....
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9725 0 9725 0 0 7209 0 --:--:-- 0:00:01 --:--:-- 7214
Status = PROCESSING Success Percent = 100.0 Total Tests = 75 Total Failed = 0 Run = 16
Checking Status....
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9725 0 9725 0 0 7155 0 --:--:-- 0:00:01 --:--:-- 7155
Status = PROCESSING Success Percent = 100.0 Total Tests = 75 Total Failed = 0 Run = 16
Checking Status....
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9862 0 9862 0 0 7465 0 --:--:-- 0:00:01 --:--:-- 7465
Status = PROCESSING Success Percent = 93.0 Total Tests = 75 Total Failed = 5 Run = 16
Checking Status....
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9862 0 9862 0 0 7493 0 --:--:-- 0:00:01 --:--:-- 7493
Status = PROCESSING Success Percent = 93.0 Total Tests = 75 Total Failed = 5 Run = 16
Checking Status....
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9886 0 9886 0 0 7517 0 --:--:-- 0:00:01 --:--:-- 7517
Status = COMPLETED Success Percent = 90.0 Total Tests = 75 Total Failed = 7 Run = 16
这是我的 shellscript
#!/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
只需要单行我需要在上面的脚本中添加的内容,以便它应该只显示一行,即 状态.... 状态 = 已完成成功百分比 = 90.0 总测试 = 75 总失败 = 7 运行 = 16
【问题讨论】:
-
你在问如何关闭 curl 的进度日志吗? (手册页回答了这个问题)。如果应该只有一行输出,为什么还要多次调用 curl?
-
是的,只有我需要
-
然后将前两个
curls 完全取出,在第三个上使用-s。或者如果您出于其他原因确实需要前两个,则将-s添加到所有三个中,但重定向前两个的输出,以便不会向用户显示。 -
明确地说,
somecommand运行一些命令其输出到标准输出。你不需要写echo $(somecommand)来写它的输出,因为输出是总是写的,除非你主动做一些事情(比如添加>/dev/null)来关闭它。并且echo $(somecommand)在I just assigned a variable, butecho $variableshows something else! 中描述的方式也有问题。