-1

I am trying to cut ABCservice and DEFService dfrom the array and print them.What am I missing here?

  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

Expected Output:

ABCService
DEFService

Current Output:

./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
4

5 回答 5

4

这附近有什么?

service=$(echo $url | cut -d"/" -f4)
echo $service

或直接

echo $(echo $url | cut -d"/" -f4)

您的代码中的问题:

service=echo $url|cut -f4 -d\
  • 要将命令输出保存在变量中,我们这样做service=$(command)
  • cut\作为分隔符而不是/. 用括号括起来也很好:-d "/"
于 2013-05-30T19:26:42.197 回答
1
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
于 2013-05-30T19:27:36.603 回答
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
于 2013-05-30T20:14:11.267 回答
0

您的分隔符应该是正斜杠

echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
于 2013-05-30T19:26:56.803 回答
0

\一个转义字符。试试\\吧。

于 2013-05-30T19:27:25.103 回答