2009-11-19 55 views

回答

273

如果在PATH系統變量中找不到給定命令,並且它不是內置shell命令,則返回值爲127的/bin/sh。換句話說,系統不理解你的命令,因爲它不知道在哪裏找到你想要調用的二進制文件。

+24

如果bash腳本沒有模式「+ x」但確實存在,也會發生這種情況。 – MatthewKremer 2014-03-04 20:48:02

+0

您可以嘗試使用'which [程序]'來查看操作系統正在使用哪個二進制文件。如果它變空,下一步是檢查執行位和PATH。 – cr125rider 2014-06-12 16:33:58

+7

@ cr125rider,''不是特別準確 - 它不知道別名,shell函數,PATH查找記憶或shell狀態內部的其他因素。使用'type'更好,這是一個瞭解所有這些事情的shell內建函數。 – 2014-09-11 22:45:13

6

它沒有特殊的意義,比過去的進程退出與127

但是,它也被用於慶典的退出狀態這樣做(假設你使用bash作爲一個殼)等告訴你你試圖執行的命令不能執行(即找不到它)。不幸的是,如果進程退出狀態爲127,或者無法找到,那麼不會立即推論出來。

編輯:
不能立即推論,除了在控制檯上的輸出,但這是堆棧溢出,所以我假設你在腳本中這樣做。

6

shell約定是成功的可執行文件應該以0值退出。其他任何情況都可以解釋爲某種類型的故障,或者是您剛剛運行的bash或可執行文件的一部分。另見$ PIPESTATUS和bash的手冊頁的退出狀態部分:

For the shell’s purposes, a command which exits with a zero exit status has succeeded. An exit status 
    of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a 
    fatal signal N, bash uses the value of 128+N as the exit status. 
If a command is not found, the child process created to execute it returns a status of 127. If a com- 
    mand is found but is not executable, the return status is 126. 

    If a command fails because of an error during expansion or redirection, the exit status is greater than 
    zero. 

    Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error 
    occurs while they execute. All builtins return an exit status of 2 to indicate incorrect usage. 

    Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in 
    which case it exits with a non-zero value. See also the exit builtin command below. 
3

127 - command not found

例如:$ CAAT 錯誤消息將

bash:

CAAT:找不到命令

現在你檢查使用echo $?

+0

非常好的例子! – Victor 2018-02-02 14:57:55

30

一般它是指:

127 - 找不到

但命令時,它也意味着命令發現,
但是需要命令未找到

1

如果您嘗試使用腳本語言運行程序,則可能需要包含要執行的文件的完整路徑。例如:

exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css'); 
+0

謝謝,這對我很有用。所以我做了哪些gs,然後在我的腳本中使用了輸出路徑。工作.. – Juan 2017-08-02 16:14:56

0

這個錯誤也有時候是騙人的。它說,即使文件確實存在,文件也沒有找到。這可能是由於您使用的編輯器可能導致文件中存在無效的不可讀特殊字符。在這種情況下,這個鏈接可能會幫助你。

-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory

,以找出是否是此問題的最好的辦法是簡單的地方,在整個文件中的echo語句,如果同樣的錯誤被拋出驗證。