Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable assignment in KornShell

Tags:

shell

ksh

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)

like image 968
Jorge Vega Sánchez Avatar asked Apr 11 '26 07:04

Jorge Vega Sánchez


1 Answers

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
like image 147
Charles Duffy Avatar answered Apr 17 '26 09:04

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!