首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shell脚本中echo语句中的问题

shell脚本中echo语句中的问题
EN

Stack Overflow用户
提问于 2016-06-28 11:31:37
回答 1查看 76关注 0票数 0

我今天写的剧本有一个很特别的问题。我试图从两个变量,即url和port,形成一个ip地址。我从一个库脚本中获取url值,该脚本回声10.241.1.8,端口号为10000。现在,如果我将url和端口连接到另一个变量ip中,就会得到一个完全奇怪的结果(:10000241.1.8)。下面是我的代码和结果。请帮我解决这个问题。

代码语言:javascript
复制
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 

这是我的终端输出。

代码语言:javascript
复制
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

我期待下面的结果

代码语言:javascript
复制
ip=10.241.1.8:10000

添加下面的lib_getHiveServer2ip.sh脚本

代码语言:javascript
复制
. $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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-28 11:33:46

由于某种原因,您的url变量包含一个回车字符。检查lib_getHiveServer2ip.sh的怪异之处。

将您的echo输出输送到hexdump以进行确认。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38075059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档