3

I've an weird error when trying to assign the value to a variable into another. The initial variable value contains ' signs at the beginning and at the end.

Here is the code :

server = $(uname -n) passpre = "'HPre2053#'" passmon = "'MonH2053#'" mdp="" echo ${server} if [[ "$server" = "cly1024" ]]; then echo "Dentro Pre" mdp = $(passpre) echo $mdp logit "Exécution du script sur Pre. Mot de passe choisi." elif [[ "$server" = "pcy4086" ]]; then echo "Dentro MON" mdp = ${passmon} logit "Exécution du script sur MON. Mot de passe choisi." fi 

Code error :

cly1024 Dentro Pre modMDPconfig.ksh[51]: passpre: not found modMDPconfig.ksh[51]: mdp: not found 

Line 51 is where i do the variable assignation mdp = $(passpre)

3
  • mdp = $(passpre) is not doing an assignment at all. It runs a command mdp with the first argument = and the second command the output of running passpre as a command. Given this, the error message it gives you is entirely descriptive of the problem. Commented Feb 25, 2014 at 14:04
  • Note, by the way, that quotes-as-data (in "'foo'", the ' quotes are data, and the " quotes are syntax) are never able to substitute for syntactical quotes. When you do a parameter expansion, data always stays data -- it can't turn into syntax unless you're then explicitly running that data through an evaluation pass, and doing so is very bad form for security reasons. Commented Feb 25, 2014 at 14:10
  • A variable assignment in the sh family must consist of a single string containing an = sign. On the left side must be a valid variable name (alphanumeric + underscore), the right side can contain anything, but whitespace must be quoted. The normal difference for " ... " and ' ... ' quoting apply. Commented Feb 26, 2014 at 8:23

1 Answer 1

4

This is wrong:

var = value 

This is right:

var=value 

Do not put spaces around the = operator in assignments.


A corrected form of this script follows:

server=$(uname -n) passpre="'HPre2053#'" passmon="'MonH2053#'" mdp="" echo "$server" if [[ "$server" = "cly1024" ]]; then echo "Dentro Pre" mdp=$passpre echo "$mdp" logit "Exécution du script sur Pre. Mot de passe choisi." elif [[ "$server" = "pcy4086" ]]; then echo "Dentro MON" mdp=$passmon logit "Exécution du script sur MON. Mot de passe choisi." fi 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.