sh脚本从git拉取后无法工作

wswtfjt7  于 2023-05-21  发布在  Git
关注(0)|答案(1)|浏览(252)

我已经创建了几个shell脚本,这有助于执行数据库部署活动。那些.sh和.properties文件工作得非常好。然而,我已经将这些文件推送到git仓库,并拉取了相同的文件,并试图运行,但它抛出了一些未知的错误。虚拟机正在运行Linux RHEL 7.5版本。
同样供参考,在推送到git之前,相同的.sh和.properties文件在VM上运行良好。
有人能告诉我们是否有任何git编辑器到VM Vi编辑器的兼容性问题吗?
尝试使用Notepad++、Notepad、VS Code打开文件,并推回到git,从git拉回到VM。但没有用。还尝试通过电子邮件发送文件(.sh和.properties),通过WinSCP复制后访问-这工作正常

Shell脚本初始部分*****

文件名--> test.sh

#########Properties file #############
set -e
properties_file="$1"
     echo `date "+%m/%d/%Y %H:%M:%S"` ": $properties_file"
if [ -f "$properties_file" ]
then
     echo `date "+%m/%d/%Y %H:%M:%S"` ": Property file \"$properties_file\" found."
    while IFS='=' read -r key value
    do
        key=$(echo $key | tr '.' '_')
         eval ${key}=\${value}
   done < "$properties_file"
else
     echo `date "+%m/%d/%Y %H:%M:%S"` ": Property file not found, Pass property file name as argument."
fi

还假设test.properties是属性文件
在VM上运行:

-bash-4.2$sh test.sh test.properties  
ERROR Message:
: invalid optionloy.sh: line 2: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
rhfm7lfc

rhfm7lfc1#

这里的问题可以是新行字符。更具体地说,Windows DOS使用“\r\n”,Linux使用“\n”行结尾。当你将文件推送到git时,“\n”被替换为“\r\n”。因此,您需要用“\n”删除“\r\n”。为此,您可以使用“-sed命令$ sed -i 's/\r$//' file

相关问题