Shell脚本:命令不是"foundne",但在终端上运行良好 [英] Shell script: command not "foundne", but works well on terminal

查看:75
本文介绍了Shell脚本:命令不是"foundne",但在终端上运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to shell script, I created a sh file and it works well on the terminal, see the code below:

#!/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文件时,错误显示如下:

However, when I execute the sh file, the error shows below:

  [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

在此先感谢您提供任何建议或评论.另请参见下面的屏幕截图

Thanks in advance for any advise or comment. Also see screenshot below

推荐答案

出现"foundne"消息是由于您有一个额外的CR(回车符),并在其后加一个空格 第3行和第7行中的内容.shell尝试执行该CR,导致出现错误消息:

The "foundne" message is due to the fact you have an extra CR (carriage return) followed by a space at the beginning of the lines 3 and 7. The shell tries to execute that CR leading to the error message:

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

显示为:

: command not foundne 3: 

使用以下方法删除它:

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

请注意,除非与与运行 mac2unix 等效的 -c mac 选项一起使用,否则 dos2unix 无法解决此问题.

Note that dos2unix cannot fix this issue unless used with the -c mac option which is equivalent to running mac2unix.

这篇关于Shell脚本:命令不是"foundne",但在终端上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆