1

I have a function defined below. Each time it runs through the array, I want it to update an existing variable (${1}_FLAG}). Not working as I intended it to. Basically if a new version of said package exist, set the flag to true so I can call another function.

Removed "extra" variables because people are caught up on them. What I need is to update the HTTP_FLAG PHP_FLAG and MOD_QOS variables when they're being looped through.

HTTP_FLAG=false
PHP_FLAG=false
MOD_QOS=false

PKGS=(HTTP PHP MOD_QOS);

check_new_version() {        
    # If a new version is available, download the source file
    if [ ${!check_version} != ${!current_version} ]
    then
        ...
        ...
        ${1}_FLAG=true
    fi
} 

for i in "${PKGS[@]}"
do
    check_new_version $i
done

if ${HTTP_FLAG}; then
    ...
    ...
fi
3
  • Where are you setting HTTP_VER, HTTP_CURRENT, HTTP_LINK, et. al.? Commented Nov 19, 2012 at 17:28
  • Above the script is rather long, I didn't think they would be relevant. An example would be HTTP_LINK="http://apache.ziply.com/httpd/${HTTP_VER}" Commented Nov 19, 2012 at 17:30
  • please edit your question to include demonstration of "Not working as I intended it to", with required output VS current output. OR use set -vx and set +vx to turn on/off shell debugging trace to see how each cmd/block is being processed with variable substitutions include. Good luck. Commented Nov 19, 2012 at 17:31

2 Answers 2

1

I think you need eval:

$ set -- FOO
$ FOO_FLAG=false
$ eval ${1}_FLAG=true
$ echo $FOO_FLAG
true
$

In bash, you'd like to use the ${!var} notation, but I didn't manage to find the direct invocation that would work. This gets close via the var variable, but not quite there:

$ var=${1}_FLAG
$ FOO_FLAG=false
$ ${!var}=true
bash: false=true: command not found
$ : ${!var}=true
$ echo $FOO_FLAG
false
$ 
Sign up to request clarification or add additional context in comments.

2 Comments

That is exactly the issue I'm having.
I got around this by using export "${1}_FLAG"=true
0

bash has indirect parameter expansion, so you can do:

varname=${1}_LINK
value=${!varname}

example

$ foo_LINK="hello world"
$ set -- foo
$ varname=${1}_LINK
$ echo ${!varname}
hello world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.