program story

Bash 스크립트는 빈 줄에 "Command Not Found"를 인쇄합니다.

inputbox 2020. 8. 17. 08:53
반응형

Bash 스크립트는 빈 줄에 "Command Not Found"를 인쇄합니다.


bash scriptname.sh데비안의 명령 줄에서 사용하여 스크립트를 실행할 때마다 스크립트 Command Not found결과를 얻습니다 .

스크립트는 작동하지만 항상 Command Not Found빈 줄마다 화면에 명령문이 인쇄됩니다. 각 빈 줄은 명령을 찾을 수 없습니다.

/var폴더 에서 스크립트를 실행하고 있습니다 .

다음은 스크립트입니다.

#!/bin/bash

echo Hello World

다음을 입력하여 실행합니다.

bash testscript.sh

왜 이런 일이 발생합니까?


첫 번째 줄은 다음과 같습니다.

#!/bin/bash

그렇지 않은 경우 bash에 대한 경로를 입력하십시오. /bin/bash


다음을 실행 해보십시오.

dos2unix script.sh

그러면 줄 끝 등이 Windows에서 유닉스 형식으로 변환됩니다. 즉,이 행 끝에서 \ R (CR)이 그들을 변경 스트립 \r\n (CR+LF)\n (LF).

dos2unix명령 에 대한 자세한 정보 (맨 페이지)


파일이 dos / Win 형식인지 확인하는 또 다른 방법 :

cat scriptname.sh | sed 's/\r/<CR>/'

출력은 다음과 같습니다.

#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>

그러면 파일의 <CR>\r문자에 대해 표시된 전체 파일 텍스트가 출력 됩니다 .


bash -x scriptname.sh추적하는 데 사용할 수 있습니다 .


비슷한 문제가 발생했습니다. 문제는 권한 인 것 같습니다. 를 수행하면 ls -l파일에 실행 비트가 켜져 있지 않을 수 있음을 식별 할 수 있습니다. 이렇게하면 스크립트가 실행되지 않습니다. :)

@artooro가 주석에 추가했듯이 :

이 문제를 해결하려면 실행 chmod +x testscript.sh


스크립트가 작업을 (상대적으로) 잘 수행하면 정상적으로 실행되고있는 것입니다. 문제는 경로에 없거나 설치되지 않았거나 철자가 틀렸거나 유사한 프로그램을 참조하는 파일의 한 줄일 수 있습니다.

한 가지 방법은 set -x스크립트의 맨 위에 를 배치 하거나 bash -x대신 실행하는 bash것입니다. 그러면 실행하기 전에 행이 출력되며 일반적으로 문제의 원인을 확인하려면 오류 직전에 명령 출력을 확인하면됩니다.

말했듯이 문제를 일으키는 빈 줄이 있다면 그 안에 무엇이 있는지 실제로 확인하고 싶을 것입니다. 운영:

od -xcb testscript.sh

CTRL-MWindows 유형 편집기를 사용하여 얻을 수 있는 (캐리지 리턴) 과 같은 "보이지 않는"재미있는 문자가 없는지 확인하십시오 .


이것은 사소하고 OP의 질문과 관련이 없을 수 있지만 스크립팅을 배울 때 처음에는 종종 실수했습니다.

VAR_NAME = $(hostname)
echo "the hostname is ${VAR_NAME}"  

그러면 '명령을 찾을 수 없음'응답이 생성됩니다. 올바른 방법은 공백을 제거하는 것입니다.

VAR_NAME=$(hostname)

dos2unix스크립트 파일에서 사용 하십시오.


실행하려면 예를 들어 전체 경로를 제공해야합니다.

/home/Manuel/mywrittenscript

Notepad ++가 있고 .sh 오류 메시지 : "command not found" 또는이 autoconf 오류 메시지 "line 615 : ../../autoconf/bin/autom4te : No such file or directory"가 표시되는 경우 .

메모장 ++에서 편집 -> EOL 변환으로 이동 한 다음 Macinthos (CR) 을 확인하십시오 . 그러면 파일이 편집됩니다. 곧 이러한 오류가 발생하므로이 명령으로 모든 파일을 확인하는 것이 좋습니다.


시험 chmod u+x testscript.sh

나는 여기에서 그것을 알고있다 : http://www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/


Windows 용 Bash에서 실행을 잘못 시도했습니다.

run_me.sh 

without ./ at the beginning and got the same error.

For people with Windows background the correct form looks redundant:

./run_me.sh

Had the same problem. Unfortunately

dos2unix winfile.sh
bash: dos2unix: command not found

so I did this to convert.

 awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh

and then

bash unixfile.sh

Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:

./run_me.sh \ 
--with-some parameter

(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.


I was also having some of the Cannot execute command. Everything looked correct, but in fact I was having a non-breakable space &nbsp; right before my command which was ofcourse impossible to spot with the naked eye:

if [[ "true" ]]; then
  &nbsp;highlight --syntax js "var i = 0;"
fi

Which, in Vim, looked like:

if [[ "true" ]]; then
  highlight --syntax js "var i = 0;"
fi

Only after running the Bash script checker shellcheck did I find the problem.


I ran into this today, absentmindedly copying the dollar command prompt $ (ahead of a command string) into the script.


Add the current directory ( . ) to PATH to be able to execute a script, just by typing in its name, that resides in the current directory:

PATH=.:$PATH

You may want to update you .bashrc and .bash_profile files with aliases to recognize the command you are entering.

.bashrc and .bash_profile files are hidden files probably located on your C: drive where you save your program files.

참고URL : https://stackoverflow.com/questions/7362504/bash-script-prints-command-not-found-on-empty-lines

반응형