bash - Issue in echo statement in shell scripting -
i have peculiar issue script have wrote today. trying form ip address 2 variables namely url , port. getting url value library script echos 10.241.1.8 , port number 10000. if concatenate both url , port variable ip, strange result(:10000241.1.8). have code , result below. please me suggestions 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 staging,c,d,p , a> -ns <optional: hive namespace> -deppath <deployment path>" } # function validate if value of parameter not empty validate () { if [[ $flag != 1 ]]; if [[ $tmpvar == *"-"* ]] || [[ -z $tmpvar ]]; usage exit 1 fi fi } options=$@ if [[ -z $options ]]; usage exit 1 fi arguments=($options) index=0 # function extract parameter values check (){ x in $options 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 execution directory user=$(id -u -n) pwd=`pwd` install_root=$pwd local_dir="/tmp/$user/sample_duns" if [[ ! -d $local_dir ]]; mkdir -p $local_dir echo ">> created local directory $local_dir" if [[ $? -ne 0 ]]; echo ">> unable create $local_dir, writing current folder $install_root" local_dir=$install_root fi fi if [[ $(ls -a $local_dir) ]]; echo ">> removed temp files $local_dir" rm -r $local_dir/* fi # create file name datestamp=$(date '+%y%m%d%h') outfile=sample_duns_$datestamp.txt # copy contents hdfs local directory echo ">> copying required files hdfs" hdfs dfs -copytolocal $deppath/data-warehouse/config/server.properties $local_dir || error_exit "cannot copy files hdfs! exiting now.." hdfs dfs -copytolocal $deppath/data-warehouse/reporting/lib_gethiveserver2ip.sh $local_dir || error_exit "cannot copy files hdfs! exiting now.." if [[ $? -ne 0 ]]; echo ">> files missing. exiting now.." exit 1 fi # call lib script appropriate hiveserver2 ip address supplied environment beeline execution echo ">> reading 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 output terminal.
11:18:16>> "sample records" script started... >> removed temp files /tmp/user/sample_duns >> copying required files hdfs >> reading hiveserver2 ip url=10.241.1.8 :10000241.1.8
i expecting below result
ip=10.241.1.8:10000
adding lib_gethiveserver2ip.sh script below
. $2 # read properties file if [[ $1 == "d" ]]; ip=$devhser elif [[ $1 == "c" ]]; ip=$crankhser elif [[ $1 == "s" ]]; ip=$stghser elif [[ $1 == "p" ]]; ip=$prdhser elif [[ $1 == "a" ]]; ip=$alphser else echo ">> invalid cluster ip encountered. exiting ..." exit 1 fi echo $ip
your url
variable contains carriage return character reason. check lib_gethiveserver2ip.sh
weirdness.
pipe echo
output hexdump
confirm.
edit: looks properties file has bad line endings. use file
utility check.
Comments
Post a Comment