The newline problem of script running in linux under windows

#!/bin/bash
NOWDATE=`date +%Y%m%d`
LOG_DIR="/data/logs/nginx"
LOG_PATH=${LOG_DIR}${NOWDATE}
echo "date:$NOWDATE"
echo "LOG_DIR:$ LOG_DIR"
echo "LOG_PATH:"
echo ${LOG_PATH}

Simple code, the output is not what I want
date: 20160708
LOG_DIR:/data/logs/nginx
LOG_PATH:
20160708gs/nginx

View with sh -x nginxstatic.sh
++ date +%Y%m%d
+ NOWDATE=$'20160708\r'
+ LOG_DIR=$'/data/logs/nginx\r'
+ LOG_PATH=$'/data/logs/nginx\r20160708\r\r'
+ echo $'date:20160708\r\r'
date:20160708
+ echo $'LOG_DIR:/data/logs/nginx\r\r'
LOG_DIR: /data/logs/nginx
+ echo $'LOG_PATH:\r'
LOG_PATH:
+ echo -e $'/data/logs/nginx\r20160708\r\r\r'
20160708gs/nginx

It turns out that the script was written under Windows as the newline character \r\n, while the linux newline character is \n \r is to return to the beginning of the line, so there will be such a problem.
You can later convert the file with the dos2unix command before executing the windows file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326541461&siteId=291194637