可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
#!/bin/sh for repo in repoA, repoB, repoC; do echo Cloning $repo. done
When I execute this (sh myscript.sh
) I get the following:
myscript.sh: line 2: syntax error near unexpected token `$'\r'' 'yscript.sh: line 2: `for repo in repoA repoB repoC;
Ideas?
回答1:
Windows uses two characters at the end of each line: '\r'
and '\n'
. Unix just uses '\n'
. Presumably you're editing this in notepad and running it in cygwin, which is why you're getting this error.
Download the Notepad++ editor, which has an option for unix-style line endings under Edit / EOL Conversion
.
回答2:
You're script was probably written on a windows machine and contains DOS newlines CR LF instead of Unix ones, LF
So you just need to convert the newlines.
You can do that using a variety of tools but my preference is dos2unix
To install it on CentOS, Fedora or RHEL do:
sudo yum install dos2unix
To install dos2unix on Ubuntu or Debian:
sudo apt-get install tofrodos sudo ln -s /usr/bin/fromdos /usr/bin/dos2unix
Now to actually do the conversion do:
dos2unix your_script.sh
Then run the script
sh your_script.sh
回答3:
You forgot a semicolon:
#!/bin/sh for repo in repoA repoB repoC; do echo Cloning $repo. done
Note that repoA, repoB and repoC are being treated as variables in this context.