我正在Ubuntu18OS上编写一个BASH脚本。我正在对字符串(追加、剪切)进行操作,之后,我希望将它用作docker命令的参数。问题是它带撇号返回,而我只需要没有这些的值。
if [[ "${TESTIM_LABEL}" =~ "," ]]; then
IFS=','
read -a strarr <<< "$TESTIM_LABEL"
LABELS=""
prefix=" --label "
for val in "${strarr[@]}";
do
LABELS+=${prefix}${val}
echo "$LABELS"
done
printf "$LABELS"
else
printf "im outside the IF"
LABELS="--label ${TESTIM_LABEL}"
fi在这个IF语句上,当条件为真,并且我在IF中,标签变量的值打印了,没有撇号,但是当我稍后使用这个param作为一个更长的命令的一部分时,它被插入和撇号
示例:
RESULT=$(docker run --rm -e rpLaunch="${RP_LAUNCH_NAME}" -e rpTeam="${RP_TEAM}" -e rpUuid="${rp_uuid}" -e rpBranchNameTag="${BRANCH_NAME}" -e rpDescription="${RP_DESCRIPTION}" -v $2:/opt/testim-runner ${TESTIM_DOCKER} \
--token ${TESTIM_TOKEN} \
--project "${TESTIM_PROJECT}" \
${LABELS} 输出将是(插入到IF "xxx,yyy“之后):
docker run --rm -e rpLaunch=master/testim/@arion_ab_testing -e rpTeam=SocialArion -e rpUuid= -e rpBranchNameTag=master -e rpDescription=http://jenkins-prod-search.internalk.com/job/ui-pull-request/3127/ -v /home/centos/jenkins/workspace/ui-pull-request:/opt/testim-runner testim/docker-cli --token Dt9kFOtOhNcMum2gZjvnapOpGyq8vgreEnZOJF2nR9SeCJaRGE --project bJFghGy6Jo9yvtOO3ZiO ' --label xxx --label yyy'周围的撇号--标签xxx --标签yyy'需要移除。
我该怎么做?
发布于 2021-01-06 18:57:20
太久不能发表评论..。我认为这可能归结为一个(简单的)问题,即如何填充TESTIM_LABEL变量。注意到:OP尚未向我们展示上述变量是如何填充的。
一个简单的例子是,将OP的当前代码用于演示目的:
#!/usr/bin/bash
read -p "enter TESTIM_LABEL: " TESTIM_LABEL # added to OP's code; have user enter value @ prompt;
# the rest is cut-n-pasted from the question ...
if [[ "${TESTIM_LABEL}" =~ "," ]]; then
IFS=','
read -a strarr <<< "$TESTIM_LABEL"
LABELS=""
prefix=" --label "
for val in "${strarr[@]}";
do
LABELS+=${prefix}${val}
echo "$LABELS"
done
printf "$LABELS"
else
printf "im outside the IF"
LABELS="--label ${TESTIM_LABEL}"
fi有几个示例运行在我们引用输入的地方(而不是):
$ testim.bash
enter TESTIM_LABEL: 'xxx,yyy'
--label 'xxx
--label 'xxx --label yyy'
--label 'xxx --label yyy' # unwanted quotes
$ testim.bash
enter TESTIM_LABEL: xxx,yyy
--label xxx
--label xxx --label yyy
--label xxx --label yyy # no quotes当然,可能还有另一种解释,但如果OP提供更多关于如何设置TESTIM_LABEL变量的详细信息,则会有所帮助。
发布于 2021-01-06 19:31:32
只需像这样把它们移除:
test="'test'"
#with
$ echo "$test"
'test'
#without
$ echo "${test//\'}"
testhttps://stackoverflow.com/questions/65597123
复制相似问题