> -----Original Message----- > From: Chad Skinner > Subject: Bash Script Question > > > 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.
There are probably a dozen ways to write this program, but when it comes to defining shell variables... I always try to leave the spaces out the definition. i.e. BLOCKED_SERVICES=\ "tcp,111,Sun RPC;\ udp,111,Sun RPC;\ tcp,443,Microsoft DS;\ udp,443,Microsoft DS" Also, within the for/do/done, you can redfine IFS="," EX: IFS=';' for BLOCKED in $BLOCKED_SERVICE ; do IFS=',' ; let i=1 for SERVICE in $BLOCKED ; do a[$i]=$SERVICE ; let i++ done printf "a[1]=%s, a[2]=%s, a[3]=%s\n" ${a[1]} ${a[2]} ${a[3]} done Steve Cowles -- redhat-list mailing list unsubscribe mailto:redhat-list-request@;redhat.com?subject=unsubscribe https://listman.redhat.com/mailman/listinfo/redhat-list