我有一个像这样的文件
#!/bin/bash
find . -type f -exec chmod 644 {} \;
find . -type f -exec chown vagrant:www-data {} \;
find . -type d -exec chmod 755 {} \;
find . -type d -exec chown vagrant:www-data {} \;让我们假设它名为foo.sh,我在Ubuntu14.04机器上,在执行它之前我有根权限sudo su。
如果我调用sh foo.sh,命令行告诉我:
# sh foo.sh
: not foundh: 2: foo.sh:
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
: not foundh: 7: foo.sh: 但是,当我直接从命令行运行这4个命令时,它就能工作了。问题是:怎么了?为什么它抱怨第2行和第7行(它们是空的)?
谢谢(:
发布于 2015-01-05 00:49:18
多亏了@fejese的帮助,我才得以修复它。
问题是这些文件有Windows/DOS的行尾。不知道为什么,也许我开过一次窗户的机器。更重要的是,它是如何发生的,我如何才能解决它。
首先,找出所使用的文件结尾。因此,我们可以使用命令行:
file foo.sh如果输出的结果类似于:
foo.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators如果您有CRLF line terminators,那么您必须用dos2unix程序来修复它。
sudo apt-get install dos2unix
dos2unix foo.sh
file foo.sh如果还没有安装,那么只需运行apt-get (第一行)。现在应该如下所示:
foo.sh: Bourne-Again shell script, ASCII text executable现在,您可以运行它,没有任何问题使用
sh foo.sh进一步阅读有关文件、dos2unix和unix2dos的内容,您可以在这里找到:View line-endings in a text file
发布于 2015-01-05 09:28:52
作为安装dos2unix的替代方案
sed -i -e "s/\r//g" foo.sh此命令替换文件中的所有\r字符.
https://stackoverflow.com/questions/27771800
复制相似问题