shell变量中的正斜杠

时间:2017-05-22 20:40:46

标签: bash shell sh

我有一个shell脚本来构建一个cURL URL并检查状态。

    HOST_NAME=`hostname`
    PORT=`awk -F= '/SERVER_PORT/{print $2}' conf/mypros.properties`
    echo $HOST_NAME
    echo $PORT
    CURL_URL=http://$HOST_NAME:$PORT/api/vrtl/test
    echo $CURL_URL

输出

mytesthost
9993
/apisthost:9993

而不是

mytesthost
9993
http://mytesthost:9993/api/vrtl/test

当我把它变成:

CURL_URL="http://$HOST_NAME:$PORT/api/vrtl/test" it gives: mytesthost 9993 /api/vrtl/testost:9993

端口号输出正确如下:

[user@mytesthost]$ PORT=awk -F= '/SERVER_PORT/{print $2}' conf/myprops.properties [user@mytesthost]$ echo $PORT 9993

$echo $0
bash
$cat 1.sh
HOST_NAME=mytesthost
PORT=awk -F= '/SERVER_PORT/{print $2}' conf/myprops.properties
echo $HOST_NAME
echo $PORT
CURL_URL="http://$HOST_NAME:$PORT/api/vrtl/test"
echo $CURL_URL

$./1.sh
mytesthost
9993
/api/vrtl/testost:9993

ANSWER

添加tr命令解决了这个问题。

tr -d '\r' <conf/myprops.properties > conf/myprops_temp.properties
HOST_NAME=`hostname`
echo $HOST_NAME
PORT=`grep SERVER_PORT conf/myprops_temp.properties | cut -d= -f2`
echo $PORT
CURL_URL="http://$HOST_NAME:$PORT/api/vrtl/test"

0 个答案:

没有答案