6

This question already has an answer here:

I'm using following script on linux server. And which is giving:

: not found [No such file or directory]

This is the script:

# ------------------------------------------------------------------
# K.Sugunan Host health check
#           Description
#
#           This script to initiate health check on servers.
#           It will check for CPU, memory and some connectivity with 
#           up stream and down stream system.
#
# ------------------------------------------------------------------

# Applying initial variables and loading config parameters
DOCROOT=$(pwd)"/"
TIME_NOW=$(date +"%Y-%m-%d %H:%M:%S")

echo $DOCROOT

. $DOCROOT"config/main.sh"
. $DOCROOT"config/web.sh"
. $DOCROOT"config/telnet.sh"

echo $CPU_A

The error will be like follows:

: not found [No such file or directory]
: not found [No such file or directory]
/home/sugunan/phobos/
: not found [No such file or directory]
: cannot open [No such file or directory]os/

I use following ways to run the script

ksh poc.sh
sh poc.sh
./poc.sh

All of the above giving similar error. Even I include #!/bin/bash still the error exist. I have tried #!/bin/ksh and #!/usr/bin/ksh also. All of that gives the error. What may be the reason for the issue on this server?

marked as duplicate by Gilles bash Aug 19 '15 at 21:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Just add at the beginning of your script set -x (and better add back the #!/bin/sh at the real beginning). This little command (I think it works also for ksh, for sure with bash) will display what it will execute and then display the execution output (if any). It is really handy to spot any mistakes. You might have to add it to your other scripts if the error comes from one of them. – Huygens Aug 19 '15 at 12:06
  • Add #!/bin/bash as first line and start script this way: ./poc.sh – Cyrus Aug 19 '15 at 12:08
7

It appears like the empty lines contain an invisible (unprintable) character, and the server is trying to run it as if it were the name of a command; hence the error : not found [No such file or directory]. Delete the empty lines or ensure they're really empty.

Edit: OP's comment confirms that it is indeed the case, as the code was written under Windows. On Windows, ASCII text files end each line with a Line Feed + Carriage Return character, while UNIX uses only a Line Feed.

To solve this, you can either:

  • copy & paste the code directly on an editor under Linux
  • make sure you set your Windows editor to convert end-of-line characters to the UNIX standard
  • use the dos2unix command to convert the Windows ASCII file to a UNIX one
  • 7
    high probability that the script file contains \r\n line endings. – glenn jackman Aug 19 '15 at 12:30
  • 1
    You are correct. I use notepad++ on windows to edit the code. Which make the issue. Now I copy the code and place on linux vi editor. Thanks for suggestion. "dr01" may edit the answer accordingly. Then can mark as correct answer. – sugunan Aug 19 '15 at 14:20
  • Answer edited to add a bit more info. – dr01 Aug 19 '15 at 14:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.