0

I have removed parentheses, but still I was not able to fetch ENV_NODE value in second function scpTAR. Please let me know what is wrong.

set -x  
MASTER_HOSTNAME=`hostname | cut -d . -f1`  
TARGET_ENVIRONMENT = it  
evaluateEnvProp(){  
  if [ ${TARGET_ENVIRONMENT} = it ]; then  
    ENV_NAME=it && ENV_NODE=1cf62108e084   
  fi  
}  

scpTAR() {  
  echo ENV_NODE  
  echo ${ENV_NODE}  
  if [ ENV_NODE = ${MASTER_HOSTNAME} ] ; then  
    echo "scpTAR ENV_NODE = ${MASTER_HOSTNAME} " 
  else   
    "echo 'scpTAR ssh other node than jenkins server ENV_NODE=${MASTER_HOSTNAME}'"    
  fi       
}   

main(){    
  scpTAR    
}    

main
2
  • 2
    Variables are by default global variables. Commented Jan 3, 2018 at 8:58
  • I am using ${ENV_NAME} in scpTAR function but its corresponding value which we set as "it" in evalEnvProp function is not getting values as "it" where am i getting wrong Commented Jan 3, 2018 at 9:14

2 Answers 2

3

As @cyrus said, variables are global by default. What you did is setting the variables in a subshell:

( ENV_NAME=it && ENV_NODE=xyx && ENV_WLS_DOMAIN=user1 && ENV_NODE_PATH=path )

Because of that, these are gone (not propagated to calling script's environment) once the subshell exists. This is why you do not see their values set in scpTAR function. Remove the parentheses and your code should start working.

Update

Updated version of your code (based on answer by itChi) has another major error. You put spaces around the assignment operator when setting TARGET_ENVIRONMENT = it. This syntax is invalid and as a result TARGET_ENVIRONMENT is not assigned the specified value, thus the condition inside evaluateEnvProp function evaluates to false and ENV_NODE variable is not being set. Removing the spaces should solve the problem. You also did not call evaluateEnvProp as pointed out in update to @itChi's answer.

I'd highly recommend that you start using ShellCheck to verify correctness of your scripts.

Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned, variables are global by default, so if you reference them in your scpTAR function, you will get a return value.

However, as a second method, should you wish, you can reference it like so:

scpTAR $ENV_NAME $ENV_NODE $ENV_WLS_DOMAIN $ENV_NODE_PATH

Then in your scpTAR function reference them as:

echo "$1 $2 $3 $4"
it xyx user1 path

Particularly useful should you wish to run code on another machine, run a remote script, or set your script up to pass variables as arguments from bash.

EDIT:

Sorry if I tread on someone's toes, but here is your answer without subshell:

evalEnvProp(){
 if [ ${TARGET_ENVIRONMENT} = "it" ]; then

 ENV_NAME=it
 ENV_NODE=xyx
 ENV_WLS_DOMAIN=user1
 ENV_NODE_PATH=path
 fi
 }  
scpTAR() {  
  echo $ENV_NODE_PATH
}

main(){  
  evalEnvProp   
  scpTAR   
}  
main

Update2:

#!/bin/bash

set -x
MASTER_HOSTNAME=`hostname | cut -d . -f1`
TARGET_ENVIRONMENT=it
evaluateEnvProp(){
if [ ${TARGET_ENVIRONMENT} = "it" ]; then
ENV_NAME=it && ENV_NODE=1cf62108e084
fi
}

scpTAR() {
echo ENV_NODE
echo ${ENV_NODE}
if [ ENV_NODE == ${MASTER_HOSTNAME} ] ; then
echo "scpTAR ENV_NODE = ${MASTER_HOSTNAME} " else
"echo 'scpTAR ssh other node than jenkins server >ENV_NODE=${MASTER_HOSTNAME}'"

fi

}
main(){
evaluateEnvProp
scpTAR
}
main
  1. MASTER_HOSTNAME needs `` for the command. you are calling hostname. You can also accomplish this with $()
  2. Spacing in the IF statement either side of the = sign. otherwise you are not evaluating, you are setting a variable.
  3. In your edit you missed out a function call to evaluateEnvProp which failed because you didn't set the variable.

3 Comments

i have removed paranthesis.But still i couldnt able to fetch ENV_NODE value in second function scpTAR set -x MASTER_HOSTNAME=hostname | cut -d . -f1 TARGET_ENVIRONMENT=it evaluateEnvProp(){ if [ ${TARGET_ENVIRONMENT}=it ]; then ENV_NAME=it && ENV_NODE=1cf62108e084 fi } scpTAR() { echo ENV_NODE if [ ENV_NODE==${MASTER_HOSTNAME} ] ; then echo "scpTAR ENV_NODE = ${MASTER_HOSTNAME} " else "echo 'scpTAR ssh other node than jenkins server ENV_NODE=${MASTER_HOSTNAME}'" fi } main(){ scpTAR } main
@sudhir please format this properly so that we can help you.
This code is not very clear, for example you need spaces when using if [ ${TARGET_ENVIRONMENT}=it ] otherwise you are just setting TARGET_ENVIRONMENT Variable. Can you edit your post with your full code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.