Search code examples
shellcut

Shell script cut command


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

Solution

  • 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 "/"