-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
Jill448
  • 1,745
  • 10
  • 37
  • 62

5 Answers5

4

What abut this?

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

or directly

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

The problems in your code:

service=echo $url|cut -f4 -d\
  • to save a command output in a variable, we do it like this: service=$(command).
  • your cut had \ as delimiter instead of /. Also it is good to wrap it with brackets: -d "/"
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

Using bash string function:

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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

Your delimiter should be a forward slash

echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
0

The \ is an escape character. Try \\ instead.

zessx
  • 68,042
  • 28
  • 135
  • 158
beiller
  • 3,105
  • 1
  • 11
  • 19