Operating System - HP-UX
1826444 Members
4200 Online
109692 Solutions
New Discussion

Re: How do I write this scipt

 
Fonda
New Member

How do I write this scipt

I want to write a script to modify a variable list, like

fruit="apple pear banana"
I want to modiy it to something like
fruit="apple cherry pear banana strawberry"

How do I write this shell command? Thanks
5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: How do I write this scipt

maybe the shift command

fruit="apple pear bannana"
var2="stawberry"
fruit="${fruit}${var2}"

echo $fruit

Output is:
apple cherry pear banana strawberry

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sridhar Bhaskarla
Honored Contributor

Re: How do I write this scipt

Hi,

YOu will need to where you are going to add your values. To add cherry after apple and strawberry to the end, I would do the following

fruit=$(echo $fruit|sed -e 's/apple/apple cherry/g' -e 's/$/ strawberry/')

There is a space after strawberry in the second sed statement.
-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ronelle van Niekerk
Regular Advisor

Re: How do I write this scipt

This will add cherry and strawberry to the end of the other fruits:

fruit="apple pear banana"

fruit="$fruit cherry strawberry"

#echo $fruit
apple pear banana cherry strawberry


You could use awk to add cherry to the middle of the other fruits and strawbwerry to the end:

fruit=`echo $fruit | awk '{print $1_"_cherry_"_$2_"_"_$3_"_strawberry"}'

- replace the underscores with spaces.
rm -r /it/managers
curt larson_1
Honored Contributor

Re: How do I write this scipt

you could do something like the attached script does
Martin Robinson
Frequent Advisor

Re: How do I write this scipt

It's quite easy.

fruit="apple pear banana"
fruit="apple cherry pear banana strawberry"

.. unless you are trying to perform an operation on the original value, but you don't give enough information on what you are really trying to do.

:->