脚本包含 Windows 格式的回车换行符(\r\n),而在 Linux 中应该使用 Unix 格式的换行符(\n)。这导致 bash 将 \r 解释为命令的一部分而报错。
./mmiot_ecdsa_sign.sh: line 2: $'\r': command not found
./mmiot_ecdsa_sign.sh: line 4: $'\r': command not found
./mmiot_ecdsa_sign.sh: line 5: syntax error near unexpected token `$'\r''
'/mmiot_ecdsa_sign.sh: line 5: `function CheckStop()
修复方法1:直接转换文件格式
# 使用 sed 命令去除 \r 字符sudo sed -i 's/\r//' ./mmiot_ecdsa_sign.sh
# 或者使用 dos2unix 工具(如果已安装)sudo dos2unix ./mmiot_ecdsa_sign.sh
修改后执行成功:
sudo bash ./mmiot_ecdsa_sign.sh gen_ecdsa_key prikey_filepath pubkey_filepath
gen_ecdsa_key succ prikey_path:prikey_filepath pubkey_path:pubkey_filepath
方法2:修改脚本为正确的 Unix 格式
这是修复后的脚本内容,已经去除了 Windows 格式的行尾符:
#!/bin/sh
time_stamp=`date +%s`
function CheckStop(){if [ $? -ne 0 ]; thenecho "execute fail, error on line_no:"$1" exit!!!"exitfi}
function GenEcdsaKey(){ec_param_file_path="/tmp/ec_param.pem."$time_stampopenssl ecparam -out $ec_param_file_path -name prime256v1 -genkey CheckStop $LINENOopenssl genpkey -paramfile $ec_param_file_path -out $1 CheckStop $LINENOopenssl pkey -in $1 -inform PEM -out $2 -outform PEM -puboutCheckStop $LINENOrm $ec_param_file_pathecho "gen_ecdsa_key succ prikey_path:"$1" pubkey_path:"$2}
function GenEcdsaSign(){ec_sign_info_file="/tmp/ec_sign_info_file."$time_stampec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stampec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stampecho -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENOopenssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEMCheckStop $LINENOopenssl base64 -e -in $ec_binary_sign_file -out $4CheckStop $LINENOrm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_fileecho "gen_ecdsa_sign succ sign_file_path:"$4}
function VerifyEcdsaSign(){ec_sign_info_file="/tmp/ec_sign_info_file."$time_stampec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stampec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stampecho -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENOopenssl base64 -d -in $4 -out $ec_binary_sign_file CheckStop $LINENOopenssl pkeyutl -verify -in $ec_sign_info_sha256 -sigfile $ec_binary_sign_file -pubin -inkey $3 -keyform PEM rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file}
function Usage(){echo "Usage:"echo "mmiot_ecdsa_sign.sh gen_ecdsa_key echo "mmiot_ecdsa_sign.sh gen_ecdsa_sign echo "mmiot_ecdsa_sign.sh verify_ecdsa_sign }
if [[ $# -eq 3 && $1 = "gen_ecdsa_key" ]];thenGenEcdsaKey $2 $3
elif [[ $# -eq 5 && $1 = "gen_ecdsa_sign" ]];thenGenEcdsaSign $2 $3 $4 $5
elif [[ $# -eq 5 && $1 = "verify_ecdsa_sign" ]];thenVerifyEcdsaSign $2 $3 $4 $5elseecho "------------------- Invalid Args !!! -------------------"Usagefi
或者其它方法:安装 dos2unix 工具(可选)
如果你想以后更方便地处理这类问题,可以安装 dos2unix 工具:
# Ubuntu/Debiansudo apt-get install dos2unix
# CentOS/RHELsudo yum install dos2unix
# 使用sudo dos2unix ./mmiot_ecdsa_sign.sh
修复后,脚本应该能正常运行了。