我有一个看起来像这样的文件
#!/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
我在Ubuntu 14.04机器上,在执行之前我有root权限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行(它们是空的)
谢谢(:
答案 0 :(得分:4)
感谢@fejese的帮助,我设法解决了这个问题。
问题是文件有Windows / DOS行结尾。不知道为什么,也许我已经在我的Windows机器上打开过一次了。更重要的是它是如何实现的,我该如何解决它。
首先找出使用的文件结尾。因此我们可以使用命令行:
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
如果你还没有安装dos2unix,你只需要运行apt-get的东西(第一行)。 现在看起来应该是这样的:
foo.sh: Bourne-Again shell script, ASCII text executable
现在您可以使用
毫无问题地运行它sh foo.sh
您可以在此处找到有关文件,dos2unix和unix2dos的更多信息:View line-endings in a text file
答案 1 :(得分:1)
作为安装dos2unix
的替代方法:
sed -i -e "s/\r//g" foo.sh
此命令替换文件中的所有\r
个字符。