Bash script syntax error “do”?

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

#!/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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!