1

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"

And the outcome is:

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

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

enter image description here

Samoth
  • 716
  • 15
  • 34
  • 3
    Since you are using bash specific syntax you'll need to run the script with `bash scriptname`, not `sh scriptname`. Btw, the `==` is the only bashism here. Replace it by `=` – hek2mgl Aug 12 '16 at 06:41
  • 2
    Additionally, your error messages suggest that your file at least in part contains `\r` characters (probably Windows-style CRLF line endings); make sure that the file only contains LF-only (`\n`) line endings. – mklement0 Aug 12 '16 at 06:45
  • @hek2mgl, thanks, but after revising the 2 issues, the same error still occurs. – Samoth Aug 12 '16 at 06:47
  • @mklement0, thanks, but can you explain more detailed? I am not quite so familiar with bash. – Samoth Aug 12 '16 at 06:54
  • Please (after making the changes requested above), either copy-and-paste your program, and the output you get, **exactly**, or provide a link to a screenshot. I don't think you really get the error message *command no **foundne*** from your shell, as this is (grammatically) nonsense. – user1934428 Aug 12 '16 at 07:15
  • @user1934428: thanks, and I really get the error message command no foundne from my shell. That's why I am so confused. – Samoth Aug 12 '16 at 07:18
  • 1
    @Samotht: Convert your file to Unix line endings - see [this answer](http://stackoverflow.com/a/30155649/45375) of mine. – mklement0 Aug 12 '16 at 07:25
  • @Samotht, I do not understand that you say "it works well on the terminal" first, and later you say "when I execute the sh file". Do you not execute the same script in both cases? Where anywhere else do you execute the script? – Jdamian Aug 12 '16 at 07:37
  • Finally solved the issue using `dos2unix` to covert the file. Thanks for the all valuable advise and suggestions. – Samoth Aug 13 '16 at 00:57
  • @Samotht I'm surprised dos2unix worked in that case. Did you also try the suggestion in my answer (`tr -d`)? Didn't you actually use `mac2unix` or `dos2unix -c mac`? – jlliagre Aug 13 '16 at 08:37

1 Answers1

5

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

which is displayed as:

: command not foundne 3: 

Remove it with:

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

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

jlliagre
  • 29,783
  • 6
  • 61
  • 72