On Mon, 4 Nov 2002, Chad Skinner wrote:

> Is there a way in a bash script to trim the spaces from the front and end of
> a variable
>
> I have a script that contains the following variable definition
>
>
>       BLOCKED_SERVICES="tcp,111,Sun RPC;\
>                         udp,111,Sun RPC;\
>                         tcp,443,Microsoft DS;\
>                         udp,443,Microsoft DS"
>
> I am wondering what the simplest method of extracting each of the three
> elements from each line of the variable. In otherwords, each line is a
> PROTOCOL, PORT_RANGE, and DESCRIPTION. I have tried a for loop in bash
> similar to the following.
>
>       IFS=";"
>
>       for SERVICE in $BLOCKED_SERVICES
>       do
>          # SEPARATE SERVICE VARIABLES
>          PROTOCOL=`echo $SERVICE | $CUT -f1 -d","`
>          PORT=`echo $SERVICE | $CUT -f2 -d","`
>          MESSAGE=`echo $SERVICE | $CUT -f3 -d","`
>
> The problem is that this gives the protocol with the leading spaces and I
> need to get rid of them. Does anyone know how to do this or have a better
> solution.
>

How about like this?


while read PROTOCOL PORT MESSAGE
do
        echo $PROTOCOL $PORT $MESSAGE
done <<-Eod
    tcp 111 Sun RPC
    udp 111 Sun RPC
    tcp 443 Microsoft DS
    udp 443 Microsoft DS
Eod



-- 
John Darrah (u05192)    | Dept: N/C Programming
Giddens Industries      | Ph: (425) 353-0405 #133
PO box 3190             | Ph: (206) 767-4212 #133
Everett  WA    98203    | Fx: (206) 764-9639



-- 
redhat-list mailing list
unsubscribe mailto:redhat-list-request@;redhat.com?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to