bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: unable to redirect readonly variable error


From: Chris F.A. Johnson
Subject: Re: unable to redirect readonly variable error
Date: Thu, 7 Jul 2005 21:08:32 -0400 (EDT)

On Thu, 7 Jul 2005, Ian Macdonald wrote:

On Thu 07 Jul 2005 at 20:07:33 -0400, you wrote:

On Thu, 7 Jul 2005, Ian Macdonald wrote:

Hello,
This doesn't seem right:
[ianmacd@jiskefet bash_completion]$ foo=x
[ianmacd@jiskefet bash_completion]$ foo=bar
[ianmacd@jiskefet bash_completion]$ readonly foo
[ianmacd@jiskefet bash_completion]$ foo=bar >/dev/null
bash: foo: readonly variable
[ianmacd@jiskefet bash_completion]$ foo=bar 2>/dev/null
bash: foo: readonly variable
[ianmacd@jiskefet bash_completion]$ foo=bar &>/dev/null
bash: foo: readonly variable

    What are you tring to redirect? There is no output from your
    command; it is only an assignment to a read-only variable (which is
    naturally going to fail).

I'm trying to redirect the error, because I have a read-only variable
that may have attempts made to redefine it. I want those to silently
fail.

   You could define a function:

assign()
{
   case $(declare -p $1 2>/dev/null) in
      declare\ -r*) ;;
      *) eval "$1=\$2" ;;
   esac
}

   ..and use it for your assignments;

$ assign foo bar
$ echo $foo
bar
$ assign foo "$foo and grill"
$ echo $foo
bar and grill
$ readonly foo
$ assign foo bar
$ echo $foo
bar and grill
$

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]