Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

At the top of my script I have some variables defined like so

# Start Variables
defaultmap="de_dust2"
port="27015"
sourcetvport="27020"
clientport="27005"
maxplayers="16"
ip="0.0.0.0"
parms="-game csgo -usercon +map ${defaultmap} -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +tv_port ${sourcetvport} -maxplayers ${maxplayers}"

I have a function fn_autoip that is designed to change ${ip} if it is set to 0.0.0.0 to the network card IP (somthing like 192.168.1.2). However it is not changing this.

My guess is that it is not changing ${ip} because is is within another variable ${parms} that was already defined when ${ip} was 0.0.0.0. This is how it is executed:

fn_debugserver(){
fn_autoip
${executable} ${parms}
}

Is there a way where ${parms} can be 'reloaded' with the new ${ip} from fn_autoip?

This is from my project on GitHub

share|improve this question
1  
Why do you have a params var set at the beginning of the file? –  RSFalcon7 Jan 23 '14 at 21:29
    
All variables I want to user to easily see and change are at the top. It just makes things much easier. –  dgibbs Jan 23 '14 at 23:06
    
And params is a variable that the users should easily see? –  RSFalcon7 Jan 23 '14 at 23:46

3 Answers 3

Wrapping the value of a variable with double quotes is not enough to protect any nested variable names. So what's happening is you're setting $params, but when your program executes, $params is having those variables expanded, so they're gone when you evaluate $params later on.

Example

Say we have this situation, similar to yours.

$ nested_var="nested secret"
$ params="the value is: ${nested_var}"

When we evaluate these variables:

$ echo $nested_var
nested secret
$ echo $params
the value is: nested secret

So we can see that the variable in $params definition has already been expanded, so it's no longer in the form $nested_var, it's now netsted_secret.

Using single quotes would help protect $params definition.

$ params='the value is: $nested_var'
$ echo $params
the value is: $nested_var

But now the question is how do we tell Bash to expand this variable, later on. Here's where you could use the command eval.

$ nested_var="nested secret"
$ eval "echo $params"
the value is: nested secret

Changing $nested_var:

$ nested_var="another nested secret"
$ eval "echo $params"
the value is: another nested secret

The eval command has a bad rep though. So I think I would encourage you to do what you're trying using functions instead.

Alternative method

I would be tempted to create a function that you pass into it parameters, and the function would return constructed $params string back.

Example

$ genparam () { printf -- "-game csgo -usercon +map %s -strictportbind -ip %s -port %s +clientport %s +tv_port %s -maxplayers %s\n" "$1" "$2" "$3" "$4" "$5" "$6";  }

Here's an expanded view of that one-liner:

$ genparam () { 
    printf -- "-game csgo -usercon +map %s -strictportbind -ip %s -port %s +clientport %s +tv_port %s -maxplayers %s\n" \
       "$1" "$2" "$3" "$4" "$5" "$6";    \
}

Now when we call our function genparm() we pass it the arguments that we want it to use like so:

$ genparam $defaultmap $ip $port $clientport $sourcetvport $maxplayers
-game csgo -usercon +map de_dust2 -strictportbind -ip 0.0.0.0 -port 27015 +clientport 27005 +tv_port 27020 -maxplayers 16
share|improve this answer
    
Wow thanks for this answer. I will have a good read of this later on to make sure I understand this :D –  dgibbs Jan 24 '14 at 12:37

Probably not the best solution, but you can transform you variable into a function and use it like this

parms () {
  echo "-game csgo -usercon +map ${defaultmap} -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +tv_port ${sourcetvport} -maxplayers ${maxplayers}"
}

#...

fn_debugserver() {
  fn_autoip
  $executable $(params)
}
share|improve this answer
    
This does work well. However I am hoping there is another option if possible. Thanks for your help –  dgibbs Jan 23 '14 at 23:14

You need to set parms after fn_autoip is run (or at the end of that function). The variables are set in the order they show up in the file.

You 'reload' the variable by setting it again.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.