Function to conditionally set a variable read-only
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
If I had a script which sets variables read-only to some odd values, and sets errexit
because of other unsafe operations:
#!/bin/bash
set -e
declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
declare -r SAY=_say
declare -r SUM=_sum
declare -r MV=_mv
set +e
And I source it to get the definitions, the second time because it's in development:
$ . s.bash
$ . s.bash
bash: declare: NOTIFY: readonly variable
Exited
Normally declare -r EXISTING_VAR
would neither stop the script nor remove the old, working definition of EXISTING_VAR
.
But with errexit
, assigning to an existing variable is understandably a failure. The easy options are to remove -r
or use set +e
for that part of the script.
Barring those, is it possible to write a Bash function to take the place of declare -r
but not re-assign if the name already exists?
I tried:
# arg #1: var name, #2: value
set_var_once ()
# test whether the variable with the
# name stored in $1 exists
if [[ -z "$!1" ]]
then # if it doesn't, set it
declare -r $1=$2
fi
I also tried things along the lines of eval "declare -r $1=$(eval $2)"
, it feels like eval
is required somewhere here but I'm not sure where.
All of the versions of set_var_once
result in not setting the variable they should.
bash shell-script variable readonly declare
add a comment |Â
up vote
1
down vote
favorite
If I had a script which sets variables read-only to some odd values, and sets errexit
because of other unsafe operations:
#!/bin/bash
set -e
declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
declare -r SAY=_say
declare -r SUM=_sum
declare -r MV=_mv
set +e
And I source it to get the definitions, the second time because it's in development:
$ . s.bash
$ . s.bash
bash: declare: NOTIFY: readonly variable
Exited
Normally declare -r EXISTING_VAR
would neither stop the script nor remove the old, working definition of EXISTING_VAR
.
But with errexit
, assigning to an existing variable is understandably a failure. The easy options are to remove -r
or use set +e
for that part of the script.
Barring those, is it possible to write a Bash function to take the place of declare -r
but not re-assign if the name already exists?
I tried:
# arg #1: var name, #2: value
set_var_once ()
# test whether the variable with the
# name stored in $1 exists
if [[ -z "$!1" ]]
then # if it doesn't, set it
declare -r $1=$2
fi
I also tried things along the lines of eval "declare -r $1=$(eval $2)"
, it feels like eval
is required somewhere here but I'm not sure where.
All of the versions of set_var_once
result in not setting the variable they should.
bash shell-script variable readonly declare
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If I had a script which sets variables read-only to some odd values, and sets errexit
because of other unsafe operations:
#!/bin/bash
set -e
declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
declare -r SAY=_say
declare -r SUM=_sum
declare -r MV=_mv
set +e
And I source it to get the definitions, the second time because it's in development:
$ . s.bash
$ . s.bash
bash: declare: NOTIFY: readonly variable
Exited
Normally declare -r EXISTING_VAR
would neither stop the script nor remove the old, working definition of EXISTING_VAR
.
But with errexit
, assigning to an existing variable is understandably a failure. The easy options are to remove -r
or use set +e
for that part of the script.
Barring those, is it possible to write a Bash function to take the place of declare -r
but not re-assign if the name already exists?
I tried:
# arg #1: var name, #2: value
set_var_once ()
# test whether the variable with the
# name stored in $1 exists
if [[ -z "$!1" ]]
then # if it doesn't, set it
declare -r $1=$2
fi
I also tried things along the lines of eval "declare -r $1=$(eval $2)"
, it feels like eval
is required somewhere here but I'm not sure where.
All of the versions of set_var_once
result in not setting the variable they should.
bash shell-script variable readonly declare
If I had a script which sets variables read-only to some odd values, and sets errexit
because of other unsafe operations:
#!/bin/bash
set -e
declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
declare -r SAY=_say
declare -r SUM=_sum
declare -r MV=_mv
set +e
And I source it to get the definitions, the second time because it's in development:
$ . s.bash
$ . s.bash
bash: declare: NOTIFY: readonly variable
Exited
Normally declare -r EXISTING_VAR
would neither stop the script nor remove the old, working definition of EXISTING_VAR
.
But with errexit
, assigning to an existing variable is understandably a failure. The easy options are to remove -r
or use set +e
for that part of the script.
Barring those, is it possible to write a Bash function to take the place of declare -r
but not re-assign if the name already exists?
I tried:
# arg #1: var name, #2: value
set_var_once ()
# test whether the variable with the
# name stored in $1 exists
if [[ -z "$!1" ]]
then # if it doesn't, set it
declare -r $1=$2
fi
I also tried things along the lines of eval "declare -r $1=$(eval $2)"
, it feels like eval
is required somewhere here but I'm not sure where.
All of the versions of set_var_once
result in not setting the variable they should.
bash shell-script variable readonly declare
bash shell-script variable readonly declare
asked 12 mins ago
cat
1,66021135
1,66021135
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478515%2ffunction-to-conditionally-set-a-variable-read-only%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password