Shell脚本:命令不是" foundne",但在终端上运行良好

时间:2016-08-12 06:20:24

标签: bash shell

我是shell脚本的新手,我创建了一个sh文件,它在终端上运行良好,请参阅下面的代码:

#!/bin/bash
# Use dmidecode to get version
dmidecode=`dmidecode -t chassis | grep 'Version' | sed -r 's/.*(.{6})/\1/'`  
# Use ipmitool to get version
fru=`ipmitool fru | grep 'Chassis Part Number' | sed -r 's/.*(.{6})/\1/'`
# Compare the result
compare_result=0 
if [ "$dmidecode" == "$fru" ]; then 
   compare_result="pass"
else    
   compare_result="false"
fi
# Create json
printf '"tcresult": {"dmidecode":{"chassis_type":"%s"},"fru":{"chassis_type":"%s"},"compare_result":"%s"}\n' "$dmidecode" "$fru" "$compare_result"

结果是:

 "tcresult": {"dmidecode":{"chassis_type":"N42.12"},"fru":{"chassis_type":"N42.12"},"compare_result":"pass"}

但是,当我执行sh文件时,错误显示如下:

  [root@localhost ~]# cd Desktop/
  [root@localhost Desktop]# ls
  avms  avms.tar  check_chasis.sh
  [root@localhost Desktop]# sh check_chasis.sh
  : command not foundne 3:
  : command not foundne 7:
   check_chasis.sh: line 15: syntax error: unexpected end of file

提前感谢任何建议或评论。另见下面的截图

enter image description here

1 个答案:

答案 0 :(得分:4)

“foundne”消息是由于您有一个额外的CR(回车),然后是第3行和第7行的开头的空格。shell尝试执行该CR导致错误消息:

check_chasis.sh: line 3: \r : command not found

显示为:

: command not foundne 3: 

删除它:

tr -d '\r' < check_chasis.sh > check_chassis.bash

请注意dos2unix无法解决此问题,除非与-c mac选项一起使用,这相当于运行mac2unix