-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,695
  • 9
  • 36
  • 59

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
  • 252,262
  • 96
  • 511
  • 570
1
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Adam Siemion
  • 15,064
  • 6
  • 51
  • 88
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
  • 71,025
  • 22
  • 98
  • 142
0

Your delimiter should be a forward slash

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

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

zessx
  • 66,778
  • 28
  • 130
  • 153
beiller
  • 3,075
  • 1
  • 10
  • 17