On Thu, 25 Jan 2001 at 10:21am (-0800), Brad Doster wrote:

> 1) I have a variable with a list of variable names as its content, e.g.
> 'varlist=$var1 $var2 $var3'.  I want to manipulate $varlist such that it
> ='var1 var2 var3', i.e get rid of the '$'s in front of each variable name.
> A 'sed' example that I've tried is:
>
>       varlist=`echo "$varlist" | sed 's/$//'`
>
> I've tried every variation I can think of with and without "s around
> '$varlist', using single and double quotes for the sed command, and escaping
> the $ with \.  In short, nothing works -- it always comes back with '$var1
> $var2 $var3'.

Sounds like your doing the right thing... I don't know why it whoudl not be
working.

$ varlist='$var1 $var2 $var3'
$ echo $varlist
$var1 $var2 $var3
$ echo $varlist | sed 's/\$//g'
var1 var2 var3
$

Not using the g on the end of the sed command would cause it to only replace
the first $ but I would still have expected it to do /something/.

$ echo $varlist | sed 's/\$//'
var1 $var2 $var3
$

> I did find that 'varlist=`echo $varlist | awk -F "$" '{print $2}'` does
> work, but it seems there ought to be a way to do it with sed.  Any idea what
> I'm missing?
>
> 2) Given two variables, say var1="a" and var2="b", I want to create var3
> such that it is "a<newline>b", i.e 'echo "$var3"' produces:
>
>       a
>       b
>
> Combinations like 'var3=$var1\n$var2' end up as 'anb', i.e. the '\n' is not
> treated as a <newline> character.  Again, I've tried it with a variety of
> quoting patterns, etc., all to no avail.  So again...
>

This parts a bit trickier I think.  Even if you get the linefeed in there
bash is not inclined to show it to you.  It treats a linefeed as a word
seperator so a space is a tab is a linefeed.  IFS lets us define what is to
be concidered as seperators so we can get round this.

$ var1=a
$ var2=b
$ var3="$var1
> $var2"
$ echo $var3
a b
$ IFS=' '
$ echo $var3
a
b
$

I don't know if that's a workable 'solution' in your case.

In the example I wrote the assignment on two seperate lines but you could
also do something like...

var3="$(echo $var1; echo $var2)"

... or..

var3="$(echo -e $var1\\n$var2)"

... you just need something else to provide the line feed becuase bash won't
evaluate \n directly.

M.


-- 
WebCentral Pty Ltd           Australia's #1 Internet Web Hosting Company
Level 1, 96 Lytton Road.           Network Operations - Systems Engineer
PO Box 4169, East Brisbane.                       phone: +61 7 3249 2583
Queensland, Australia.                            pgp key id: 0x900E515F



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to