我正在尝试从数组中剪切ABCservice
和DEFService
并打印它们。我在这里缺少什么?
urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
for url in "${urlArray[@]}"
do
service=echo $url|cut -f4 -d/
echo $service
done
预期输出:
ABCService
DEFService
当前输出:
./test1.sh: line 6: http://server:port/ABCservice/services/ABCservice?wsdl: No such file or directory
./test1.sh: line 6: http://server:port/DEFservice/services/DEFservice?wsdl: No such file or directory
答案 0 :(得分:3)
这是什么?
service=$(echo $url | cut -d"/" -f4)
echo $service
或直接
echo $(echo $url | cut -d"/" -f4)
代码中的问题:
service=echo $url|cut -f4 -d\
service=$(command)
。cut
已将\
作为分隔符而不是/
。最好用括号括起来:-d "/"
答案 1 :(得分:1)
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
答案 2 :(得分:1)
使用bash
字符串函数:
for i in "${!urlArray[@]}"; do
urlArray[i]="${urlArray[i]%\?*}"
urlArray[i]="${urlArray[i]##*/}"
echo ${urlArray[i]}
done
$ urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
$ for i in "${!urlArray[@]}"; do urlArray[i]="${urlArray[i]%\?*}"; urlArray[i]="${urlArray[i]##*/}"; echo ${urlArray[i]} ; done
ABCservice
DEFservice
答案 3 :(得分:0)
您的分隔符应为正斜杠
echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
答案 4 :(得分:0)
\
是转义字符。请改为\\
。