我有两个名字不同的类似脚本。一个很好,但另一个抛错了。谁能告诉我有什么问题吗?
这是我的test.sh脚本,运行良好
[nnice@myhost Scripts]$ cat test.sh
#!/bin/bash
function fun {
echo "`hostname`"
}
fun
[nnice@myhost Scripts]$ ./test.sh
myhost.fedora
这是我的另一个脚本demo.sh,但是它会引发错误
[nnice@myhost Scripts]$ cat demo.sh
#!/bin/bash
function fun {
echo "`hostname`"
}
fun
[nnice@myhost Scripts]$ ./demo.sh
bash: ./demo.sh: cannot execute: required file not found
具有相同权限的两个脚本
[nnice@myhost Scripts]$ ll test.sh
-rwxr-xr-x. 1 nnice nnice 65 Oct 21 10:47 test.sh
[nnice@myhost Scripts]$ ll demo.sh
-rwxr-xr-x. 1 nnice nnice 58 Oct 21 10:46 demo.sh
发布于 2022-10-21 05:52:41
您的demo.sh
脚本是DOS文本文件。这样的文件有CRLF行的结尾,行尾的额外CR (回车)字符会引起问题。
它引起的具体问题是,#!
-line上的解释器路径名现在指的是名为/bin/bash\r
的东西( \r
表示回车,这是一个类似空格的字符,因此通常是不可见的)。找不到这个文件,所以这就是导致您的错误消息的原因。
要解决这个问题,请将脚本从DOS文本文件转换为Unix文本文件。如果要在Windows上编辑脚本,则可以通过配置Windows编辑器来创建Unix文本文件,但也可以使用dos2unix
实用程序,这对于大多数常见的Unix变体都是可用的。
$ ./script
bash: ./script: cannot execute: required file not found
$ dos2unix script
$ ./script
harpo.local
关于您的代码:请不要通过echo `some-command`
或echo $(some-command)
输出some-command
的输出。只需直接使用命令:
#!/bin/sh
fun () {
hostname
}
fun
(由于该脚本现在不使用任何需要bash
的内容,所以我也转向调用更简单的/bin/sh
shell。)
发布于 2023-02-27 09:38:43
在我的例子中,在Windows上,这是由我试图使用这个shebang #! /user/bin/bash
而不是#!/bin/bash
执行的bash脚本造成的。
https://unix.stackexchange.com/questions/721844
复制相似问题