Support us and view this ad

可选:点击以支持我们的网站

免费文章

脚本包含 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 ]; then
echo "execute fail, error on line_no:"$1" exit!!!"
exit
fi
}

function GenEcdsaKey()
{
ec_param_file_path="/tmp/ec_param.pem."$time_stamp
openssl ecparam -out $ec_param_file_path -name prime256v1 -genkey
CheckStop $LINENO
openssl genpkey -paramfile $ec_param_file_path -out $1
CheckStop $LINENO
openssl pkey -in $1 -inform PEM -out $2 -outform PEM -pubout
CheckStop $LINENO
rm $ec_param_file_path
echo "gen_ecdsa_key succ prikey_path:"$1" pubkey_path:"$2
}

function GenEcdsaSign()
{
ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp
ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp
ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp
echo -n "$1"_"$2" > $ec_sign_info_file
openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file
CheckStop $LINENO
openssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEM
CheckStop $LINENO
openssl base64 -e -in $ec_binary_sign_file -out $4
CheckStop $LINENO
rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file
echo "gen_ecdsa_sign succ sign_file_path:"$4
}

function VerifyEcdsaSign()
{
ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp
ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp
ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp
echo -n "$1"_"$2" > $ec_sign_info_file
openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file
CheckStop $LINENO
openssl base64 -d -in $4 -out $ec_binary_sign_file
CheckStop $LINENO
openssl 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" ]];then
GenEcdsaKey $2 $3

elif [[ $# -eq 5 && $1 = "gen_ecdsa_sign" ]];then
GenEcdsaSign $2 $3 $4 $5

elif [[ $# -eq 5 && $1 = "verify_ecdsa_sign" ]];then
VerifyEcdsaSign $2 $3 $4 $5
else
echo "------------------- Invalid Args !!! -------------------"
Usage
fi

或者其它方法:安装 dos2unix 工具(可选)

如果你想以后更方便地处理这类问题,可以安装 dos2unix 工具:

# Ubuntu/Debian
sudo apt-get install dos2unix

# CentOS/RHEL
sudo yum install dos2unix

# 使用
sudo dos2unix ./mmiot_ecdsa_sign.sh

修复后,脚本应该能正常运行了。

 


登陆