0

I have a very peculiar issue with a script that I have wrote today. I am trying to form an ip address from two variables namely url and port. I am getting the url value from a library script which echos 10.241.1.8 and the port number is 10000. Now if I concatenate both the url and the port into another variable ip, I get completely a strange result(:10000241.1.8). I have my code and its result below. Please help me with your suggestions to fix this.

clear
echo $(date +'%H:%M:%S')'>> "Sample Records" Script started...'
usage() {
  echo ">> $ script.sh -ctoff 89 -env c -ns reporting -depPath /user/release/audit_prime_oozie"
  echo "Usage: $ script.sh -ctoff <Cutoff number> -env <testing cluster. ex: s for staging,c,d,p and a> -ns <optional: hive namespace> -depPath <deployment path>" 
}

# Function to validate if value of a parameter is not empty 
validate () {
if  [[ $flag != 1 ]]; then
  if [[ $tmpVar == *"-"* ]] || [[ -z $tmpVar ]]; then
    usage
    exit 1
  fi
fi
}

options=$@
if [[ -z $options ]]; then
usage
exit 1
fi
arguments=($options)
index=0

# Function to extract the parameter values
check (){
for x in $options
  do
    index=`expr $index + 1`
     case $x in
      -ctoff)
      cutOff="${arguments[index]}"
      tmpVar=$cutOff
      validate $tmpVar
      ;;
      -env)
      env="${arguments[index]}"
      tmpVar=$env
      validate $tmpVar
      ;;
      -ns)
      ns="${arguments[index]}"
      tmpVar=$ns
      validate $tmpVar
      ;;
      -depPath)
      depPath="${arguments[index]}"
      tmpVar=$depPath
      validate $tmpVar
      ;;
     esac

     if [[ -z $ns ]];then
        ns=reporting
     fi
  done
}

check $@

error_exit(){
  echo "$1" 1>&2
  exit 1
}

# Create the execution directory
user=$(id -u -n)
PWD=`pwd`
INSTALL_ROOT=$PWD
LOCAL_DIR="/tmp/$user/sample_duns"
if [[ ! -d $LOCAL_DIR ]]; then
  mkdir -p $LOCAL_DIR
  echo ">> Created local directory $LOCAL_DIR"
  if [[ $? -ne 0 ]]; then
    echo ">> Unable to create $LOCAL_DIR, writing to current folder $INSTALL_ROOT"
    LOCAL_DIR=$INSTALL_ROOT
  fi
fi
if [[ $(ls -A $LOCAL_DIR) ]]; then
  echo ">> Removed the temp files from $LOCAL_DIR"
  rm -r $LOCAL_DIR/*
fi

# create the file name
datestamp=$(date '+%Y%m%d%H')
outFile=sample_duns_$datestamp.txt

# Copy the contents from HDFS to Local directory
echo ">> Copying required files from HDFS"
hdfs dfs -copyToLocal $depPath/data-warehouse/config/server.properties $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
hdfs dfs -copyToLocal $depPath/data-warehouse/reporting/lib_getHiveServer2ip.sh $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
if [[ $? -ne 0 ]]; then
  echo ">> Files missing. Exiting now.."
  exit 1
fi

# Call the lib script to get appropriate hiveserver2 ip address from the supplied environment for beeline execution 
echo ">> Reading the HiveServer2 ip"
chmod +x $LOCAL_DIR/lib_getHiveServer2ip.sh
url=$($LOCAL_DIR/lib_getHiveServer2ip.sh $env $LOCAL_DIR/server.properties)
echo url=$url
port=10000
echo ip=$url:$b 

Here is my output from the terminal.

11:18:16>> "Sample Records" Script started...
>> Removed the temp files from /tmp/user/sample_duns
>> Copying required files from HDFS
>> Reading the HiveServer2 ip
url=10.241.1.8
:10000241.1.8

I am expecting the below result

ip=10.241.1.8:10000

Adding the lib_getHiveServer2ip.sh script below

. $2 # read properties file

if [[ $1 == "d" ]]; then
  ip=$devHSer
elif [[ $1 == "c" ]]; then
  ip=$crankHSer
elif [[ $1 == "s" ]]; then
  ip=$stgHSer
elif [[ $1 == "p" ]]; then
  ip=$prdHSer
elif [[ $1 == "a" ]]; then
  ip=$alpHSer
else
  echo ">> Invalid cluster ip encountered. Exiting now ..."
  exit 1
fi
echo $ip
2
  • Just a terminology note: 10.241.1.8 is an IP address; the combination of an IP address and a port 10.241.1.8:10000, doesn't really have a standard name, as it combines values used by two different protocols (the address is used by IP, the port by TCP or UDP). You might refer to it as a connection endpoint. Commented Jun 28, 2016 at 11:59
  • The first block of code is mostly irrelevant, only the last 7 lines matter. The last line echo ip=$url:$b references $b, but $b was not initialized in any of the code shown. Commented Jun 28, 2016 at 13:06

1 Answer 1

4

Your url variable contains a carriage return character for some reason. Check lib_getHiveServer2ip.sh for weirdness.

Pipe your echo output to hexdump to confirm.

Edit: looks like your properties file has bad line endings. Use the file utility to check.

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

3 Comments

Do you know what type of carriage return does it have?
@AlexRajKaliamoorthy, please use hexdump. hexdump shows the type of newline.
With the input from @Carl Norum I was able to find the carriage return '\r' in the url variable. I have included the below code to remove the carriage return. url=$( echo $url | tr -d '\r')

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.