bash 运行shell脚本时“找不到命令”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43771597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:07:07  来源:igfitidea点击:

"command not found" when running shell script

bashshellunix

提问by Axel Finkel

Here is the simple script.shfile I have:

这是script.sh我拥有的简单文件:

#!/bin/bash
pwd
date
ls -lg

I have used chmod +xto make the file executable, but when running it with ./script.shI get command not found: pwdcommand not found: dateand only the ls -lgcommand works. I'm a bit perplexed as to why the first two commands aren't working because when I type them into the command terminal they work the way they should.

我曾经chmod +x使文件可执行,但是当./script.sh我使用 get运行它时command not found: pwdcommand not found: date,只有ls -lg命令有效。我有点困惑为什么前两个命令不起作用,因为当我在命令终端中输入它们时,它们会以应有的方式工作。

回答by Jamil Said

Maybe there is a (invisible) character there that should not be there. Try typing it again from scratch in the environment that it will run (ex: Linux) or use this command od -c /folder/script.shto reveal those pesky characters.

也许那里有一个(隐形)角色不应该在那里。尝试在它将运行的环境(例如:Linux)中从头开始再次输入它,或者使用此命令od -c /folder/script.sh来显示那些讨厌的字符。

回答by Michael K?fer

Also do not use a variable called PATH (cause it is already used :)

也不要使用名为 PATH 的变量(因为它已经被使用了 :)