shell脚本

时间:2016-06-28 11:31:37

标签: bash shell

我对今天写的脚本有一个非常奇怪的问题。我试图从两个变量形成一个IP地址,即url和port。我从一个库脚本获取url值echos 10.241.1.8和端口号是10000.现在,如果我将url和端口连接到另一个变量ip,我得到一个奇怪的结果(:10000241.1.8)。我的代码和结果如下。请帮我解决你的建议。

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 

这是终端的输出。

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

我期待以下结果

ip=10.241.1.8:10000

下面添加lib_getHiveServer2ip.sh脚本
. $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

1 个答案:

答案 0 :(得分:4)

由于某种原因,您的url变量包含回车符。检查lib_getHiveServer2ip.sh是否有异常。

echo输出标记为hexdump进行确认。

编辑:看起来您的属性文件有错误的行结尾。使用file实用程序进行检查。